?
Solved

VBS to migrate users printers to new print server

Posted on 2008-01-28
4
Medium Priority
?
959 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:peterdeal
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Lester_Clayton
ID: 20760081
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
 

Author Comment

by:peterdeal
ID: 20760272
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
 
LVL 9

Accepted Solution

by:
Lester_Clayton earned 500 total points
ID: 20760609
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
 

Author Closing Comment

by:peterdeal
ID: 31425639
OK - I see.  That's great, thanks very much for your help!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Step by step guide to Clean and Sort your windows registry! Introduction: Always remember: A Clean registry = Better performance = Save your invaluable time In this article we're going to clear our registry manually! Yes, manually! The e…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month6 days, 20 hours left to enroll

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question