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.

Regards,
Leigh


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
:MESSAGE
pause The file \\server\ASRC IT Scripts\%COMPUTERNAME%.tmp was not found. Contact HelpDesk. Unable to set default Printer...
:END

Open in new window

LVL 1
LeighWardleAsked:
Who is Participating?
 
rejoinderConnect With a Mentor Commented:
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."
                err.clear
            end if
        end if
    loop
else
    wscript.echo "The file """ & strFileName & """ was not found. Contact HelpDesk. Unable to set default Printer..."
End If

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.