Batch files

When using Startprog to start a batch file you might have noticed that it always returns the value 0, even if the process you call goes wrong. this has to do with the command interpreter, it does not forward the error code from the programs it calls. It just says that the batch file has run without any problem, that the program that the batch file has called had an error is not shown.

When you create an error in the batch file you will see that Startprog will retry so we can use this to our advantage.

We have this batch file that we call using Startprog :

SomePreperationProgram %1 %2
SomeProgram %1
SomeOtherTool %2

When SomeProgram.exe creates an error, startprog will not get the message form the command interpreter. Startprog will then assume that all went well and see no harm in deleting the input file.

Change the batch file to :

SomePreperationProgram %1 %2
SomeProgram %1
if %ERRORLEVEL% = 1 then ErrorLabel
SomeOtherTool %2

In this case when SomeProgram.exe creates an error, the command interpreter is instructed to go to the label ErrorLabel which is omitted by design. It will cause the interpreter to generate an error and thus inform Startprog of the fact that something went wrong.

Please note : not all programs return good errorlevels

Logging from Batch files

It is possible to use logging in batch files, but there can be a locking issue that is badly handled by windows (you will get a deadlock)

in a called batch file you can do logging by using a statement like :

echo %TimeStamp% Did something noteworthy >>c:\log\logfile.log

But you might run into the deadlock problem when you run multiple programs simultaneously. There is an identifier that is only active for one instance at a time, and that is the %ProcessQueueID% variable. use it like :

echo %TimeStamp% Did something noteworthy >>c:\log\logfile%ProcessQueueID%.log

You will get multiple logfiles (up to MaxQueueCount) but you will never get a deadlock.

n.b. the TimeStamp variable is set once at the program start. It will not change with time.

Calling Other Programs can fail

The PATH and friends

When running other commands (either by StartProg or by a batchfile) you sometimes get strange errors. This has to do with the user context, this is default LocalSystem. by setting the environment correctly it should work in most cases :

Solution

Put at least this in the Environment string : "systemroot=c:\winnt","PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.DLL","PATH=c:\winnt\system32"

Symptoms

This trick will help with for instance windows ftp.exe, because it needs (indirectly) a few dll's called msadf.dll, rnr20.dll and msrnr.dll (indirectly because they are loaded after the program has started, and because the library ws2_32.dll needs them) . For some reason microsoft has hard coded ws2_32.dll to read these files from %systemroot%\system32.

In Windows 2003 the ftp program just did not work (nothing in logfiles), on my Win 2000 desktop it said : ftp: socket :Unknown error number

Read files from a different server

System context

Default Start Prog installs itself as a Local System process, this is nice because it is allowed to access all files on the system, this is also a bit of a security risk for the same reason. An other problem can be that this special account only exists on the server startprog is running on, any other machine will see this user as an Anonymous user. This will make it impossible to get files from an other server. If you are looking for this feature, you need to change the context of Start Prog.

Changing context

Go to the services applet in the control panel and double click ihsStartProg from the list, select the second tab (Log On) and change the setting to a network account (preferably a special user that you have created for this particular purpose, but the domain administrator will also work)

Please note that not only ihsStartProg runs in the context of this user, but also everything it starts.

Getting the files

As is also stated in the 'Design consequences' part of How It Works, There normally are no network shares activated within the context of Start Prog, you can fix this by refering to every 'drive' as the UNC it normally is connected to.

If you normally have a drive K connected to \\server\the_K_share and you want to get the files from K:\ForStartProg\ you set the basepath to \\server\the_K_share\ForStartProg.

Special considerations

When you make Start Prog get its files form an other server please keep in mind that this will give some traffic (in my example tree with 10 directory's every 10 seconds, average about 250B/s, peak of 3Kb/s). You might consider setting the interval bigger.

Using FTP

Using FTP with Startprog is done using the default windows ftp program. Create a batch file like this:

@echo off
rem start as 
rem   ftp_file <filename> [<destpath>]
rem change server, user and password

echo %spDateTime% : Transfer File '%1' to ftpserver  >> C:\Log\FTPexport%ProcessQueueID%.log

rem the logging is done in a seperate file per thread, as the batch interpreter is not thread aware


rem create the script file
echo OPEN 127.0.0.1 >C:\temp\ftpscript%ProcessQueueID%.txt

echo USER username >>C:\temp\ftpscript%ProcessQueueID%.txt

echo password >>C:\temp\ftpscript%ProcessQueueID%.txt

echo cd /%2 >>C:\temp\ftpscript%ProcessQueueID%.txt

echo TYPE bin >>C:\temp\ftpscript%ProcessQueueID%.txt

echo put %1 >>C:\temp\ftpscript%ProcessQueueID%.txt

echo BYE >>C:\temp\ftpscript%ProcessQueueID%.txt



rem run the ftp command
ftp -n -s:C:\temp\ftpscript%ProcessQueueID%.txt >C:\temp\resultfile%ProcessQueueID%.txt

rem check the result of the ftp command check the resultfile in your language !!!
findstr "File transfer completed successfully" C:\temp\resultfile%ProcessQueueID%.txt

if errorlevel = 1 goto MajorError



echo %spDateTime% : Transfer File '%1' Succeded >> C:\Log\FTPexport%ProcessQueueID%.log


del C:\temp\ftpscript%ProcessQueueID%.txt

del C:\temp\resultfile%ProcessQueueID%.txt



goto DirectQuit



:MajorError



echo %spDateTime% : -------  ERROR  ERROR  --------- >>C:\Log\FTPexport%ProcessQueueID%.log
type C:\temp\resultfile%ProcessQueueID%.txt >>C:\Log\FTPexport%ProcessQueueID%.log
goto GenerateMajorError


rem please note that this label does not exist on purpose !


:DirectQuit

echo %spDateTime% : -------------- Stop %spTaskID%, %spSTARTCOUNT% --------------- >>C:\Log\FTPexport%ProcessQueueID%.log

You might want to make sure the ftp executes correctly by checking the output of ftp, I might add that later.

Setup the Config File to have a commandline like:

CreateProcess.CommandLine=D:\ihsStartPrg\ftp_file.bat "%FileName%"

because of reasons mentioned before, make sure the PATH statement is set properly.

Gesponsorde koppelingen