Link to home
Start Free TrialLog in
Avatar of raksasas
raksasas

asked on

Help turning batch file into a vbs

I am administrator and we are working on converting some of our batch files into vbs scripts so we can have more users work with them instead of using the dos promt


I have no idea how to convert this batch file below into a vbs script

Thanks for your help
echo on
rem Removes the users cach folder (and all files/folders) inside that was created in the foxtmp directory. 
rem before the program lanuches. This is incase user improperly exited the Terminal service session. 
 
rmdir /s /q c:\foxtmp\%username%
 
rem Removes the users config.fpw that this batch files creates. This is incase user improperly exited the Terminal service session.
del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw
 
Rem Creates the users I/Leads config file to allow the Foxtmp\"username" folder to be created and used by I/Leads
Copy "C:\Program Files\Ileads58\config.fpw" "C:\Program Files\Ileads58\userConfigs\"%username%.fpw
 
Rem Adds the COMMAND=DBCALIAS="%username%" line into the Users config file 
Rem The COMMAND=DBCALIAS="%username%" line tells I/Leads where to place the I/Leads cach files for the user
Echo COMMAND=DBCALIAS="%username%" >> "C:\Program Files\ileads58\UserConfigs\"%username%.fpw
 
Rem Changes directory to execute I/Leads
cd \PROGRA~1\ILeads58\Rms
 
Rem Executes I/Leads using the user's config file that was created above.
start /WAIT /MAX "" "C:\Program Files\ILeads58\Rms\ILeads.exe" -t -cC:\PROGRA~1\ILeads58\UserConfigs/%username%.fpw
 
Rem Once I/Leads is closed delete the users folder in the foxtmp directory to free up space
Rem and to prevent crupted cach files. 
rmdir /s /q c:\foxtmp\%username%
 
Rem One I/leads is closed delete the users config file to free up space 
del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw
echo off

Open in new window

Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

see how this goes.  the FolderDelete may error depending upon system settings....

'EE update
Dim oFSO
Set oFSO = CreateObject("scripting.filesystemobject")
Dim oShell
set oShell = CreateObject("wscript.Shell")
 
'echo on'
Rem Removes the users cach folder (and all files/folders) inside that was created in the foxtmp directory. 
Rem before the program lanuches. This is incase user improperly exited the Terminal service session. 
 
	Dim sUserName, sCmd, sCfgFile
	sUserName = oShell.ExpandEnvironmentStrings("%UserName%")
 
 
'rmdir /s /q c:\foxtmp\%username%'
	oFSO.DeleteFolder "c:\fowtmp\" & sUserName,True
 
Rem Removes the users config.fpw that this batch files creates. This is incase user improperly exited the Terminal service session.
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
	sCfgFile = "C:\Program Files\ILeads58\UserConfigs\" & sUserName & ".fpw"
	'### save the name it gets used so often ###'
	oFSO.DeleteFile sCfgFile,True
 
Rem Creates the users I/Leads config file to allow the Foxtmp\"username" folder to be created and used by I/Leads
'Copy "C:\Program Files\Ileads58\config.fpw" "C:\Program Files\Ileads58\userConfigs\"%username%.fpw'
	oFSO.CopyFile "C:\Program Files\Ileads58\config.fpw",sCfgFile,True
 
Rem Adds the COMMAND=DBCALIAS="%username%" line into the Users config file 
Rem The COMMAND=DBCALIAS="%username%" line tells I/Leads where to place the I/Leads cach files for the user
'Echo COMMAND=DBCALIAS="%username%" >> "C:\Program Files\ileads58\UserConfigs\"%username%.fpw'
	set oEnv=oShell.Environment("System")
	oEnv("DBCALIAS")= sUserName
	'write username to cfgfile ??'
   Set oMyFile = ofso.CreateTextFile(sCfgFile, True)
   oMyFile.WriteLine(sUsername)
   oMyFile.Close
 
Rem Changes directory to execute I/Leads
'###cd \PROGRA~1\ILeads58\Rms  ##maynot be required'
 
Rem Executes I/Leads using the user's config file that was created above.
'start /WAIT /MAX "" "C:\Program Files\ILeads58\Rms\ILeads.exe" -t -cC:\PROGRA~1\ILeads58\UserConfigs/%username%.fpw'
	sCmd = "C:\Program Files\ILeads58\Rms\ILeads.exe -t -c" & sCfgFile
	Set oExec = oShell.Exec (sCmd)
	Do While oExec.Status = 0   'wait for completion'
	     WScript.Sleep 100
	Loop
 
Rem Once I/Leads is closed delete the users folder in the foxtmp directory to free up space
Rem and to prevent crupted cach files. 
'rmdir /s /q c:\foxtmp\%username%'
	oFSO.DeleteFolder "c:\fowtmp\" & sUserName,True
 
Rem One I/leads is closed delete the users config file to free up space 
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
	oFSO.DeleteFile sCfgFile ,True

Open in new window

Avatar of raksasas
raksasas

ASKER

BTW, Thanks.

FYI, I had to change fowtmp to foxtmp.

lets break this down into 3 parts:  Part 1 = Deleting the required folders/files  if part 3 does not get ran; Part 2 = creating the Config file need, Part 3 deleting the cach folders/files/config file when done with program.

Part 1: Error's If the  C:\foxtmp\%username% folder and the %username.fpw file is not present
Line: 16
Char: 9
Error: Path not found
&
Path: 22
Char: 9
Error: File not found

I am thinking this needs to be an If statement. If folders/files are there delete folders/files else go to next line in script.


Also part 3 should match part 1 in coding.

Now for part 2
I get the following error with the copy config.fpw  and paste with %username%.fpw.
Copy: C:\Program Files\ILeads58\config.fpw
paste to: C:\Program Files\ILeads58\UserConfigs\%username%.fpw

Also this whole line needs to be added after the copy and past into the .fpw file as is(with the %username% replaced with the username):
COMMAND=DBCALIAS="%username%"

Example for me %username% =
 C:\Program Files\ILeads58\UserConfigs\jrose.fpw

The inside of the .fpw file looks like this:



TITLE=I/LEADS
TMPFILES=C:\FOXTMP\TEMP
EDITWORK=C:\FOXTMP\TEMP
PROGWORK=C:\FOXTMP\TEMP
SORTWORK=C:\FOXTMP\TEMP
MVCOUNT= 16192
 
 
CLOCK= STATUS
 
 
_BROWSER = ""
_SPELLCHK = ""
_GENMENU = ""
_GENGRAPH = ""
_COVERAGE = ""
_SCCTEXT = ""
_CONVERTER = ""
_TRANSPORTER = ""
_BUILDER = ""
_WIZARD = ""
 
RESOURCE = OFF
DOHISTORY = OFF
CATMAN=OFF
 
 
COMMAND = STARTPATH = "\\ileads1\ILeads58$"
COMMAND=DBCALIAS="jrose" 

Open in new window

trial 2.
but you need to explain the copyfile error more.  I have added a debug output to the script to show the value of the variables just prior to the copyfile

post the debug text.

'EE update 2
Dim oFSO
Set oFSO = CreateObject("scripting.filesystemobject")
Dim oShell
set oShell = CreateObject("wscript.Shell")
Const ForAppending=8
 
'echo on'
Rem Removes the users cache folder (and all files/folders) inside that was created in the foxtmp directory. 
Rem before the program lanuches. This is incase user improperly exited the Terminal service session. 
 
        Dim sUserName, sCmd, sCfgFile , sTmpFile, sTmp
        sUserName = oShell.ExpandEnvironmentStrings("%UserName%")
 		sTmpFile = "c:\foxtmp\" & sUserName
 
'rmdir /s /q c:\foxtmp\%username%'
		
		If oFSO.FolderExists(sTmpFile) then
        	oFSO.DeleteFolder sTmpFile,True
        End if
 
Rem Removes the users config.fpw that this batch files creates. This is incase user improperly exited the Terminal service session.
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
        sCfgFile = "C:\Program Files\ILeads58\UserConfigs\" & sUserName & ".fpw"
        '### save the name it gets used so often ###'
        If oFSO.FileExists(sCfgFile) Then
        	oFSO.DeleteFile sCfgFile,True
        End if
 
 '###DEBUG###'
 WScript.Echo sUserName,sTmpFile,sCfgFile
 
Rem Creates the users I/Leads config file to allow the Foxtmp\"username" folder to be created and used by I/Leads
'Copy "C:\Program Files\Ileads58\config.fpw" "C:\Program Files\Ileads58\userConfigs\"%username%.fpw'
        oFSO.CopyFile "C:\Program Files\Ileads58\config.fpw",sCfgFile,True
 
Rem Adds the COMMAND=DBCALIAS="%username%" line into the Users config file 
Rem The COMMAND=DBCALIAS="%username%" line tells I/Leads where to place the I/Leads cach files for the user
'Echo COMMAND=DBCALIAS="%username%" >> "C:\Program Files\ileads58\UserConfigs\"%username%.fpw'
	sTmp = "COMMAND=DBCALIAS=" & Chr(34) & sUserName & Chr(34)
        'append command string to cfgfile '
   Set oMyFile = ofso.OpenTextFile(sCfgFile, ForAppending,false)
   oMyFile.WriteLine(stmp)
   oMyFile.Close
 
Rem Changes directory to execute I/Leads
'###cd \PROGRA~1\ILeads58\Rms  ##maynot be required'
 
Rem Executes I/Leads using the user's config file that was created above.
'start /WAIT /MAX "" "C:\Program Files\ILeads58\Rms\ILeads.exe" -t -cC:\PROGRA~1\ILeads58\UserConfigs/%username%.fpw'
        sCmd = "C:\Program Files\ILeads58\Rms\ILeads.exe -t -c" & sCfgFile
        Set oExec = oShell.Exec (sCmd)
        Do While oExec.Status = 0   'wait for completion'
             WScript.Sleep 100
        Loop
 
Rem Once I/Leads is closed delete the users folder in the foxtmp directory to free up space
Rem and to prevent crupted cach files. 
'rmdir /s /q c:\foxtmp\%username%'
 
	If oFSO.FolderExists(sTmpFile) then
        	oFSO.DeleteFolder stmpFile,True
        End if
 
Rem One I/leads is closed delete the users config file to free up space 
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
        If oFSO.FileExists(sCfgFile) Then
        	oFSO.DeleteFile sCfgFile,True
        End if

Open in new window

Debug Output:
jrose c:\foxtmp\jrose C:\Program Files\ILeads58\UserConfigs\jrose.fpw

It is deleting files/folders with no errors. It is also creating the *.fpw file correctly. Now its just getting the execution of the program right.

Here is what the target field of the desktop icon/shortcut looks like if doing this all manually:
"C:\Program Files\ILeads58\Rms\ILeads.exe" -t -cC:\PROGRA~1\ILeads58\UserConfigs/%username%.fpw

Hope this helps
Also would you be able to re-comment the whole script explaining the script and put in your credentials as the creator?

Thanks for the hard work.
can you try running the following command from Start / Run
"C:\Program Files\ILeads58\Rms\ILeads.exe" -t -c"C:\Program Files\ILeads58\UserConfigs\jrose.fpw"

does it work correctly ?  the space in the config filename may be an issue.  Can probably hard wire as "C:\Program Files" is equivalent to "C:\Progra~1" in about 99.9% of systems. It is possible to have Program Files stored on D: , though very rare. This would break other parts of script as well. It is possible to get the correct location of ProgramFiles from registry but generally not worthit.

I'll start adding some comments but its not one of my strong points. Thats partly why I commented the DOS commands out , so that you cna see the equivalent VBS command.
Yes using the command from the Start / Run does work
Not that you are probably busy, but any updates? I got asked about this one in particular.
post the value of the debug sCMD if doesn t fire correctly.

'EE update 3'
'Robberbaron  Feb.2009'
 
'allocate variables for the objects and create std objects'
Dim oFSO
Set oFSO = CreateObject("scripting.filesystemobject")
Dim oShell
set oShell = CreateObject("wscript.Shell")
Const ForAppending=8   'this is std value for oFSO'
 
 
'echo on'
Rem Removes the users cach folder (and all files/folders) inside that was created in the foxtmp directory. 
Rem before the program lanuches. This is incase user improperly exited the Terminal service session. 
 
        Dim sUserName, sCmd, sCfgFile , sTmpFile, sTmp
        'get the current username from shell envionment data'
        sUserName = oShell.ExpandEnvironmentStrings("%UserName%")
        
        'save the std location for temporary config files to ensure consistent use'
 		sTmpFile = "c:\foxtmp\" & sUserName
 
'rmdir /s /q c:\foxtmp\%username%'
		
		If oFSO.FolderExists(sTmpFile) then
        	oFSO.DeleteFolder sTmpFile,True
        End if
 
Rem Removes the users config.fpw that this batch files creates. This is incase user improperly exited the Terminal service session.
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
        sCfgFile = "C:\Program Files\ILeads58\UserConfigs\" & sUserName & ".fpw"
        '### save the config filename it gets used so often '
        '  this avoids typos if want to change location and is faster###'
        If oFSO.FileExists(sCfgFile) Then
        	oFSO.DeleteFile sCfgFile,True
        End if
 
 
Rem Creates the users I/Leads config file to allow the Foxtmp\"username" folder to be created and used by I/Leads
'Copy "C:\Program Files\Ileads58\config.fpw" "C:\Program Files\Ileads58\userConfigs\"%username%.fpw'
        oFSO.CopyFile "C:\Program Files\Ileads58\config.fpw",sCfgFile,True
 
Rem Adds the COMMAND=DBCALIAS="%username%" line into the Users config file 
Rem The COMMAND=DBCALIAS="%username%" line tells I/Leads where to place the I/Leads cach files for the user
'Echo COMMAND=DBCALIAS="%username%" >> "C:\Program Files\ileads58\UserConfigs\"%username%.fpw'
	sTmp = "COMMAND=DBCALIAS=" & Chr(34) & sUserName & Chr(34)
   'append command string to cfgfile ... chr(34) is double quote character'
   Set oMyFile = ofso.OpenTextFile(sCfgFile, ForAppending,false)
   oMyFile.WriteLine(stmp)
   oMyFile.Close
 
Rem Changes directory to execute I/Leads
'###cd \PROGRA~1\ILeads58\Rms  ##maynot be required'
 
Rem Executes I/Leads using the user's config file that was created above.
'start /WAIT /MAX "" "C:\Program Files\ILeads58\Rms\ILeads.exe" -t -cC:\PROGRA~1\ILeads58\UserConfigs/%username%.fpw'
 
    'create the desired application run command as a string for ease of use and espec testing'
        sCmd = chr(34) & "C:\Program Files\ILeads58\Rms\ILeads.exe" & Chr(34) & " -t -c" & Chr(34) & sCfgFile & Chr(34)
        
        WScript.Echo sCmd   '##DEBUG'
        
        Set oExec = oShell.Exec (sCmd)
        Do While oExec.Status = 0   'wait for completion'
             WScript.Sleep 100
        Loop
 
Rem Once I/Leads is closed delete the users folder in the foxtmp directory to free up space
Rem and to prevent crupted cach files. 
'rmdir /s /q c:\foxtmp\%username%'
 
		If oFSO.FolderExists(sTmpFile) then
        	oFSO.DeleteFolder stmpFile,True
        End if
 
Rem One I/leads is closed delete the users config file to free up space 
'del "C:\Program Files\ILeads58\UserConfigs\"%username%.fpw'
        If oFSO.FileExists(sCfgFile) Then
        	oFSO.DeleteFile sCfgFile,True
        End if

Open in new window

Maybe you can give a little info into the following:
First off, let me again thank you for your hard work and taking out the time to help me with this.
It appears to now be seeing the config (.fpw) file now when it is executing the command.
Now am I receiving an error in the program when it is starting up that I have never seen before and apparently not one that my support contact for the software is not familiar with either.

So, it executes from the following just fine:
desktop icon fine
start/run
batch file

any thoughts?
This was mentioned to me,

 try setting the working folder. add the following line after the "Set oShell" statement:

oShell.CurrentDirectory = "c:\Program Files\iLeads\Rms"
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is it. No errors. Thanks again.