There is a list of text files that needs to be processed. Their names are
samples\file_1.txt
...
samples\file_100.txt
A script calls file test.exe with 2 parameters. The first and second parameters represent input and output file names, which change with the increment 1, and the parameter -t is fixed:
test.exe \input\file_1.txt \output\file_1.txt -t
...
test.exe \input\file_100.txt \output\file_100.txt -t
How to write a simplified version of the script, processing files one by one, using the for cycle?
I solved this problem using the Python script, but hope, there is a more common way...
#echo off
cd /d "c:\samples\input"
md "..\output" 2>nul
for %%a in (*.txt) do (
test.exe "%%a" "..\output\%%a" -t
)
Related
This question already has an answer here:
Windows cmd pass output of one command as parameter to another
(1 answer)
Closed last year.
I have this long path of the temporary folder of a project:
c:\\Users\\user\\source\\repos\\src\\etc\\even\\longer
I stored the string of text of the path in the file "path.txt" for the time being.
I would like to change directory from the windows command line like I would in Bash
$ cd `cat path.txt`
I tried with cd < type path.txt but I got.
The system cannot find the file specified.
You try to redirect the contents of a file named type to the cd command. The correct syntax would be cd < path.txt or type path.txt | cd - if cd would take input from STDIN, which it does not.
You have to read the content of path.txt to a variable and use cd with that variable.
There are two methods to read from a file: set /p and a for /f loop.
A
<path.txt set /p folder=
cd /d "%folder%"
or B
for /f "usebackq delims=" %%a in ("path.txt") do set folder=%%a
cd /d "%folder%"
or (directly processing) C
for /f "usebackq delims=" %%a in ("path.txt") do cd /d "%%a"
(All methods assuming there is just one line in the file - method A reads the first line only, method B reads all (non-empty) lines, keeping the last one in the variable and method C would cd in every folder listed in the file.)
I am new to using command prompt. I have a large amount of zip files I need to run a program on.
Running the program on one of the files in the command prompt looks like this:
Tabulate.exe -i S:\Packages\ZipFolderName_1.zip -o S:\Output\ZipFolderName_1
which spits out a csv.
I have found these posts helpful, but cannot seem to implement for my situation:
Iterate all files in a directory using a 'for' loop
Loop on files and run command
Read entire for /? output, pay your attention to ~ modifiers. In the following cmd compound command is used ECHO to merely show Tabulate.exe lines to be executed:
for %I in ("S:\Packages\*.zip") do #ECHO Tabulate.exe -i %~fI -o S:\Output\%~nI
To use the FOR command in a batch program, specify %%variable
instead of %variable. Variable names are case sensitive, so %i is
different from %I.
#ECHO OFF
SETLOCAL EnableExtensions
for %%I in ("S:\Packages\*.zip") do (
rem explaining comments:
rem %~fI expands %I to a fully qualified path name
rem %~nI expands %I to a file name only ↓↓↓↓
ECHO Tabulate.exe -i %%~fI -o S:\Output\%%~nI
rem ↑↑↑↑ remove `ECHO` no sooner than debugged
)
My problem is that two FOR loops are working separately, but don't want to work one after another.
The goal is:
The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.
#echo off
Setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
echo Verarbeite %%~na
jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)
for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)
pause
This question can be answered easily when knowing what jhove is.
So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.
All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.
So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.
But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.
For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.
This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0
Test2.bat:
#echo %~n0: Arguments are: %*
#echo %~n0: Leaving %~f0
On running from within a command prompt window C:\Temp\Test1.bat the output is:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.
Now Test1.bat is modified to by inserting command CALL after do.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0
The output on running Test1.bat from within command prompt window is now:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat
Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.
Therefore the solution is using command CALL as suggested already by Squashman:
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
echo Verarbeite %%~na
call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)
for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)
pause
endlocal
A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.
All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.
And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
dir /?
echo /?
for /?
And read also the Microsoft article Using command redirection operators.
You need to use the START command with the /WAIT flag when you launch an external application.
I believe it would look something like this:
START /WAIT jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
That should cause the batch file to pause and wait for the external application to finish before proceeding.
I'm trying to convert a folder of SWFs to images using swftools.
The actual conversion is working fine, however I'm having issues trying to get it to run for all files in a folder. (and would be nice to get it to work for all subfolders as well).
Let's say the folder contains the following files: 1.swf, 2.swf, ...
Now the script I wrote for this is:
for %i in (*.swf) do (
set fileName=%i
swfrender "%fileName%" -o "%fileName:~0,-4%.png"
)
Which I'm running from inside the folder containing the SWF files. However rather than the expected result I'm seeing the following in my command prompt:
set fileName=1.swf swfrender "3.swf" -o "3.png" )
set fileName=2.swf swfrender "3.swf" -o "3.png" )
Now from what I could find the script should be fine, so I have no clue what's going wrong.
Oddly enough the following does seem to work:
for /R %x in (*.swf) do swfrender %x -o %x.png
But I'd rather not have to rename a couple of hundred files to remove the redundant .swf portion from the filename.
you need delayed expansion (and in batch files you need doubled % for the loops tokens):
setlocal enableDelayedExpansion
for %%i in (*.swf) do (
set fileName=%%i
swfrender "!fileName!" -o "!fileName:~0,-4!.png"
)
or you can extract the file name without extension:
for %%i in (*.swf) do (
swfrender "%%~i" -o "%%~ni.png"
)
I am trying to process several files by running them through a batch file. I want the batch file to be able to take all the files its given (aka dumped; or dragged and dropped) and process them.
Currently I can process the files individually with the following batch command:
"C:\Program Files\Wireshark\tshark.exe" -r %1 -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> %1".filter.txt"
I am looking to do the same thing as above, but with the ability to simply drag and drop the files onto the batch file to be processed.
For those confused about the above batch file:
-r Reads the input file, whose full file address (including extension) is captured by %1
-Y Filters out certain parts of the dragged & dropped file
-o Sets preferences (defined by stuff in the ""s) for running the executable: tshark.exe
- > redirects the results to stdout
- %1".filter.txt" outputs the results to a new file called "draggedfilename.filter.txt"
Please refrain from using this code anywhere else except helping me with this code (due to the application it is being used for). I changed several flags in this version of the code for privacy sake.
Let me know if you have any questions!
Use %* instead of %1.
Example :
#echo off
for %%a in (%*) do (
"C:\Program Files\Wireshark\tshark.exe" -r "%%a" -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> "%%a"".filter.txt"
)
Replace %%i with the right variable.
You could go for a loop using goto and shift like this (see rem comments for details):
:LOOP
rem check first argument whether it is empty and quit loop in case;
rem `%1` is the argument as is; `%~1` removes surrounding quotes;
rem `"%~1"` therefore ensures that the argument is always enclosed within quotes:
if "%~1"=="" goto :END
rem the argument is passed over to the command to execute (`"%~1"`):
"C:\Program Files\Wireshark\tshark.exe" -r "%~1" -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> "%~1.filter.txt"
rem `shift` makes the second argument (`%2`) to be the first (`%1`), the third (`%3`) to be the second (`%2`),...:
shift
rem go back to top:
goto :LOOP
:END