[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to convert simple batch file to VBS?

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-05-05
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

Question by:LeighWardle
1 Comment
LVL 14

Accepted Solution

rejoinder earned 2000 total points
ID: 22927837
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


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

829 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