Link to home
Start Free TrialLog in
Avatar of peterdeal
peterdeal

asked on

VBS to migrate users printers to new print server

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

Avatar of Lester_Clayton
Lester_Clayton

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

Avatar of peterdeal

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Lester_Clayton
Lester_Clayton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK - I see.  That's great, thanks very much for your help!