Go to the watchDirectory main page
Contact
Shop
Downloads
Faq
Support Forum
Newsletter
Online Manual
watchDirectory version 4 released!
See the upgrade information page.

GdPUtil - The command prompt handyman

GdPUtil is a small command line program with a lot of diverse functionality. Some of the functionality it offers only work when run from a .bat file called by watchDirectory. At this moment, the sub function -secinfo is the only one limited like this.
You can download GdPUtil here (~ 80kb) and unzip it in a directory in your PATH. If you enter GdPUtil without parameters it will show you its usage:
Comments and suggestions are welcome!
Please email us here.
GdPUtil v1.6, (c) 2004 - GdP Software - www.gdps.dk
Please see http://www.watchdirectory.net/watchDir/GdPUtil.html
Build Date/time: Wed Aug 18 08:10:25 2004
Usage:
GdPUtil <option> <args>
Description:
  -secinfo <filename-to-find> [<server> <replace string>]
     Searches the audit log on the specified server (or local machine
     if no server is specified) for all info on filename.
     The <replace string> will replace the drive letter
     in <filename-to-find> if you specify a server.
  -join name seconds
     Waits until another GdPUtil also is invoked with the same
     'name'. If another GdPUtil is already waiting on this 'name'
     both this and the other GdPUtil will return immediately.
     If 'seconds' has passed without another GdPUtil joining
     the ERRORLEVEL will be set to 1.
     Joining only works on the local computer.
     To join a task running under another account (XP User switching)
     prefix the name with Global\
  -count source tofind
     sets the exitcode to the number of occurences of 'tofind'
     in 'source'
     Example: GdPUtil -count "HELLO" L
     would set the ERRORLEVEL to 2.
  -datetime [ [+|-]999[d|h|m|s] ] bat-file
     creates bat-file with SET commands for all kinds of date
     and time related settings.
     you can optionally add/subtract a some days/hours/minutes or seconds
     Example: GdPUtil -datetime c:\temp\dt.bat
  -matches filename wildcards
     returns 1 if filename (file.txt) matches wildcards (*.txt).
  -hasline filename string
     returns 1 if filename has a line that is exactly 'string'
  -sleep seconds
     will sleep (pause) the number of seconds specified.
  -runkill seconds program parameters of program
     starts program and stops it after 'seconds' seconds
  -play <.wav file> | <directory>
     plays the .wav file and exits
     or plays a random .wav file from <directory> and exits.
  -isdir filename
      Sets the ERRORLEVEL to 0 if 'filename' represents a directory,
      to 1 if it is another filesystem object or to 2 if filename
      does not exist.
  -isfile filename
      Sets the ERRORLEVEL to 0 if 'filename' represents a file,
      to 1 if it is another filesystem object or to 2 if filename
      does not exist.
  -deleteolderthan days directory
      Deletes all files in <directory> that are created <days> days ago
  -initobat inifile batfile
      Creates a batfile from the specified ini file.
 

SECINFO - Print information from the auditing event log

Only works when called by watchDirectory.
This option will read the Auditing event log on the specified server, or the local machine if no server is specified. It will display a report of all occurrences of the specified filename in the auditing log.
More information on auditing is available here.

If you are monitoring a directory on the local machine (your C: drive) you would use the following command line:

GdPUtil -secinfo C:\Dir\FileToCheck.ext
This command will create an auditing report of all file references of C:\Dir\FileToCheck.ext on the local computer.

If you are monitoring a directory located on a disk from another computer, use the following command line:

GdPUtil -secinfo Q:\Dir\FileToCheck.ext \\ServerName C:\Shares
The last parameter (C:\Shares) will 'replace' the drive letter (Q:) of the file you want an auditing report for. The -secinfo option reads the auditing log on the specified server and searches, in the above example, for references to the file C:\Shares\Dir\FileToCheck.ext.
If you didn't map a drive letter your command line would be:
GdPUtil -secinfo \\ServerName\Shares\Dir\FileToCheck.ext \\ServerName C:\Shares
Yes, you will need to specify the same information twice.

 

JOIN - Synchronize .bat files

You have a computer with a lot of horse power. It can run multiple programs at the same time without a problem.
Now, you have a .bat file that takes a long time because it is, for example, uploading a file to 2 FTP sites. The CPU usage of your computer is not the problem, but the slowness of those 2 FTP sites. Your first solution is to split the .bat file in 2, and start those .bat files at the same time, something like:

	 start uploadToFtp1.bat
	 start uploadToFtp2.bat
	 del file2upload.zip
	 
Problem: you delete the file before those .bat files have finished, causing them to fail. So, how do you know when it is safe to delete the .zip file?? You need to synchronize the .bat files: let them tell the main .bat file when they have finished.
If you add the following code to the end of uploadToFtp1.bat:
	 rem notify the main .bat file we have finished
	 GdPUtil -join ftp1 60
	 
and the following to upoadToFtp2.bat:
	 rem notify the main .bat file we have finished
	 GdPUtil -join ftp2 120
	 
and change your main .bat file to
	 start uploadToFtp1.bat
	 start uploadToFtp2.bat
	 rem wait for both .bat files to finish uploading
	 GdPUtil -join ftp1 60
	 GdPUtil -join ftp2 60
	 rem they are done, now it is safe to delete the .zip
	 del file2upload.zip
	 
Your main .bat file will first wait for uploadToFtp1.bat to signal it is ready, and after that for uploadToFtp2.bat. The 60 is a timeout value (measured in seconds). If the timeout expires (there is no -join done for the same name) GdPUtil will exit with an ERRORLEVEL 1 (ignored in the example).
In the above example, we let uploadToFtp2.bat wait a little longer for somebody to join because the main .bat file first waits for uploadToFtp1 to exit.

 

COUNT - How many occurences are there

Usage:

GdPUtil -count source tofind
Will count the number of times 'tofind' occurs in 'source'. The exit-code will be the number of occurences.
Example:
  GdPUtil -count abccba bc
  goto :COUNT%ERRORLEVEL%
  :COUNT0
  ECHO 0
  GOTO :QUIT
  :COUNT1
  ECHO 1
  GOTO :QUIT
  :COUNT2
  ECHO 2
  GOTO :QUIT

 

DATETIME - What is the current time

This is a very flexible command that will create a .bat file with SET statements for the current date/time. It can also do some limited arithmetic with time. Assuming the current date/time is 1 second past 1PM, on Sunday the 29th of february 2004, the following SET statements would be written to the .bat file if you use the command

GdPUtil -datetime C:\TEMP\DT.BAT

	SET DT_YEAR=2004
	SET DT_MONTH=02
	SET DT_DAY=29
	SET DT_DATE=20040229
	SET DT_DOWNUM=0
	SET DT_DOW=SUN
	SET DT_HOUR=13
	SET DT_MIN=00
	SET DT_SEC=01
	SET DT_TIME=130001
	SET DT_MILLI=428

The idea is that you first call GdPUtil to create a temporary .bat file, then you start that .bat file and finally you delete it. The resulting DT_xxx variables can then be used in your own .bat file.
So, if you want to rename a file so it has the current time as an extension, you could do:

GdPUtil -datetime c:\temp\dt.bat
call c:\temp\dt.bat
del c:\temp\dt.bat
ren c:\FileToRename.txt c:\FileToRename.%DT_TIME%

You can also do some limited arithmetic with the date/time:

GdPUtil -datetime +1d C:\TEMP\DT.BAT
adds 1 day to the current date/time.
GdPUtil -datetime +1h C:\TEMP\DT.BAT
adds 1 hour to the current date/time.
GdPUtil -datetime +1m C:\TEMP\DT.BAT
adds 1 minute to the current date/time.
GdPUtil -datetime +1s C:\TEMP\DT.BAT
adds 1 second to the current date/time.

Obviously, you can use -1m to subtract a minute, or +794d to really travel in time.

Another option is to find the date/time settings of next sunday, using this command line:

GdPUtil -datetime +sun C:\TEMP\DT.BAT
or the previous friday using:
GdPUtil -datetime -fri C:\TEMP\DT.BAT
For those weekday calculations you need to use the following abbreviations: sun, mon, tue, wed, thu, fri, sat.

 

MATCHES - Check if a string matches a mask

This option allows you to do some basic pattern matching, similar to a command prompt wildcard search (DIR *.TXT). It supports both the ? and * wildcards. The result of the test is set as the errorlevel of the program, where errorlevel 1 means a match was found.
Example:

GdPUtil -matches hello there
if %ERRORLEVEL% == 1 (ECHO MATCH) else (ECHO NOMATCH)
Would print NOMATCH
GdPUtil -matches hello ??LLO
if %ERRORLEVEL% == 1 (ECHO MATCH) else (ECHO NOMATCH)
Would print MATCH (match ignores case)
GdPUtil -matches hello *LLO
if %ERRORLEVEL% == 1 (ECHO MATCH) else (ECHO NOMATCH)
Would print MATCH
GdPUtil -matches hello *LL
if %ERRORLEVEL% == 1 (ECHO MATCH) else (ECHO NOMATCH)
Would print NOMATCH

 

HASLINE - Check if a string is present in a file

Searches the file for a line that is exactly the same as 'string'. If a line is found that matches the string, the ERRORLEVEL will be set to 1. If no match is found it will be set to 0.

rem do not process files 2 times
GdPUtil -hasline c:\temp\alreadydone.txt a-new-file.doc
if %ERRORLEVEL% == 1 GOTO :QUIT
ECHO a-new-file.doc >> c:\temp\alreadydone.txt

 

SLEEP - Pause for a few seconds

Just in case you do not have one of the other 1510 implementations of the sleep command.

GdPUtil -sleep 5
This will pause for 5 seconds.

 

RUNKILL - Start a program and stop it after x seconds

This option will start another program and, if it is not finished after x seconds, stops it again. Runkill will first politely ask the program to stop (send a WM_CLOSE message), but if that fails the program will be aborted in a fairly brute manner. For example:

GdPUtil -runkill 5 notepad.exe
starts notepad and kills it after 5 seconds.

 

PLAY - Play a .wav file

This option will play a .wav file. You can either specify a single .wav file to be played, or a directory from which a random .wav file will be selected by GdPUtil.

GdPUtil -play "c:\windows\media\windows xp startup.wav"
will play the startup sound of Windows XP.
GdPUtil -play c:\windows\media
will play one of the .wav files found in the directory.
GdPUtil will wait until the sound has finished playing. If you want to do more in your .bat file while the sound is playing, you should start the .wav like this:
start GdPUtil -play "c:\windows\media\windows xp startup.wav"

 

ISDIR - Is this a directory?

This option will check if the name passed is a directory. If it is, it will set the ERRORLEVEL to 0. If it is another file system object (not a directory), the error level will be set to 1. If the name does not exist, the ERRORLEVEL will be set to 2.
Example:

GdPUtil -isdir C:\Temp
if %ERRORLEVEL% EQU 0 ( GOTO :ISDIR ) else ( GOTO :NOTDIR )

 

ISFILE - Is this a file?

This option will check if the name passed is a file. If it is, it will set the ERRORLEVEL to 0. If it is another file system object (a directory), the error level will be set to 1. If the name does not exist, the ERRORLEVEL will be set to 2.
Example:

GdPUtil -isfile C:\Temp\hello.txt
if %ERRORLEVEL% EQU 0 ( GOTO :ISFILE ) else ( GOTO :NOTFILE )

 

DELETEOLDERTHAN - Remove all files older than x days

Deletes all files in <directory> that are created <days> days ago.
Example:

GdPUtil -deleteolderthan 14 c:\temp
All files that were created more than 14 days ago will be removed from c:\temp

 

INITOBAT - Create a .bat file based upon an .ini formatted file

If you want to use the values of .ini formatted files inside your .bat file, you can use this option to create a .bat file with all current .ini settings.
After you created this .bat file, you can call it to retrieve the value of keys.
Example:

GdPUtil -initobat c:\windows\system.ini c:\temp\systemini.bat
call c:\temp\systemini.bat drivers wave
echo the value of the wave key in the [drivers] section is %RESULT%