Using variables in batch & VBS hybrids

2019-05-23 08:29发布

This thread outlines how to code batch hybrids that may include a combination of several scripting languages, such as batch, VBS, JScript, PowerShell, etc. The question is, whether a batch hybrid treats "foreign" language blocks as "functions", meaning calls to these blocks may include arguments like regular and delayed expansion batch variables, that are referenced as usual arguments like %1, %2, etc?

Example below shows the approach in the task of unzipping a file, while using this file unzip code, but it gives an error in Win10 64-bit - why? Note, the linked file unzip code gives an error as well when run in Win 10, but a different one.

<!-- : 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

1条回答
看我几分像从前
2楼-- · 2019-05-23 08:46

In vbscript the first argument is : wscript.Arguments(0)

the second argument is : wscript.Arguments(1)

So,you should write it like that : `

----- 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>
查看更多
登录 后发表回答