在批处理和 VBS 混合中使用变量
Using variables in batch & VBS hybrids
此 thread 概述了如何编写可能包含多种脚本语言组合的批处理混合代码,例如批处理、VBS、JScript、PowerShell 等。问题是,批处理混合代码是否处理 "foreign" 语言块作为 "functions",这意味着对这些块的调用可能包括像常规和延迟扩展批处理变量这样的参数,它们被引用为像 %1、%2 等通常的参数?
下面的示例显示了解压缩文件任务的方法,同时在 Win 10 中使用 this file unzip code, but it gives an error in Win10 64-bit - why? Note, the linked file unzip code gives an 以及 运行,但不同。
<!-- : Begin batch script
@echo off
set "dir=C:\Temp\" & set "file=%USERPROFILE%\Downloads\archive.zip\"
cscript //nologo "%~f0?.wsf" "%dir%" "%file%"
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(%1) Then
fso.CreateFolder(%1)
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(%2).items
objShell.NameSpace(%1).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
</script></job>
:: Error
..\test.bat?.wsf(9, 8) Microsoft VBScript compilation error: Invalid character
在 vbscript 中,第一个参数是:wscript.Arguments(0)
第二个参数是:wscript.Arguments(1)
所以,你应该这样写:
`
----- Begin wsf script --->
<job><script language="VBScript">
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(wscript.Arguments(0)) Then
fso.CreateFolder(wscript.Arguments(0))
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(wscript.Arguments(1)).items
objShell.NameSpace(wscript.Arguments(0)).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
</script></job>
此 thread 概述了如何编写可能包含多种脚本语言组合的批处理混合代码,例如批处理、VBS、JScript、PowerShell 等。问题是,批处理混合代码是否处理 "foreign" 语言块作为 "functions",这意味着对这些块的调用可能包括像常规和延迟扩展批处理变量这样的参数,它们被引用为像 %1、%2 等通常的参数?
下面的示例显示了解压缩文件任务的方法,同时在 Win 10 中使用 this file unzip code, but it gives an error in Win10 64-bit - why? Note, the linked file unzip code gives an
<!-- : Begin batch script
@echo off
set "dir=C:\Temp\" & set "file=%USERPROFILE%\Downloads\archive.zip\"
cscript //nologo "%~f0?.wsf" "%dir%" "%file%"
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(%1) Then
fso.CreateFolder(%1)
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(%2).items
objShell.NameSpace(%1).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
</script></job>
:: Error
..\test.bat?.wsf(9, 8) Microsoft VBScript compilation error: Invalid character
在 vbscript 中,第一个参数是:wscript.Arguments(0)
第二个参数是:wscript.Arguments(1)
所以,你应该这样写: `
----- Begin wsf script --->
<job><script language="VBScript">
set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(wscript.Arguments(0)) Then
fso.CreateFolder(wscript.Arguments(0))
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(wscript.Arguments(1)).items
objShell.NameSpace(wscript.Arguments(0)).CopyHere(FilesInZip)
set fso = Nothing
set objShell = Nothing
</script></job>