Home   Notes   Contact Me

Batch Files (Windows/DOS)

Local

Current Best External Site

http://www.robvanderwoude.com/batchcommands.php

External


Touch files opened for edit it Perforce

@echo off setlocal enabledelayedexpansion enableextensions set P4USER=jpetriti set P4CLIENT=jpetriti-dt-depot-IMAGINE_A-2 SET THEFILE= FOR /F %%I IN ('p4 opened') DO ( SET THEFILE=%%I REM echo x===================================================x REM echo THEFILE !THEFILE! REM USE delims= with no value so that it doesn't parse FOR /F "delims=" %%J IN ('p4 fstat -T clientFile %%I') DO ( REM ECHO J is %%J SET LINE_OF_RESULTS=%%J REM ECHO LINE_OF_RESULTS is !LINE_OF_RESULTS! SET ACTUALFILE=!LINE_OF_RESULTS:~15! CALL :TOUCH !ACTUALFILE! ) ) goto theEND :TOUCH ECHO File %1 SET FILESDIR=%~p1 SET FILEONLY=%~n1%~x1 ECHO %FILESDIR% ECHO %FILEONLY% PUSHD %FILESDIR% COPY /B !FILEONLY!+,, POPD GOTO:EOF :theEnd

String Cutting Off the End

for /F "tokens=1 delims=#" %%J in ("//depot/fred.vcproj#86") do ( ECHO %%J ) Shows: //depot/fred.vcproj

stderr piping to stdout

# pipe stdout only: somecommand.exe > log.txt # pipe stdout and stderr somecommand.exe > log.txt 2>&1

Assign Result of a Command to an Environment Variable

REM ========== from command line: for /f %i in ('date/t') do set var=%i REM ========== from a batch file: for /f %%i in ('date/t') do echo a line of the results is %%i for /f %%i in ('date/t') do ( echo a line of the results is %%i )

Gotchas!

  1. In bat files, For variables need to have two %'s but on the command line they just need one %

Directory Stuff

rem get the current directory set CURRENT_DIR=%cd% rem show it echo Current Directory is %CURRENT_DIR% rem change it cd .. set NEW_DIR=%cd% echo New dir is %NEW_DIR% rem go back to original directory cd %CURRENT_DIR%

Environment Variables

Checking to see if a var exists

IF DEFINED varname ECHO yes IF NOT DEFINED varname ECHO no

Console, run in a new Console

Start command runs in a new window and sort of 'forks' (that is the original window is free to do other stuff while the 'start'ed process is running

start "console name" bash -c "tail -f log.txt"

File Tests

#File Existance

IF EXIST %FILENAME% GOTO gotoTarget
IF NOT EXIST %FILENAME% GOTO gotoTarget

Log Rotate Examples

Apache log backup to filename appended with date and time and .txt
Usage from cygwin:

cmd
logrotate.bat *.log
exit
@echo off

SET STOP=T

IF %STOP% EQU T (..\bin\Apache -k stop)

FOR /F "tokens=2-4 delims=/ " %%a IN ('date /t') DO (SET DATE=%%c-%%a-%%b)
REM echo The date is %DATE%

FOR /F "tokens=1-3 delims=: " %%a IN ('time /t') DO IF %%c EQU PM (SET /A HOUR=%%a + 12) ELSE (SET HOUR=%%a)
REM echo The hour is %HOUR%

FOR /F "tokens=1-3 delims=: " %%a IN ('time /t') DO (SET MINUTE=%%b)
REM echo The minute is %MINUTE%

SET PRE=%DATE%-%HOUR%-%MINUTE%-
REM echo The pre is %PRE%

SET SUF=-%DATE%-%HOUR%-%MINUTE%
REM echo The suf is %SUF%

FOR %%A IN (%1) DO (
	IF EXIST %%A%SUF%.txt (echo Already Rotated to %%A%SUF%.txt)
	IF NOT EXIST %%A%SUF%.txt (
REM		echo %%A
REM		echo %%A%SUF%.txt
		COPY %%A %%A%SUF%.txt
		DEL %%A
	)
)

IF %STOP% EQU T (..\bin\Apache -k start)

Note: Don't let it recursively affect files

@echo off

FOR %%A IN (%1) DO FOR /F "tokens=1-3 delims=/ " %%a IN ("%%~tA") DO (
	REM name to use: %%~nxA-%%c-%%a-%%b.txt
	IF NOT EXIST %%c-%%a-%%b-%%~nxA.txt (
REM		echo %%A
REM		echo %%c-%%a-%%b-%%~nxA.txt
		REN %%A %%c-%%a-%%b-%%~nxA.txt
	)
)

Conditionals

IF NOT EXIST log.txt GOTO NOFILE
del log.txt
:NOFILE

IF EXIST log.txt GOTO DOIT1
exit 1
:DOIT1
exit 0

For Loops

SET MYLIST=one two three four
FOR %%i IN (%MYLIST%) DO (
  echo do one thing to %%i
  echo do another thing to %%i
)

Variables, user created

SET MyVAR=fred
echo MyVar contains %MyVAR%

Note: You can type help {commandname} to get help on the commands from a command shell

Table of Commands

DescriptionCommandDetailsExample
AssignmentSET {varname}={value}SET PROGNAME=Unknown
Comment::
REM
rem
Begins a Comment line:: Comment 1
REM Comment 2
rem Comment 3
Compare Stringsif (%var1%) == (%var2%) {?command?}
Compare Strings
Works if either is empty string as well
if "%var1%" == "%var2%" {?command?}
File ExistanceIF EXIST %FILENAME%IF EXIST %FILENAME% GOTO ItIs
echo "no such file"
exit 1
:ItIs
echo "file exists"
For LoopFOR %%i IN (%SET%) DO (
{thing1 to do}
{thing2 to do}
)
Loop variable must be 1 letter long
SET MYLIST=one two three four
FOR %%i IN (%MYLIST%) DO (
  echo do one thing to %%i
  echo do another thing to %%i
)
Label:{labelname}:label1
:ErrBadValueLabel
Shift argsshiftShifts args %2 becomes %1 and so on

Commandline Parameters

%1 First Parameter
%0 Program name
%* All Parameters (except program name)
%~f1 Expands to fully qualified path name of %1
%~d1 Expands to the drive number from %1
%~p1 Expands to the path of %1
%~n1 Expands to the filename of %1 (without the extension)
%~x1 Expands to the extension of %1
%~s1 Expands to the short name of %1
%~sn1 Expands to the filename of the short name of %1
%~sx1 Expands to the extension of the short name of %1