在我作为桌面支持的角色中,我一直在为一些特定的工作编写一些批处理文件。
我一直在编写Profile Backup脚本几个月的脚本,随着角色的需要而变化,并且随着新问题的出现而变化。
我正在尝试从批处理文件中运行以下VBS脚本,并将其输出指向另一个文件夹。
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
这个VBS脚本(不是我)将用户映射网络驱动器导出到CMD文件,该文件使用netuse命令重新映射它们。
如果VBS脚本在批处理文件之外运行,那么它的工作正常,但是当它使用cscript等在批处理内部运行时,它会创建输出文件Mapped_Network_Drives_%username%。cmd,但它是空的
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
上面是我一直在使用的代码的剪辑。
答案 0 :(得分:0)
试试这个
vbs part :更正了驱动器集合的迭代次数
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
批量部分:确保正确处理所有路径引用
@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