Difference between revisions of "Batch"
From MohidWiki
(→Duplicating a Run in a MOHID Simulation) |
(→Sample applications) |
||
Line 97: | Line 97: | ||
sed -e "s/%1/%2/g" %%i > %%i.bak | sed -e "s/%1/%2/g" %%i > %%i.bak | ||
move /Y %%i.bak %%i | move /Y %%i.bak %%i | ||
+ | ) | ||
+ | |||
+ | === Set a run number === | ||
+ | echo off | ||
+ | REM REM ATTENTION: Must be run with the CMD /V option! | ||
+ | REM ARG1 %1 SOURCE RUN ID | ||
+ | |||
+ | REM Activate the chosen run | ||
+ | for /F %%i in ('dir ..\*Nomfich.dat /S /B ') do ( | ||
+ | REM @echo copy /Y %%~di%%~pi..\dat\Nomfich_%1.dat %%i | ||
+ | set MYDIR=%%~di%%~pi | ||
+ | copy /Y !MYDIR:~0,-4!data\Nomfich_%1.dat %%i | ||
) | ) | ||
Revision as of 10:54, 21 March 2011
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 subs=21 @set fil=%i & copy /Y !fil! !fil:20=%subs%!
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
Duplicating a Run in a MOHID Simulation
This batch file duplicates a run with a new number (and updates the Nomfich files correspondingly). It requires a CMD /V console calling and also the gnu utility Sed.
@echo off REM DUPLICATE_RUN.BAT REM ATTENTION: Must be run with the CMD /V option! REM ARG1 %1 SOURCE RUN ID REM ARG2 %2 TARGET RUN ID REM Duplicates the filenames for /F "delims=*" %%i in ('dir /S /B *_%1.dat') do ( set NAME=%%i @echo Created !NAME:%1=%2! copy /Y "!NAME!" "!NAME:%1=%2!" ) REM Updates the nomfich_N.dat files for /F %%i in ('dir /S /B Nomfich_%2.dat') do ( sed -e "s/%1/%2/g" %%i > %%i.bak move /Y %%i.bak %%i )
Set a run number
echo off REM REM ATTENTION: Must be run with the CMD /V option! REM ARG1 %1 SOURCE RUN ID REM Activate the chosen run for /F %%i in ('dir ..\*Nomfich.dat /S /B ') do ( REM @echo copy /Y %%~di%%~pi..\dat\Nomfich_%1.dat %%i set MYDIR=%%~di%%~pi copy /Y !MYDIR:~0,-4!data\Nomfich_%1.dat %%i )