How to convert simple batch file to VBS?

Posted on 2008-11-10
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

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now