Need a hand in Windows batch processing syntax - loops

I have compiled a C++ program on Windows, and I need it to process a huge number of my data files. The files are named, for instance, "x0000y" to "x9999y".
The C++ program only takes one file at a time, create the output of each respective file, save somewhere, and terminate. I do not want to hard code the program, as my data set does not always have the same number of files - and keep recompiling the program just for this is not cool. So I am looking for a quick way of doing this: batch processing.
Here comes the trouble: I am having trouble trying to get the batch syntax correct and valid. So could somebody show me the following pseudocode in batch processing version?:
for (int i = 0; i < lastFile; i++){
String filename;
/*
Because the files are named "x0000y", "x0034y", etc.
We need to put in all the extra 0s in the string if i is less than 1000.
*/
String numberedString = convertNumToFourDigit(i);
filename = "myFileName" + numberedString + "Footer";
/*
execute the program with the respective filename.
*/
execute("MyProgram.exe " + filename);
}

This is all you'll need in your .bat file to run your program against all of the files in the current directory.
for %%I IN (*) DO ( MyProgram.exe %%I )
If the data files are in a subdirectory with an extension, here's an example.
for %%I IN (subdir\*.dat) DO ( MyProgram.exe %%I )
If you only want files in the form of x0000y, then this will do the trick.
for %%I IN (x????y) DO ( MyProgram.exe %%I )
These will process the data files in whatever order the filesystem provides their names.

Related

need to copy some contient from one txt file to anther txt file by using batch file

Hi I am new to writing batch files so please help me.
I have a txt file with data as below:
\nwe data
test data and
othere files
resrt
/* 20170804 */
test data
new line and work
only work
new test
online
master
/*letest*/
I want to copy the data from /* 20170804 */ to /*letest*/. How do I get that data copied to another txt file?
First, create the logic:
for every line in the text file do
If the line is the start string, set a flag.
If flag is set, print the line.
If the string is the stop string, delete the flag.
continue with next line.
then redirect all output of the whole loop to a file.
#echo off
setlocal
set "flag="
(for /f "delims=" %%a in (t.txt) do (
if "%%a"== "/* 20170804 */" set "flag=yes"
if defined flag echo %%a
if "%%a"== "/*letest*/" set "flag="
))>out.txt
Note: this works with something like your example. If there are special chars or you want to keep empty lines, more code is necessary.
Have a look at command line tools like gawk.exe or sed.exe that use regular expressions to search patterns and work on the files.
For example gawk using flag to print the lines.
gawk "/\/\* 20170804 \*\//{flag=1;next}/\/\*letest\*\//{flag=0}flag" C:\temp\data.txt > C:\temp\out.txt

How to create many copies of a single file with different names using command prompt?

I need to create new copies of a single .html file. The file's name is 065.html.
I copied the file into 066.html using the following command:
C:\>copy 065.html 066.html
But I need more. I need to copy up to 090.html
How can I do this? A loop should do this but I don't know the syntax.
I have tried to use the command as shown here,
for /L %%i IN (66,1,90) do copy 065.html 0%%i.html
but I get an error message that says
%%i was unexpected at this time
I have deleted one preceding % signs. It works now.
for /L %i IN (66,1,90) do copy 065.html 0%i.html

How can I run same exe file consecutively N times?

I have an exe file. I want to run it several times repeatedly one after another. I tried below batch file but couldnt do it is there a way to do it? Sorry but i am a rookie in code writing
#echo off
start ran.exe
start ran.exe
start ran.exe
Assuming that ran.exe is in the current folder or on your path, then you simply write:
#echo off
ran
ran
ran
to invoke it three times. If it is not found on the PATH, then use a fully qualified name like this:
#echo off
c:\path\to\ran
c:\path\to\ran
c:\path\to\ran
Running a program is the normal effect of naming it on a line of a batch file.
Furthermore, because .EXE is listed in the PATHEXT environment variable, you don't need to include that in the name, unless there is also a file name ran.com since .COM is listed in the default value of PATHEXT ahead of .EXE.
The START builtin command is only needed in batch files for handling some special cases. See the output of START /?1 for its documentation. In general, you don't need it just to launch programs.
Update: To generalize this to N invocations, use the FOR command. FOR is extremely powerful, type FOR /? at the command prompt for documentation. For N repeats specified as the argument to the batch file, and passing the current count to the command as its first argument do this:
#echo off
for /L %%N in (1,1,%1) do c:\path\to\ran %%N
The tricky thing to remember with FOR is that the iteration variable must be named with two percent signs in batch files. The help text says that, but only in passing.
Update 2: Some more details and explanation.
In this case, we want to repeat a command N times. FOR supports a variety of kinds of loops, but the easiest way to get exactly N iterations is to use its /L option which uses a starting value, a step size, and an ending value to define the number of iterations.
These are specified in parenthesis, as FOR /L %%N (start,step,end). To get a simple counter of 1 to N, we tell it to start at 1, step by 1, and to stop at the value of the first argument to the batch file which is named %1.
The arguments to the batch file itself are named as %1 through %9, and %* names all of the arguments. Note that there is a vast minefield of subtlety here related to properly quoting file names that contain spaces. To keep life simple, try very hard not to need to do that. Otherwise, CALL /? documents the command line argument conventions, and SET /? documents many things related to general batch file variables.
Other forms of FOR let you iterate over files (no option), directories (/D), directories recursively in a tree (/R), or various parts of the contents of files (/F).

Can't pass file path to a Batch file

I'm new to programming so apologies in advance if this is really simple.
I'm using PA File Sight to monitor a folder for open files. When a file is opened it starts a program (batch file in this case) and passes the entire file path to the variable: $Item(1)$
The batch file looks like this at the moment:
set FILE_PATH="$Item(1)$"
echo.>%FILE_PATH%_IS_OPEN
I'm trying to get the batch file to create a new file with IS_OPEN on the end of it so that users know that a file "is open"
Running the batch file creates the following in it's folder:
$Item(1)$_IS_OPEN
So it's not storing the path for some reason.
I'd suggest you try
set FILE_PATH="%~1"
echo. "%FILE_PATH%_IS_OPEN"
echo.>"%FILE_PATH%_IS_OPEN"
PAUSE
This should set FILE_PATH to the first parameter that the batch file sees - the ~ removes any enclosing quotes
The next line echoes the result to the console and may be removed if the test proves successful.
The third line encloses the proposed filename in quotes to allow the use of spaces in the filename generated.
The PAUSE holds the CMD window open until you press ENTER to allow you to see the results. It too can be removed if your results are as expected.
modified to replace the first 2 characters of the NAME portion with "AA"
set FILE_PATH="%~1"
FOR /f "delims=" %%i IN ("%file_path%") DO (SET dpi=%%~dpi&SET ni=%%~ni&SET xi=%%~xi)
SET file_path=%dpi%AA%ni:~2%%xi%
echo. "%FILE_PATH%_IS_OPEN"
echo.>"%FILE_PATH%_IS_OPEN"
PAUSE
This assumes that it's the first 2 characters that need to be replaced. It works by assuming that the literal-string in the variable file_path is a filename whic, miracle of miracles, it is. dpi is then set to the drive and path, ni to the name and xi to the extension. then the full name is reconstructed, substituting AA for the first 2 characters of the name (dpi (the path) + AA + ni:2 (the name from the second character to the end) + xi (the extension))
$Item(1)$ seems like a constant string. If $Item(1)$ is the name of an actual environment variable, your first line should be
set FILE_PATH="%$Item(1)$%"
Although that seems quite an odd name for a variable.

Batch or VBS | How to check the bitrate of a music file?

I need to check the bitrate of a music file, I need to receive the number in digits like: 192000 (for 192 kbps), 320000 (for 32kbps) or (+)3000000 for wavs and uncompressed music. I mean I need exactly the number, If an MP3 is VBR and is compressed at 194 kbps, I need the 194000 number, not the current CBR 192000.
I was do this job with MEDIAINFO (x64) CLI Program, In Batch:
for /f "tokens=*" %%%% in ('mediainfo "%%a" "--Inform=General;%%BitRate/String%%"') do set "BitRate=%%~%%"
But I have 35.000+ files to check and then the comprobation of all files is more than 2 hours of time.
I need a simple code to check it, Not a program which need to execute it and to waste that lot of time...
Is very important that the code needs to recognize at least this filetypes (I mean the internal bitrate):
AIFF, FLAC, M4A, MP3, OGG, WAV, WMA.
And can't be a code for Ruby or Python, because I'll need to "compile" it and sure when is "compiled" waste a lot of time to check much files (Cause the uncompression of the .exe compiled).
More info: I thinked about store the results in a file and then do a comparision to chek only new added files, But I can't store the result to do a comparision at the next run cause sometimes I'll need to replace checked files (old files). By the way neither I can't handle this by file datestamps. Need to be one unique procediment to check ALL the files, Ever (Or this is what I think...).
I tried another method to check the bitrates, I'm really sure this is what I need but I can't get it run like I want...
This VBS uses the DBPowerAmp program API, And shows a window with info (included the bitrate), But with a window I can't do nothing... Maybe if I can redirect the windows info to a text file... And then set the variable "Bitrate" by reading the bitrate info in the text file... But I don't know how to do that:
' create shell object
Set WshShell = CreateObject("WScript.Shell")
' Create dMC Object
Set dMC = CreateObject("dMCScripting.Converter")
'Read audio properties of a file
Dim AudioProps
AudioProps = dMC.AudioProperties("C:\test.aac")
Call WshShell.Popup(AudioProps, , "Returned Audio Properties", 0)
I've tried to "convert" that code into Batch, like this, But don't run, I get nothing:
#echo off
rundll32.exe dMCScripting.Converter.AudioProperties("C:\Test.aac") > test.txt
exit
Oh and I've tried this too, but waste more time than mediainfo:
mplayer "test.aac" -frames 0 | findstr "kbit"
To give you an idea of what it is like in Ruby, audioinfo is just one of the many libraries doing such things.
require "audioinfo"
AudioInfo.open("R:/mp3/j/John Coltrane - I Think.mp3") do |info|
puts info.to_h
end
=>{"artist"=>"John Coltrane", "album"=>"John Coltrane", "title"=>"I Think", "tracknum"=>nil, "date"=>nil, "length"=>272, "bitrate"=>128}
Here a vbs script, works with mp3, the rest i didn't try
Set objPlayer = CreateObject("WMPlayer.OCX" )
Set colMediaCollection = objPlayer.mediaCollection
Set colMedia = colMediaCollection.getAll()
For i = 0 to colMedia.Count - 1
Set objItem = colMedia.Item(i)
Wscript.Echo objItem.Name & " : " & objItem.GetItemInfo("bitrate")
Next
See http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP+Get~Audio~File~Information.txt for a list of attributes you can use.

Resources