Tag Cloud

Saturday
Sep302006

## Batch Snippets

### Create a timestamp with Date and Time with a underscore seperating them.

set DateStamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
set TimeStamp=%TIME::=%
set TimeStamp=%DateStamp%_%TimeStamp:.=%
set TimeStamp=%TimeStamp: =0%
echo %TimeStamp%
:: TimeStamp is like 20120326_13034087


### Tests to see if an svn export was successful. This snippet could be applied to a lot of situations where you want to test if a command was successful.

svn export <REPO_URL> C:\path\to\outputdir --username <USERNAME> --password <PASS> --non-interactive
set svnErrorLevel=%ERRORLEVEL%
IF NOT %svnErrorLevel% == "0" GOTO FAILED

rem PUT STUFF HERE THAT SHOULD EXECUTE ONLY IF EXPORT WAS SUCCESSFUL

GOTO end
:FAILED


### How to test to see if an argument is set. (Notes: 1) there must be quotes around the %1 and 2) the goto must be on same line as if)

if "%1" == "" goto displayUsage

NOTE: I asked a question about this on stackover flow and got a good answer that works even when you have variables that might have quote in it. Google blak3r on stackoverflow + batch test and you’ll probably find it.

### Add a user definable delay, where the number following -n is number of seconds

ping -n 2 1.1.1.1 > NUL