How to continuously run a exe within a batch file with different parameters - windows

I have an exe application that takes in 2 parameters. One is a input file path with an specific extension (e.g. *.jpg) and Second is the output file path.
Now in a folder, I have let's say 100 jpeg images which I want to pass in continuously and saved the output with the same file name as the input (extension will be different, the exe does the conversion).
Any idea how do I write a batch file to achieve this?
Thanks and Regards,
Perumal

Try this:
#FOR %%1 IN (%1) DO convert %%1 %2\%%n1.png
To be used as:
bulkconvert c:\test\*.jpg c:\test
It'll call convert for each file that matches the search pattern c:\test*.jpg and a 2nd parameter will be provided with the path provided as batch's 2nd parameter (note: there is not the trailing backslash) with the same file name but with extension png.
As reference: How to get folder path from file path with CMD

Related

Batch command XCOPY prompts me: Is the destination file or directory

When I run this file:
xcopy .\*.odt .\source.zip
I am prompted to specify what source.zip is:
xcopy .\*.odt .\source.zip
Does .\source.zip specify a file name
or directory name on the target
(F = file, D = directory)?
In my case when it find the .odt file to copy the file and place in the same directory but with new name source.zip. Is there approach to avoid the prompting since I always want destination to be a file not directory.
Any .odt file (being in .zip format in fact) is a binary file, see OpenDocument Document Representation
As a collection of several sub-documents within a package, each of which stores part of the complete document. This is the common
representation of OpenDocument documents. It uses filename extensions
such as .odt, .ott, .ods, .odp ... etc. The package is a
standard ZIP file with different filename extensions and with a
defined structure of sub-documents. Each sub-document within a package
has a different document root and stores a particular aspect of the
XML document. All types of documents (e.g. text and spreadsheet
documents) use the same set of document and sub-document definitions.
Therefore, you need to treat it as a binary file (read copy /?):
copy /B .\*.odt .\source.zip
Above command would work smoothly only if there will be only one file with extension .odt. Otherwise, it will prompt you for Overwrite .\source.zip? (Yes/No/All):. To stay on the safe side:
from command line for %G in (.\*.odt) do copy /B "%G" ".\source_%~nG.zip"
from a batch script for %%G in (.\*.odt) do copy /B "%%G" ".\source_%%~nG.zip"
%~nG (or in batch %%~nG) explanation: read Parameter Extensions.

Windows Batch - separate directory and filename from full file path string

I'd like to separate both a filename and a directory string from inside a full file path variable so i can refer to each separately later in a batch script.
Input Variable: SET "FULL=C:\test\file.txt"
Wanted Output:
FILE: file.txt
PATH: C:\test\
Currently the for loop & syntax is not making a whole lot of sense to me (in this batch scripting language) which is making it harder for me to find a working solution online...
set "FULL=C:\test\file.txt"
for %%a in ("%FULL%") do (
set "filePath=%%~dpa"
set "file=%%~nxa"
)
for loop will iterate over a set of files (only one file in set in this case), and for each of them the code after the do clause is executed.
For each iteration of the for loop and so for each execution of the do clause, the replaceable parameter (the %%a in the previous code) will hold a reference to the file being processed.
This replaceable parameter has some modifiers (that can be seen running for /?) to retrieve the required information from the file. The modifiers are in the form
%% ~ modifier replaceableParameter
In the previous sample code, d modifier is the drive where the file is stored, p is the path (folder hierarchy) where the file is stored, n is the file name without extension and x is the extension. So
%%~dpa = drive and path of the file being referenced by a
%%~nxa = name and extensions of the file being referenced by a

Why file cannot be found to copy

I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/

Renaming files of variable length in DOS

I'm trying to rename files that have '#' to '_'.
I understand that there is a straigt forward way of replacing the nth character in a file.
How do we rename files , if # symbol is present in different places in different files
For example, assuming the below files are present in a directory
a#file.txt
asdf#kfi.png
uiuydfjfk#kdi.txt
I want the output to be like this one
a_file.txt
asdf_kfi.png
uiuydfjfk_kdi.txt
Is there anyway to accomplish this ?
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
It creates renfile.bat for you to examine for errors, and then execute.
dir *#* /b |repl "(.*)(#)(.*)" "ren \x22$&\x22 \x22$1_$3\x22" x >renfile.bat

Windows Batch file Dynamic create statements

I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).

Resources