0
votes

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
1
Note: Create variable in vbs (vbscript), pass as parameter to bat (batch) fileCarl-eieio
Please refrain from adding tags like "solved" to the subject of your question. If your problem is resolved you should accept the answer presenting the (best) solution. If you found a solution yourself it's perfectly fine if you post an answer of your own and accept that.Ansgar Wiechers

1 Answers

0
votes

When a parent process execute a child one and waits for it to terminate, there is no way that the parent get an environment variable created by the child. The child environment is local to it and it is released when the child process terminate. You need to use a different way to return the value to the parent; for example, via a disk file or redirected Stdin/Stdout.

I used this method, but in the opposite relation: a Batch file that execute a VBS script as child and read its result via Stdout output:

for /F "delims=" %%a in ('Cscript //nologo VBScript.vbs') do set result=%%a

In previous line the VBS program just write the result to Stdout.

I also wrote a Batch-JScript hybrid script with this scheme:

  • The Batch file is executed from the command-line.
  • The Batch file execute the JScript code.
  • The JScript code create some environment variables with PROCESS type.
  • The JScript code does NOT terminate, but re-execute the Batch file again!
  • The Batch file have a method to know if it is executed the first time (from the command prompt) or re-executed from the JScript code. In the second case, it can use the environment variables created by the JScript code.