(by DOS I mean windows cmd.exe - I don't want to enforce powershell or similar on the end user)
I want to run a command line file that prints output to CON / the screen.
I want to capture that output and compare it to an expected output.
... in a .bat / .cmd file?
Specifically, the identify command of ImageMagick, and I want to run this over +- 300 files and compare the actual sizes to expected sizes.
example output:
$ identify rose.jpg
rose.jpg JPEG 640x480 sRGB 87kb 0.050u 0:01
If I understand the question correctly, you want to run the identify command on all the jpg files in a directory and capture the output of that command into a text file for later comparison. The comparison however is not part of the spec?
Something like the line below should do that job. Just run it from the folder the jpg files are located:
for /R %%X in (*.jpg) do identify %%X >> PicInfo.txt
This will capture the rose.jpg JPEG ... line for every .jpg file you have in the directory (and subdirectories thanks to '/R') that you run the command in and append it to the file PicInfo.txt.
You can call your identify program with a symbol that redirects console output to a file, which is the > character. Something like:
identify rose.jpg > myoutput.txt
Additionally, the >> will append output to what is already in the file. So using
identify rose.jpg >> myoutput.txt
...should create one file with all your output.
You can then use the DOS COMP command, which compares the contents of two files. The syntax is:
COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]
Which you could also redirect to an output file using the > symbol.
Related
I found the following command line expression for exiftool here: Extract thumbnail from jpeg file
exiftool -a -b -W %d%f_%t%-c.%s -preview:all YourFileOrDirectory
The command works great on the command line, but when ran in the following batch program it seems the percent signs are misinterpreted and I'm not sure how to get it to be accept it. Can someone show me how to write this command for batch.
:: This exif command will create a file in the same directory as the one
:: where the original photo is located that contains all the thumbnail
:: images contained in the file's exif data. The exact path to the original
:: must be specified in this command.
cd\
exiftool -a -b -W %d%f_%t%-c.%s -preview:all c:\users\cher\pictures\one.jpg
pause
See exiftool FAQ 27
In a Windows .BAT file the "%" character is significant, so all "%" characters in ExifTool commands must be changed to "%%".
I have written one code for remove duplicates from CSV file & now i want to save file with its original name. I dont want to save as with different name. Below is the batch script :
Code :
#echo off
C:\sw\awk\bin\gawk.exe "!x[$0]++" *.csv > "{print FILENAME, $0 > FILENAME ".csv"}" file*
My Objective is : I want to build dynamic batch script which will run on any CSV file. There should not be any type of dependency(file name).
Error is :
The filename, directory name or volume label syntax is incorrect.
Please help me with this.
Thanks.
This batch file is trying to redirect output from gawk into a file called {print FILENAME, $0 > FILENAME, which is not a valid filename.
At present, the gawk code produces a single output that omits all duplicate lines in all of the CSV files in the directory.
If you want to omit duplicate lines from each CSV file individually and write each file out individually, you will need a loop in the batch file to present each CSV file to the gawk script individually and output each modified file individually. You can do that as follows (presuming tmp.tmp is not an existing file in the folder):
for %%f in (*.csv) do (
gawk.exe "!x[$0]++" "%%f" >tmp.tmp
copy tmp.tmp "%%f"
)
del tmp.tmp
As a cautionary note, on DOS or Windows if you write through standard output to a file that you are reading, you can overwrite the file that you are reading before you've finished reading it. That is why the code above writes to a temporary file and then copies the temporary file to the original file.
There are a number of problems with your attempt. Firstly, you cannot write to the same file that you're reading from (at least, not while you're reading from it). Secondly, you're using the awk special variable FILENAME outside of the awk script, where it doesn't exist.
The following may work for a single file at a time. It reads the entire file in, using the line as the key of an associative array and using the line number as the value. Then in the END block, it prints the array out in order of the values, writing to the file it just read.
gawk "!($0 in a) {a[$0] = NR} END {PROCINFO[\"sorted_in\"]=\"#val_num_asc\"; for(x in a) print x >FILENAME}"
Edit: Brief Summary
I have number files in a directory, called crash0, crash1 etc. I want to run a .bat file for each of this with a command line like:
abc.bat crash0 > crash0.txt
How can I make another .bat file that loops over all the crashXX files calls abc.bat once for each one of them?
Original Question
Please find my situation below..
I have some files (number may vary each time) in a folder with its name starting with crash. That is crash0, crash1..etc. I want to provide these files as an input to a .bat file (let it be abc.bat) and then navigate the out put a corresponding text file. The command looks like abc.bat crash0 > crash0.txt. I have to do this to all the crash files in the folder. This abc.bat files actually converts the non-readable files to a readable format. So at the end I should have txt files like crash0.txt, crash1.txt.. etc for the corresponding crash files which i provided as the input. Can any one help with a .bat script to run this in cmd?? am new to .bat scripting.. thx in advance
for %%i in (crash*) do #call abc.bat %%i > %%i.txt
I am just starting to get a handle on batch programming.
To date, I've been copy/pasting into the MS-DOS command prompt from a text editor. Some of these copy pastes are getting large. Im sure there is a better way to go about this, ie. writing a line in command prompt that calls other text files (effectively doing the work of copy pasting).
Are these external files going to be .bat (which are just text that could also be put directly into the command prompt?) or .txt or something else?
I am mainly looking into this so that I can get into reusing code and getting into looping.
Are there any tutorials someone would recommend to get my acquainted with these topics?
Thanks for any help.
You can name a text file .bat or .cmd (the latter if you know it's only usable as a Windows batch file) and put commands into it, line by line.
You can run such files by typing their name at the command prompt when you're in the directory where they reside (or if they are contained in one of the PATH directories).
By default the behavior will match pretty much exactly with what you would type by hand. You'll see what commands are executed as well as their output. For example the following batch file (saved as test.cmd here)
echo Hello World
dir /b *.cmd
yields the following output when run
> echo Hello World
Hello World
> dir /b *.cmd
date.cmd
foo.cmd
test.cmd
x.cmd
y.cmd
You can suppress the output of the command being run by including the line
echo off
in your batch file. Prefix it with an # to suppress command output for that line in particular, but ever subsequent command won't be echoed:
#echo off
If other concrete questions arise, feel free to ask.
I am trying to write a .bat file to automate some shell commands. Most of the commands are easy and I can just put them into the batch file directly, but there is one command which instead of taking command line parameters, expects you to type in the options you want using "the standard input". I'm not exactly sure what that means. Can someone tell me how to do this? The text I would like to be entered is the contents of one of the files in the directory: "options.txt" which I want to concatenate with a variable inside the batch file "$(additionaloptions)".
Make sense?
The usual way to do this in .bat files is to use echo to write a small text file, then redirect the text file to standard in of the command.
#echo foo > bar.txt
#echo if you need multiple lines >> bar.txt
the_cmd < bar.txt
In your specific example it would something like
copy myfile.txt bar.txt
#echo %variable% >> bar.txt
The fact that you are mention $(variable) suggests to me that this is a makefile rather than a batch file. for a makefile, theres a better way.