Running an .exe file through a batch file and passing parameters - batch-file

I have an .exe file that takes two parameters when i run it from the command line, as such:
test_app.exe -vid.avi -data.txt
How would i be able to START the .exe file through a batch script and pass it those parameters?
If i have multiple .avi and .txt files that i need to pass to the .exe file through START, how would i be able to have a variable that goes through all of those files two at a time? (pairing every .avi with it's correspondant .txt).
Let's assume that every pair of .avi and .txt share the same name but obviously have different extensions.
I need to write something like this:
#ECHO OFF
START test_app.exe -vid.avi -data.txt
pause
But the parameters should be variables that increment every time a pair of parameters are proccessed through the .exe so it would loop on all of the files in the CWD.
Trying to do this but seems like START does not work that way?
#echo off
for %%a in (*.avi) do (
START Tester.exe -%%a -%%~na.txt
)
pause

try this, it works with AVI as the main extension, you may change this:
#echo off &setlocal enabledelayedexpansion
for %%i in (*.avi) do (
set "line="
for %%j in ("%%~ni.*") do set line=!line! -"%%~j"
start "" test_app.exe !line!
)

Try this with your avi files. It will just echo the bunch of commands and you can see what it does. The - signs seem a bit odd but I included them with the names.
#echo off
for %%a in (*.avi) do (
echo exe.file "-%%a" "-%%~na.txt"
)
pause

Related

Copy list of files from directory+subfolders to another folder

First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.
Example:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so
what I basically want is: \Tabletop Simulator\Mods\ as source path and
the script shall look into all subfolders, too.
The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
#echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause
#echo off
setlocal
set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
set "file_list=%USERPROFILE%\Desktop\list.txt"
set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
if not exist "%dst_folder%" md "%dst_folder%"
for /d /r "%src_folder%\" %%A in (*) do (
pushd "%%~A" && (
for /f "usebackq delims=" %%B in ("%file_list%") do (
for %%C in (%ext_list%) do (
if exist "%%~B%%~C" (
echo copy /y "%%~B%%~C" "%dst_folder%\"
)
)
)
popd
)
)
You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.
A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.
The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.
If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.
To view help for a command, use command /?. This will work for many of commands used in the code.
View command /? help for copy, for, if, setlocal ...

Copy a random file to another folder

I'm trying to create a batch file to copy a random file from one folder to another folder . but when I run the code below it copies the same file every time . does anyone know what's wrong with the code ?
I have different types of files in the folder .jpg .txt .rtf I don't know if that is why it is not working.
#echo off
setlocal EnableDelayedExpansion
cd c:\users\paul\desktop\11\
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" c:\users\paul\desktop\12
Are you running this Batch file via a double-click from the explorer? When cmd.exe start execution it initialize the random seed with the current time, so if you execute the Batch file several times in a short lapse, and the number of different files to choose is small (like 50), the same file will be selected every time.
You may "randomize" the random number in several ways; this is one of the simplest methods:
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
Insert this line before the line that uses %random%.

Trouble converting multiple PDF files to JPEG files using Ghostscript from a batch file

I have several (thousand) multipage PDF files that I am trying to convert to JPEG files (one file for each page of each PDF) with Ghostscript. I have the following batch file, but I can't get it to work properly.
for %%f in (*.pdf) do (
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg %%f
)
The loop seems to work okay in the sense that it is opening the files. It is going wrong at the "p%03d.jpg" -- I'm guessing that the loop sees the "%0" and for some reason inserts the name of the batch file. I get the following error:
**** Could not open the file pC:\Users\bmjones\Downloads\convert.bat3d.jpg
("convert.bat" is the name of the batch file)
I'm guessing that even if I got this to work, I would have another problem. When I manually run:
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg [file name].pdf
It works fine. I get a set of files names 001.jpg, 002.jpg, etc. But I would like them to be numbered differently. So, if my first two files have 3 and 2 pages respectively, I would like to have out something like this:
f1_pg1.jpg, f1_pg2.jpg, f1_pg3.jpg, f2_pg1.jpg, f2_pg2.jpg
Instead of using a counter for unique filenames, you could set the name of the output files based on the names of the PDFs.
#echo off
setlocal
for %%I in (*.pdf) do (
gswin64 -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile="%%~nI_p%%02d.jpg" "%%~I"
)
%%~nI gets the base name without extension of each PDF. The "%%~I" just makes sure filenames containing spaces don't break things. Enter help for in a console window and see the last two pages for more information about this syntax.
Alright, with a little help from #rojo, I think I've got this (I'm certainly open to better solutions)
set /a c=0
setlocal ENABLEDELAYEDEXPANSION
for %%f in (*.pdf) do (
set /a c=c+1
set FNAME=f_!c!_p%%02d.jpg
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=!FNAME! %%f
)

Batch read and rename from text file issue?

I'm a beginner with .bat files, and I'm attempting to rename multiple drawing (.dwg) files using a list generated in notepad. The notepad list contains a list of drawing numbers that look like this:
01013-13000p001
06301-12550p001
etc..
There is hundreds of them, and I want to take those numbers from the text and put it into a blank series of dwg files that are generic named for now (drawing.dwg, drawing(2).dwg, drawing(3).dwg etc..) I've only come up with a way to read a text file, but cant figure out how to take from the text file and rename multiple drawing files with it. Below is as far as I have gotten, after failed attempts of trying to take whats read from a text file and put it into the .dwg files. I plan on working this out in all the same directory, and any suggestions will be greatly appreciated. Thanks.
#echo off
for /f "tokens=* delims=" %%x in (dwgNumbers.txt.txt) do echo %%x
pause
would
for /f "delims=" %%x in (dwgNumbers.txt.txt) do echo copy /b "blank generic drawing.dwg" "%%x.dwg"
(as a batch line) do what you want? - note that the ECHO keyword is there to show what would be done. The echo keyword needs to be removed to actually execute the copy.
This will take the numbers from .txt, renaming the existing .dwg files with the data readed. If there are more files that numbers in .txt, it will rename until number exhaustion, no more.
for loop is using a dir command to get the list of files to avoid the case of files that after being renamed falls under the filter of the for and gets reprocessed.
This code has a echo command included in rename line to prevent data loss in case of malfunction. When the output to console is what is needed, remove the echo command from the rename line.
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem Read input file for numbers
< dwgNumbers.txt (
rem Process dwg files in directory
for /f "tokens=*" %%f in ('dir /b *.dwg') do (
rem Get number from input file
set "number="
set /p "number="
rem If there is a number, rename the file
if defined number echo ren "%%~f" "!number!.dwg"
)
)
rem Clean
endlocal

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

Resources