Batch
From MohidWiki
Batch files are the original ms-dos scripting language. A batch file is a script that contains a list of ms-dos commands, with one command per line. When you try to execute the batch file, it will execute the ms-dos commands, one after another, waiting for each command to finish before starting the next one.
Examples
For
The following example will output each line of the text file to the stdout:
> FOR /F %i in (list.txt) DO @echo %~ni
The following example will allow to parse and edit the environment variables (invoke the command line with CMD /V):
> for /F %i in ('dir /B /S *_1.dat') do @set fil=%i & copy /Y !fil! !fil:1=2!
If you want to write FOR loops in a batch file instead of the command line, then use %%i instead of %i.
To place the first line of a file into a variable:
> SET /P _MyVar=<MyFilename.txt
To place the last line of a file into a variable:
> FOR /F "tokens=*" %A IN (C:\boot.ini) DO SET TestVar=%A
Nested FOR Loops
> FOR /F %a IN (filelist.txt) DO (FOR /F %b IN (salinitytemperature.txt) DO echo %a %b)
Nested FOR loops encapsulated in a batch file
for /F %%a in (%1) do ( for /R %%b in (../*) do ( echo %%a echo %%b ) )
Delimeters
This command will look for the first underscore in the list of filenames and will split each filename in two at the underscore.
> for /f "tokens=1-4 delims=_" %i in ('dir /B *.eps') do @echo %i %j %k %l
Misc
Manipulating environment variables
Substring find and replace
> set HELLO=HelloWorld > echo %HELLO:l=w% echo> HewwoWorwd
Extension stripping
> For /F %%i in (list.txt) DO echo %%~ni
See more help
>set /?
Escaping characters
<,|,> and % characters are special characters in dos. To escape them use the ^ character or a double %
> echo ^<example^> <example>
Displaying blank lines
Use command echo to display a blank line instead of its default status result
>echo.
Sample applications
Copy a clean MOHID project
Here's a combination with a batch file, that creates a clean copy of a MOHID simulation:
> copysimulation MySimulation
#copysimulation.bat content #Input argument %1 stands for simulation name @echo off set COPYHOME=C:\Program Files\2gif xcopy %1\*.* %1_copy /E /EXCLUDE:%COPYHOME%\exclui.txt perl %COPYHOME%/replace.pl 's/%2/\.\.\/\.\./g' * perl %COPYHOME%/replace.pl 's/\\/\//g' * @echo on
Remove all openmp directives from the code
NOTE: cygwin or gnuwin32 with all the gnu tools executables must be installed and accessible from the path command line.
$ for /F %i in ('dir /B /S *.F90') do @cat %i | grep -v !$OMP > %~ni.tpp $ for /F %i in ('dir /B /S *.tpp') do @copy %i %~ni.F90 $ for /F %i in ('dir /B /S *.tpp') do @del %i
Appending hourly data files from Instituto Hidrográfico
for /F %i in ('dir /B *') DO @type %i | grep MCAS >> one-file.txt