Wednesday, May 5, 2010

Scripting: Batch files - how to do stuff

Batch files generally start with:

@echo off
cls

If you want to call a batch file within another batch file:

CALL .\folder\batchfile.cmd

If you want to set a variable:

SET VARIABLENAME=value

To use an IF loop to check if a variable is right:

IF %MODE%==ALL (
Echo Your mode is set to copy inital data, montie, and user folders
CALL .\Scripts\InitialDataCopy.cmd
CALL .\Scripts\InitialUserCopy.cmd
GOTO END
)

To check if a file exists:

IF EXIST %TEXTFILE_BASE%\%TEXTFILE_NAME% GOTO READFILE

To read in values from a CSV file and echo them to screen:

For /F "tokens=1-3 delims=,. " %%a in (%VAR_CONTAINING_FILENAME%) Do (
echo %%a, %%b, %%c
)

Tokens: 1-3 means it will read up to three variables on each line, each assigned as %%a, %%b, and %%c.
delims: the delimiter between each variable. For a CSV file this will be the comma ",".

No comments:

Post a Comment