Solved, scroll down to Clean up #2.
Goal: get the filename variable out of the bat file, passing it back to the calling vbs so it can be used to copy the log file to a central repository.
The test.bat sets variables to establish a file name. It has to end and have the log file closed, before the log file can be copied.
---- 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%
Moved into this direction: (now I'm trying to pass arg to 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
--- Now its working, but its no longer silent and a DOS window opens -------- Edit: added ",0" at the end of the WshShell.Run line - runs silently
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
----- Cleaned up
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
---- Clean up #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
--- As reference --- 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