0

I have been writing some batch files to some specific job in my role as Desktop Support.

I have been scripting a Profile Backup script for a couple of months now, changing as the role requires and as new issues crop up.

I am trying to run the following VBS script from inside my batch file and point its output to another folder.

Set oNet= WScript.CreateObject("WScript.Network")
Set oDrives = oNet.EnumNetworkDrives
oUser = oNet.UserName
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("Mapped_Network_Drives_"&oUser&".cmd", True)
For i = 0 to oDrives.Count - 1
oFiletxt.WriteLine "Net Use " & oDrives.Item(i) & " " & chr(34)& oDrives.Item(i+1) & chr(34)  & " /persistent:yes"
Next
oFiletxt.Close

This VBS script was made (not by me) to export a user Mapped Network Drives to a CMD file that uses the netuse command to remap them.

If the VBS script is run itself outside of the batch file then its works fine, but when it is run inside the batch using cscript, etc, then it creates the output file Mapped_Network_Drives_%username%.cmd, but it is empty.

REM pushes script to use batch file Drive Letter as working directory
setlocal & pushd %~d0
ECHO =============================
ECHO Creating Backup Folder
ECHO =============================
MD "%UserName%_Backup"
ECHO =============================
ECHO Backing Up Mapped Network Drives
ECHO =============================
REM pushes script to use Backup Folder as working directory
pushd "%~d0%UserName%_Backup"
REM Runs VB script to backup Mapped Network Drives
cscript //nologo "%~dp0Mapped_Network_Drives.vbs"
pause

Above is a snipped of the code i have been working with.

Scott Smith
  • 25
  • 1
  • 4
  • Works for me. Hint: Don't forget to call `popd` (twice) at the end (to be consistent). Also on my machine where i only have 1 mapped network drive the `oDrives.Count` is 2 (throwing a `Subscript out of range` but maybe my WMI is messed up). Bottom line, the batch run creates the `.cmd` file. – CristiFati Jun 03 '15 at 07:12

1 Answers1

0

Try with this

vbs part : Corrected iteration over the drive collection

Option Explicit 

Dim userName, drives
    With WScript.CreateObject("WScript.Network")
        userName = .UserName
        Set drives = .EnumNetworkDrives 
    End With 

Dim drive
    With WScript.CreateObject("Scripting.FileSystemObject").CreateTextFile("Mapped_Network_Drives_" & userName & ".cmd", True)
        For drive = 0 To (drives.Count-1) Step 2
            .WriteLine "net use " & drives.Item(drive) & " """ & drives.Item(drive+1) & """ /persistent:yes"
        Next 
        .Close
    End With 

batch part: Ensures all path references are properly handled

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getCurrentBatchFile _f0
    for %%a in ("%_f0%") do (
        set "backupFolder=%%~da\%username%_Backup"
        set "scriptsFolder=%%~dpa"
    )

    md "%backupFolder%" 2>nul
    pushd "%backupFolder%" && (
        cscript //nologo "%scriptsFolder%\Mapped_Network_Drives.vbs"
        popd
    ) || (
        echo Backup folder does not exist
    )

    goto :eof

:getCurrentBatchFile returnVar
    set "%~1=%~f0"
    goto :eof
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • @ScottSmith, you posted a vbscript with problems (mainly iteration over the list of drives), and the answer includes a corrected version. You posted a batch file with non clear paths references when changing folder and/or drives, `pushd "%~d0%UserName%_Backup"` fails when the script is located in the same drive that the backup and the current folder is not the root of the drive (and `%0` references [do not always return](http://stackoverflow.com/a/26851883/2861476) what you expect) and the answer includes a more secure version. What am I missing? – MC ND Jun 03 '15 at 08:51
  • You are not missing anything and thank you for the help. You have actually explained what you have done in more detail above which is what I was looking for. I wasn't understanding how to batch part would fit into my current script. – Scott Smith Jun 03 '15 at 19:58