已解决,向下滚动至清理#2。
目标:从bat文件中获取filename变量,将其传递回调用vbs,以便将其用于将日志文件复制到中央存储库。
test.bat设置变量以建立文件名。在复制日志文件之前,它必须结束并关闭日志文件。
---- vbs to call a bat file in silent mode - works
Set WshShell = CreateObject("WScript.Shell" )
rem Next line, exit with %mFileName% from test.bat
WshShell.Run chr(34) & "C:\hgis\test.bat" & Chr(34), 0
Set WshShell = Nothing
rem MsgBox (%mFullName%) - doesn't work
WScript.Sleep 60000
Dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile "C:\hgis\%mFullName%", "C:\t\" - doesn't work, a simple file copy does
---- test.bat - works
@echo off
echo Processing test, please wait....
set "mTimestamp=%time:~-8,2%%time:~-5,2%%time:~-2,2%"
set "mDatestamp=%date:~-4,4%%date:~-7,2%%date:~-10,2%"
set "mFileName=%USERNAME%-%mDatestamp%-%mTimestamp%-hgsd.pd.txt"
set "mFullName=c:\hgis\%mFileName%"
echo Opened new log file. >%mFullName%
echo === Starting Time Stamp ============= >>%mFullName%
time /t >>%mFullName%
rem actions
time /t >>%mFullName%
echo.
echo Test completed.
exit /b %mFileName%
向这个方向移动:(现在我试图将arg传递给test.bat)
Set WshShell = CreateObject("WScript.Shell" )
Set objNetwork = CreateObject("Wscript.Network")
arg = timeStamp()
MsgBox (arg)
rem WScript.Sleep 5000
WshShell.Run chr(34) & "test.bat" & Chr(34), 0
Set WshShell = Nothing
rem WScript.Sleep 60000
rem Dim filesys
rem set filesys=CreateObject("Scripting.FileSystemObject")
rem filesys.CopyFile "C:\hgis\%mFullName%", "C:\t\"
rem filesys.CopyFile "C:\hgis\dummybatch.bat", "C:\t\"
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
timeStamp = objNetwork.UserName & "-" & timeStamp & "-hgsd.pd.txt"
End Function
---现在它正常工作,但它不再是静音,DOS窗口打开了 --------编辑:在WshShell.Run行的末尾添加“,0” - 静默运行
public param1
Set objNetwork = CreateObject("Wscript.Network")
Set WshShell = CreateObject("WScript.Shell" )
param1 = timeStamp()
WshShell.Run "c:\hgis\test.bat " + param1,0
Set WshShell = Nothing
WScript.Sleep 6000
Dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile "C:\hgis\" + param1, "C:\t\"
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
timeStamp = objNetwork.UserName & "-" & timeStamp & "-hgsd.pd.txt"
End Function
-----清理
Public mFilename, mDir
Set objNetwork = CreateObject("Wscript.Network")
Set WshShell = CreateObject("WScript.Shell" )
mDir = "c:\hgis\"
mFilename = objNetwork.UserName & "-" & timeStamp() & "-hgsd.pd.txt"
WshShell.Run mDir + "test.bat " + mFilename,0
Set WshShell = Nothing
WScript.Sleep 3000
Dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile mDir + mFilename, "C:\t\"
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
End Function
----清理#2
Public filesys, mDir, mFilename
Set filesys = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set WshShell = CreateObject("WScript.Shell" )
mDir = "c:\hgis\"
mFilename = objNetwork.UserName & "-" & timeStamp() & "-hgsd.pd.txt"
WshShell.Run mDir + "test.bat " + mFilename,0
Set WshShell = Nothing
WScript.Sleep 3000
filesys.CopyFile mDir + mFilename, "C:\t\"
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
End Function
---作为参考--- test.bat
@echo off
set mFullName=%1
echo Open new log file. >%mFullName%
echo === Starting Time Stamp ============= >>%mFullName%
time /t >>%mFullName%
ipconfig >>%mFullName%
time /t >>%mFullName%
echo === Ending Time Stamp =============== >>%mFullName%
exit
答案 0 :(得分:0)
当父进程执行子进程并等待它终止时,父进程无法获取子进程创建的环境变量。子环境是本地环境,并在子进程终止时释放。您需要使用不同的方式将值返回到父级;例如,通过磁盘文件或重定向的Stdin / Stdout。
我使用了这种方法,但是处于相反的关系:一个批处理文件,它作为子程序执行VBS脚本并通过Stdout输出读取其结果:
for /F "delims=" %%a in ('Cscript //nologo VBScript.vbs') do set result=%%a
在前一行中,VBS程序只是将结果写入Stdout。
我还用这个方案写了一个Batch-JScript混合脚本: