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

peterdealAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lester_ClaytonCommented:
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

0
peterdealAuthor 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

0
Lester_ClaytonCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peterdealAuthor Commented:
OK - I see.  That's great, thanks very much for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.