How to use gdal_calc in a batch file - batch-file

I'm trying to create a mask file from several input images using gdal with a batch windows file. However, the system is sending me a error when I use the "!" on the comparison calc, and after the first round, all the variables had read as a string.
My code is the following:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mypath=F:\my_in_path\
SET path_salida=F:\my_out_path\
FOR /F %%i IN ('DIR /B %mypath%*.tif') DO (
SET infile=%%i
SET outfile=!infile!
echo %mypath%!infile!
echo %path_salida%!outfile!
gdal_calc -A %mypath%!infile! --outfile %path_salida%!outfile! --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet
)

As you've provided no feedback over half a day since my comment, here is an example to test and provide feedback on:
#Echo Off
Set "mypath=F:\my_in_path"
Set "path_salida=F:\my_out_path"
Set "calc_params=-A "%%A" --outfile "%path_salida%\%%~nxA" --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet"
For %%A In ("%mypath%\*.tif") Do gdal_calc %calc_params%

Related

appending date with filename using batch scripts

I am trying to set a housekeeping for different type of file. PFB scenario.
I have below set of files in a network path(\NAS.domain.local\data\Arasan)
test.sql
test1.txt
test2.log
I am trying to rename the files as shown below and move the files to .\Archive\ directory
Test_15-12-2016_151428.sql
Test1_15-12-2016_151428.txt
Test_15-12-2016_151428.log
([Filename]_DD-MM-YYYY_HHMISS.[ext])
I have built the below batch script.
#echo ON
SET Log_Path=\\NAS.domain.local\data\Arasan
SET Script_Path=C:\Scripts
REM ###########################################REM
REM ## Do make any changes to the text below ##REM
REM ###########################################REM
cd %Script_Path%
pushd %Log_Path%
dir /b /a-d > %Script_Path%\list.txt
set HH=%TIME:~0,8%
for /F %%A in (%Script_Path%\list.txt) do (
setlocal EnableExtensions EnableDelayedExpansion
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
ren !ffname! !fname!_%DATE%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%!fext!
endlocal
)
del %Script_Path%\list.txt
move *.* Archive\
popd
exit
Now my issue is it is working perfectly in my machine but when I transfer this to server and execute. It is
throwing "The syntax of the command is incorrect." Error.
And it is moving the files without renaming. As for as i know, file name and extension is not being assigned to the variable properly in below part
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
Server OS: Windows server 2008 R2 Standard(SP1)
Can anyone please help me. Thanks a lot.
Here is the part of the code you should be using to get a consistent date and time output:
For /F "Skip=1" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "DTS=_%DTS:~6,2%-%DTS:~4,2%-%DTS:~,4%_%DTS:~-6%"
Echo( [%DTS%]
Pause
Just incorporate this into the rest of your codeā€¦

Reading a csv file with batch to create users

I'm an IT student. I was trying to make users in my virtual machine of windows server 2008, i though that should be a better way to make them less than make it one by one, then i found that i can make users with a database, in that case csv, and a loop script. But cause my low level of knowledge about commands and batch files i can't achieve my goal. This is what i have in the csv file:
Pedro,12345,939293c
Juanjo,23456,213123v
Eufrasio,34567,2131312b
Dani,45678,123213n
Pepe,56789,12344v
Manolo,67891,12312567b
And this is the batch I've made, I'm stuck in the part of the echo, the command didn't read
SET LOCAL
#echo incio del script
set n1=
set n2=
set n3=
for /F "tokens=1,2,3* delims=," %%i in (Libro2.csv) do (
set "n1=%%i"
set "n2=%%j"
set "n3=%%k"
echo %n1 %n2 %n3
)
pause
Variables in batch are referenced with %var%, not %var.
But in blocks (between ( and ) you need to enable delayed expansion to show the current value (if you change them inside the block):
setlocal enabledelayedexpansion
set x=one
if 1==1 (
set x=two
echo %x% !x!
)
change your script a little bit:
SETLOCAL enabledelayedexpansion
#echo incio del script
set n1=
set n2=
set n3=
for /F "tokens=1,2,3 delims=," %%i in (Libro2.csv) do (
set "n1=%%i"
set "n2=%%j"
set "n3=%%k"
echo %%i %%j %%k
echo !n1! !%n2! !%n3!
)
(Note: you can directly work with the for-variables - see the first echo in the block)
(Note: tokens=1,2,3,* means: for the fourth token take the rest of the line. You don't really need it here, as you don't use %%l)

How can I run an executable on files whose name begin with a specific phrase? (Windows 7)

I'd like to run pdflatex.exe on multiple .tex files, all beginning with a certain phrase I'd like to get a prompt for before the process starts.
How can I do that?
The files I want to process are named like this:
Chap01S01SS01_Description_Name_Whatever.tex
So all in all their names all begin with:
Chap01
Chap02
and so forth...
All the names end on .tex.
Obviously the numbers vary for the chapters and sections. This is why I'd like to make it easy for myself with the prompt: if I'd like to just run the files for section 2 in chapter 3, I run those, at some point I'd like to re-generate all the files for chapter 4 and 5... and so forth.
SOLUTION
#ECHO OFF
SETLOCAL
SET "sourcedir=D:\files\"
SET /p mask="Which .tex-files?"
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims=" %%a IN (
'dir /b /a-d "%mask%*.tex" '
) DO (
pdflatex.exe -interaction=nonstopmode --shell-escape "%%a" any other parameters perhaps containing "%%~na"
)
POPD
GOTO :EOF
Full credit goes to Magoo, I just added the parameters -interaction=nonstopmode --shell-escape for the use with pdflatex. If you know what "externalization" stands for in regards to TikZ/pgfplots, use it, otherwise delete --shell-escape.
BONUS Just copy the file and write lualatex.exe instead of pdflatex.exe for huge graphics. Do note your preamble must be organized accordingly though.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET /p mask="select which *.tex files ? "
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims=" %%a IN (
'dir /b /a-d "%mask%*.tex" '
) DO (
ECHO(pdflatex.exe "%%a" any other parameters perhaps containing "%%~na"
)
POPD
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances, or even use a similar set /p scheme to input the required directory, if you wish.
Since you haven't stated what the pdflatex command line looks like, I can only show pointers.
The required pdflatex commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(pdflatex to pdflatex to actually process the files.
Assuming you have a wildcard specification that matches all the files you want, and only those files, processing them is trivial with something like
FOR %%f IN (Chap01*) DO ECHO Processing file: %%f
So the problem reduces creating a wildcard specification after prompting the user. You can accept user input interactively with SET /P, for example:
SET chapter=
SET /P chapter=Chapter to process (ENTER for everything)?
REM now %chapter% contains the input, or blank if none was given
Putting it all together to get a proof of concept:
#ECHO OFF
SETLOCAL
SET chapter=
SET section=
SET /P chapter=Chapter to process (ENTER for all)?
SET /P section=Section to process (ENTER for all)?
REM We 'll be left-padding the values to two digits with zeroes
SET chapter_spec=??
SET chapter=00%chapter%
SET section=00%section%
IF NOT "%chapter%"=="00" SET chapter_spec=%chapter:~-2%
IF NOT "%section%"=="00" SET section_spec=S%section:~-2%
SET wildcard=Chap%chapter_spec%%section_spec%*.tex
ECHO Files to be processed:
FOR %%f IN (%wildcard%) DO ECHO %%f

Batch file to create multiple variables from single lines in a text file

I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)

How do I exclude specific file names from an MS DOS dir list?

I am creating an MS DOS batch script that needs to list every .bat file in the current directory, but not show autoexec.bat or other utilities or systems .bat files that shouldn't be run by the user.
I currently have DIR "*.bat" /B /P
This lists all .bat files appropriately, but it shows autoexec.bat. How would I exclude that from the list? Also slightly important, how could I chop off the file extensions and show more than the 7-characters DOS limits files to?
Constraints: I am not able to use a DOS version above WinME. That is the version I am using.
Thanks for any help.
EDIT:
There is plenty of information on the internet about doing this, but it is all in the windows command processor, not MS DOS. Please understand that DOS and the Command Prompt are not the same thing.
#echo off
setlocal EnableDelayedExpansion
rem Add more names separated with slashes here:
set exclude=/autoexec/
for %%a in (*.bat) do (
if "!exclude:/%%~Na/=!" equ "%exclude%" (
echo %%~Na
)
)
EDIT: Some explanations added
Batch file processing is slow, so you should use techniques that allows a Batch file to run faster. For example:
Try to use the minimum lines/commands to achieve a certain result. Try to avoid external commands (*.exe files) like find, findstr, fc, etc. specially if they work on small amounts of data; use if command instead.
Use for %%a in (*.bat)... instead of for /F %%a in ('dir /B *.bat').... The second method requires to execute cmd.exe and store its output in a file before for command can process its lines.
Avoid pipes and use redirections instead. A pipe require the execution of two copies of cmd.exe to process the command at each side of the pipe.
A simple way to check if a variable contain a given string is trying to delete the string from the variable: if the result is different then the string exists in the variable: if "!variable:%string%=!" neq "%variable%" echo The string is in the variable.
Previous method may also be used to check if a variable have anyone of a list of values: set list=one two three, if "!list:%variable%=!" neq "%list%" echo The variable have one value from the list. If the values of the list may have spaces, they must be separated by another delimiter.
EDIT: New version added as answer to new comments
The easiest way to pause one page at a time is to use more filter this way:
theBatchFile | more
However, the program must reorder the output in order to show it in columns. The new version below achieve both things, so it does not require more filter; you just need to set the desired number of columns and rows per page.
#echo off
setlocal EnableDelayedExpansion
rem Add more names separated with slashes here:
set exclude=/autoexec/
rem Set the first two next variables as desired:
set /A columns=5, rows=41, wide=(80-columns)/columns, col=0, row=0
rem Create filling spaces to align columns
set spaces=
for /L %%a in (1,1,%wide%) do set spaces= !spaces!
set line=
for %%a in (*.bat) do (
if "!exclude:/%%~Na/=!" equ "%exclude%" (
rem If this column is less than the limit...
set /A col+=1
if !col! lss %columns% (
rem ... add it to current line
set name=%%~Na%spaces%
set "line=!line!!name:~0,%wide%! "
) else (
rem ... show current line and reset it
set name=%%~Na
echo !line!!name:~0,%wide%!
set line=
set /a col=0, row+=1
rem If this row is equal to the limit...
if !row! equ %rows% (
rem ...do a pause and reset row
pause
set row=0
)
)
)
)
rem Show last line, if any
if defined line echo %line%
Antonio
attrib +h autoexec.bat
should hide autoexec.bat and it should thus not appear in the list
DIR "*.bat" /B /P | find /v "autoexec" | for %i in (*.bat) do #echo %~ni
Using for to process each file name individually:
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.bat" /b') do (
set system=0
if "%%i"=="autoexec.bat" set system=1
if "%%i"=="somesystem.bat" set system=1
if !system!==0 echo %%i
)
Another method without variables:
for /f %%i in ('dir "*.bat" /b') do call :test %%i
goto continue
:test
if "%1"=="autoexec.bat" goto :eof
if "%1"=="somesystem.bat" goto :eof
echo %1
goto :eof
:continue
For both, you can add new filenames to exclude from the list.

Resources