How to convert simple batch file to VBS?

Hi Experts,

I would like to convert this batch file to VBS.

All it does basically is to read text (a single record) from "%COMPUTERNAME%.tmp" and then call another VBS script using that text as an argument.


if not exist "\\server\ASRC IT Scripts\%COMPUTERNAME%.tmp" goto MESSAGE
	rem The file %COMPUTERNAME%.tmp exists
	rem Get the Printer Share Name
	rem The file "%COMPUTERNAME%.tmp" (e.g. "PC001.tmp") will contain a UNC like "\\server\printerName" .
	set/p printer_share=<"\\server\ASRC IT Scripts\%COMPUTERNAME%.tmp"
	echo printer_share is %printer_share%
	rem pause calling Add the Specified Printer.vbs
	if not X%printer_share% == X "\\server\ASRC IT Scripts\Add the Specified Printer.vbs" "%printer_share%"
	rem pause
goto END
pause The file \\server\ASRC IT Scripts\%COMPUTERNAME%.tmp was not found. Contact HelpDesk. Unable to set default Printer...

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

This script does what your batch file does plus it maps the printer within the one script thus eliminating the calling of the second script.  :-)
On error resume next
Const ForReading = 1
Set objNetwork = CreateObject("WScript.Network")
strComputer    = objNetwork.ComputerName
strFileName    = "\\server\ASRC IT Scripts\" & strComputer & ".tmp"
Set objFSO     = CreateObject("Scripting.FileSystemObject")
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WshShell   = CreateObject("WScript.Shell")
If objFSO.FileExists(strFileName) Then
    Set objDataFile = objFSO.OpenTextFile(strFileName,ForReading)
    Do until objDataFile.AtEndofStream
        strPrinterPath = objDataFile.ReadLine
        if strPrinterPath <> "" then
            result = WshShell.Popup("Connecting to printer """ & strPrinterPath & """" & vbCRLF & "Please wait...", 3)
            'WSHNetwork.SetDefaultPrinter strPrinterPath
            WSHNetwork.AddWindowsPrinterConnection strPrinterPath
            if err.number <> 0 then
                wscript.echo "Printer """ & strPrinterPath & """ could not be mapped."
            end if
        end if
    wscript.echo "The file """ & strFileName & """ was not found. Contact HelpDesk. Unable to set default Printer..."
End If

Open in new window


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
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.