How to do a for loop in windows command line? - windows

I was wondering if this was possible? I'm not familiar with using windows command line, but I have to use it for a project I'm working on. I have a a number of files, for which I need to perform a function for each. I'm used to working with python, but obviously this is a bit different, so I was hoping for some help.
Basically I need the for loop to iterate through 17 files in a folder, perform a function on each (that's using the specific software I have here for the project) and then that will output a file with a unique name (the function normally requires me to state the output file name) I would suck it up and just do it by hand for each of the 17, but basically it's creating a database of a file, and then comparing it to each of the 17. It needs to be iterated through several hundred times though. Using a for loop could save me days of work.
Suggestions?

The commandline interpreter does indeed have a FOR construct that you can use from the command prompt or from within a batch file.
For your purpose, you probably want something like:
FOR %i IN (*.ext) DO my-function %i
Which will result in the name of each file with extension *.ext in the current directory being passed to my-function (which could, for example, be another .bat file).
The (*.ext) part is the "filespec", and is pretty flexible with how you specify sets of files. For example, you could do:
FOR %i IN (C:\Some\Other\Dir\*.ext) DO my-function %i
To perform an operation in a different directory.
There are scores of options for the filespec and FOR in general. See
HELP FOR
from the command prompt for more information.

This may help you find what you're looking for...
Batch script loop
My answer is as follows:
#echo off
:start
set /a var+=1
if %var% EQU 100 goto end
:: Code you want to run goes here
goto start
:end
echo var has reached %var%.
pause
exit
The first set of commands under the start label loops until a variable, %var% reaches 100. Once this happens it will notify you and allow you to exit. This code can be adapted to your needs by changing the 100 to 17 and putting your code or using a call command followed by the batch file's path (Shift+Right Click on file and select "Copy as Path") where the comment is placed.

You might also consider adding ".
For example for %i in (*.wav) do opusenc "%~ni.wav" "%~ni.opus" is very good idea.

Related

Batchfile: Using "start" Command to Start a batch file on a specific label

Inside this batch files, exist over 25 lables, each for different purposes..
Specifically the label called ":beep" which make a beep noise in the computer.
I wanted to scheduling the start of this batch file, but only the label "beep",
or maybe another batch, but only starting with the label "beep".
I have already used the "call command", which is not what I need.
Is there anybody who knows how to use "Start" command to run a batch file on a specific label?
Example:
start C:\interface.cmd [goto beep]
OK, so if there are no other parameters, just pass beep as a parameter e.g. batchfile beep and have a line goto %1
If there are other parameters you need to pass, time to get more creative. Have the start of your bat file, something like:
#echo off
setlocal
set p1=%1
if x%p1:~0,1% equ x: (
shift
goto %p1%
)
Then you can run: batchfile :beep "as many" other "params as you like"
Maybe i'm just being too lazy...
i will just copy the specific label and its command,
create new batch file..
paste it in there, save it,
and just run the new batch file from Shortcut, Hot Key, task schedule.. which will give me better results.. and just run the commands i needed
more work, but less stress
LOLL.....

Redirecting a command to an .exe causing a repeat in the command

I have an exe program that uses a command line GUI and would like to set up a batch script to run a few commands automatically, so here are the sort of commands that the GUI uses:
? = help
s = start
l = status
r = reset
x = exit
I would like to create a batch file that automatically runs the start command I have tried a few things to no avail such as the below.
using a cannedreponsesfile as the input:
#echo off
START /b <path>\service.exe < <path>\cannedreponses.txt
And I receive the following error:
The system cannot find the path specified.
The path referenced in the batch script seems to be ok I checked with dir /s /b
I have also attempted this command
echo s |START /b service.exe
This seems to have better results, but it seems to keep repeating input.
Any idea what is going on, or how I can do this in a batch file.
Thanks in advance
You appear to have overcensored your question.
In all probability, the problem is with <path>
If the real string you are using contains separators (especially spaces) or some other special characters, then you need "enclose the string in quotes".
There's a quirk with start however - the very first string quoted encountred is interpreted as a "window title" so you may need to use
start /b "" "whatever 1" "whatever 2"
The start is unnecessary here and only introduces a source for errors. Just say
Echo s|service.exe

Windows batch file, save contents of file dowloaded by wget to a variable

Currently, I have a batch file that uses wget to read a file from the server. Is there any way for wget to save the contents of that file to a variable and then for the batch file to take a certain action based on the value of the variable?
The peseduo-code would probably look something like this. I am very new to batch files and am still learning the semantics:
SAVE RESULT OF wget http://www.theserver.com/instruction TO VARIABLE: the_variable
IF %the_variable% == 'restart' <DO SOME ACTION HERE>
I will base this answer on the assumption that your downloaded file contains text strings.
If this is the case then it is possibile to use the FOR command in this way:
for /F %I IN (instruction.txt) DO if %I==restart #echo RESTART FOUND
This command opens the file "instruction.txt" and parse it assigning each word to the variable %I
Then for each value of variable %I executes the command specified after the keyword DO.
In this case I have compared the variabile %I to the string "restart" and if the result is true the batch execs the command #echo RESTART FOUND
You could use the GOTO: function in your batch file. So that if the variable is equal to a certain value/string it jumps to a particular section in the batch file and carries out the code in that section.
Almost like using methids in Object Orientated Programming.
CHeck this link out :-
http://www.robvanderwoude.com/goto.php

Windows Batch Script: For ... in loop to capture program output not working

I have a batch script setup to automatically retrieve a file from a remote FTP server. Part of the requirement is the file will be named with a new datestamp each day, such as "File_90611.csv." I have a command line tool that generates the filename; which is then supposed to be set to a variable using the line below:
for /f "delims=" %a in ('C:\BIN\YesterdayDateStamp.exe') do #set DATESTAMP=%a
The problem is this. This line works fine when run from the command line directly. However, when I put this exact same line in a batch script and run it; I get this error:
\BIN\YesterdayDateStamp.exe') was unexpected at this time.
I REM'ed everything out in the script except the FOR ... IN commands to make sure there wasn't some sort of conflict; but even with this I still get an error.
Been Googling for an answer but have no leads. Any ideas? Any constructive input is greatly appreciated.
Thanks,
Frank
When for is used in a batch file, you need to double the percent signs in front of the variable name.
From for /?:
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.

Batch process all files in directory

Right now i have a batch job I wrote that calls another file and passes in the variables that executable needs to run (password and filename).
Ex:
> cd f:\test\utils
> admin import-xml -Dimport.file=f:\DB\file1.xml -Dadmin.db.password=test123
I wrote a job that does this, but found out that there would be multiple files.
The username and password never change but the filename differs for like 15 different xml files--with maybe more coming soon.
The files will always be located in the same folder. Instead of ending up with like 15-20 jobs (one for each file), can I write something that will process each file located in this directory. And either wait till one is completed before the next or I can add a 3 min sleep before it starts the next file.
pushd C:\test\utils
for %%F in (F:\DB\*.xml) do (
admin import-xml "-Dimport.file=%%~dpnxF" -Dadmin.db.password=test123
)
popd
The %%~dpnxF expands to d‎rive, p‎ath, base‎n‎ame and e‎x‎tension of the current file.
If you intend to set and use environment variables (%foo%) in that loop, read help set first before you get into trouble.
You can use the for command. Something like this in a batch file:
for %%f in (*.xml) do call myotherbatch.bat %%f
Assuming that the admin command you are running doesn't return until the job is finished, the above loop would process them sequentially.
If you run the command at the prompt (as opposed to in a batch file), only use a single %.
for file in f:\DB\*
do
admin import-xml -Dimport.file="$file" -Dadmin.db.password=test123
done

Resources