We help IT Professionals succeed at work.

VBS to migrate users printers to new print server

peterdeal
peterdeal asked
on
Hi,

I've installed a new print server and have migrated the printers already.  They are working fine.

I'm working on a script to remap all users printers to the new print server (QED-PRT), which also works fine.

I create a txt file on the root of C:\ as a check file that indicates the migration has already been run on that PC, but that means that if more than 1 user logs into the PC - the printers will only be migrated for the 1st person that logs in = bad idea.

I want to create the txt file PRT_MIG.txt in "C:\Documents and Settings\%username%\Local Settings" but when changing the path to "C:\Documents and Settings\%username%\Local Settings" - I get Path not found error.

I'm assuming this is something to do with long file names, but have no idea how to get around it.

Any ideas?
dim filesys, filetxt, getname, path
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile("C:\Documents and Settings\%username%\Local Settings\PRT_MIG.txt", True)
path = filesys.GetAbsolutePathName("C:\Documents and Settings\%username%\Local Settings\PRT_MIG.txt")
getname = filesys.GetFileName(path)
filetxt.WriteLine("Printers-Migrated to QED-PRT")
filetxt.Close

Open in new window

Comment
Watch Question

Try this code
dim filesys, filetxt, getname, path, strProfilePath, objShell, objEnvironment
 
Set objShell = CreateObject("WScript.Shell")
Set objEnvironment = objShell.Environment("Process")
strProfilePath = objEnvironment("USERPROFILE")
 
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile(strProfilePath & "\Local Settings\PRT_MIG.txt", True)
path = filesys.GetAbsolutePathName(strProfilePath & "\Local Settings\PRT_MIG.txt")
getname = filesys.GetFileName(path)
filetxt.WriteLine("Printers-Migrated to QED-PRT")
filetxt.Close

Open in new window

Author

Commented:
Hey,

Cool - that works.

Seems as though I have the same problem affecting another part of the code - where it checks to see if the txt file already exists - I tried chopping your code around, but can't seem to fix it.

Can you see where I'm going wrong?

Is this a long file name issue?  Any idea where I can find some good info on how to bypass this in the future - other than to learn vbs properly!

Thanks
S
Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("C:\Documents and Settings\%username%\Local Settings\PRT_MIG.txt") Then
        Wscript.Echo "PRINTERS ALREADY MIGRATED"
Wscript.Quit
 
End If

Open in new window

The problem is not with long file names.  You cannot use %username% or other variables in paths, VBScript does not understand it.  That is why you have to substitute %username% yourself using the method shown in my example.  Your new code should be:


Set objShell = CreateObject("WScript.Shell")
Set objEnvironment = objShell.Environment("Process")
strProfilePath = objEnvironment("USERPROFILE")
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strProfilePath & "\Local Settings\PRT_MIG.txt") Then
        Wscript.Echo "PRINTERS ALREADY MIGRATED"
        Wscript.Quit
    End If

Open in new window

Author

Commented:
OK - I see.  That's great, thanks very much for your help!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.