Pipe list of files into program - windows

I have a list of files to convert like this
C:\Users\jtl999\foo_001.jpg
C:\Users\jtl999\foo_002.jpg
To convert them manually I invoke the converter like this
convert_files.bat C:\Users\jtl999\foo_001.jpg
Basically what I want to do is read through the list of files one at a time and pass the file as a argument. On Linux I could use find and xargs but I am on Windows 7.

for %f in ("c:\users\jtl999\foo_*.jpg") do call convert_files.bat "%~ff"
For each file in the indicated set, call the batch file, passing as a parameter the full path to the file
The syntax is for usage from command line. To use this in a batch file: Change the parameters of for command so that the percent sign is doubled. %f should be %%f and %~f should be %%~f

for /f %i in ('dir /b /s "C:\Users\jtl999\foo_*.jpg"') do call convert_files.bat "%i"
use %%i instead of %i in batch-files.
You need the parameter /s to get the full path. Although this means, you will also get any files in sub directories that fits your filter (if there are some).

Related

Execute all commands in current folder sequentially

Assuming I have a bunch of sqlcmd commands in .cmd files, order alphabetically e.g.:
01.setup.cmd
02.version1.cmd
03.version2.cmd
04.version3.cmd
how could one sequentially execute these in correct order with another .cmd file?
On windows:
for /F "tokens=*" %a in ('dir /b *.cmd') do call "%a"
This just loops over the result of dir /b *.cmd calling each in turn.
explanation from the docs:
FOR /F processing of a text file consists of reading the file, one
line of text at a time and then breaking the line up into individual
items of data called 'tokens'. The DO command is then executed with
the parameter(s) set to the token(s) found.
So my command says:
"tokens=*" don't give me individual tokens, give me the whole line as one hit
%a - name the line variable %a (note: it'll needs to be escaped as %%a if you're putting it in a batch file
('dir /b *.cmd') This is the input that it'll loop over. A bare directory listing for all .cmd files
then what I want it to do. Call the command %a.
If I didn't add the tokens bit it would work fine until you find a space in the file names.

Use .bat file to loop through folders/subfolder and and provide the folder/subfolder as input to an exe

I have to run a tool(.exe) that is stored in a particular folder.(Say C:\Temp)
The input parameter for this tool is the (.inf) files placed in various other folders/subfolders.(Say C:\Test\SampleInfFiles, C:\Test\SampleInfFiles\Inf2)
I want to create a batch file where i can run this tool recursively on the various folders.
I am not sure how to provide the folder name of the inf as the input parameter to the tool.
#echo off
for /r /d C:\Test\SampleInfFiles %%x in (dir /b *.inf) do(
pushd "%%x"
call C:\Test\ScanInf.exe
popd
)
I am new to all this. Could you please help create a batch file.
If I understand correctly, you need to specify the folder that contains one or more inf files to the executable, and not the inf files itself.
Apparently the order of parameters is important. for expects the part after /r to be a path. Furthermore, you can just specify * as a set.
The loop now browses all subfolders of the given folder. Inside the loop, it will check if that folder contains an inf file (on that level). If it does, the executable is called with the folder as parameter. Notice the use of ~ in the variable. This strips off any quotes, so you can safely add quotes without risking to have double quotes.
#echo off
for /d /r C:\Test\SampleInfFiles\ %%x in (*) do (
pushd "%%~x"
if exist %%x\*.inf call C:\Test\ScanInf.exe "%%~x"
popd
)
See: Using batch parameters
Here as a nice one-liner:
for /R "C:\Test\SampleInfFiles" /D %%X in (*.*) do if exist "%%~fX\*.inf" call C:\Test\ScanInf.exe "%%~fX"
If you want to run that in the command prompt directly, replace each %% by %.
I think the call command could be omitted.

How do I find a directory in and store it into a variable, using a Windows batch file?

I need to find the location of a specific directory, and then store that directory path into a variable within a Windows batch script.
I also want the command to return when it finds a match (to avoid searching the entire hard drive once the directory has already been found).
So far I've tried this on the command line:
dir c:\ /s /b /ad | find "DirectoryName"
The problem with this is that it searches the entire drive, even after a match is found. Plus, I still can't figure out how to store the result in a variable within a batch file. There should only be a single result.
Basically I need the equivilent of somehting like this on Linux/bash:
export DIRPATH=`find / -name "DirectoryName" -print -quit`
Thanks for looking!
In batch you need FOR /F to get the output of a command.
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "DirectoryName"`) DO (
set "DIRPATH=%%p"
)
echo %DIRPATH%
As there are quotes in the find command you need the usebackq-option.
And it's necessary to escape the pipe character one time, as it should pipe the dir command, not the for command

.bat file for renaming multiple folders

I am trying to write a batch script to rename multiple folders.
I would like to do something like below:
Rename all folders under the "Workspace" folder by appending my name in the end of the folder names
For example, rename:
Workspace/RiskFolder
Workspace/PNLFolder
to:
Workspace/RiskFolder_myname
Workspace/PNLFolder_myname
Is this possible?
You could use for to loop through each directory and rename it like so:
for /D %%f in (C:\path\to\Workspace\*) do rename "%%f" "%%~nxf_myname"
I tested this on Windows 7, but it should work at least as far back as with Windows XP.
What that does is this: for each directory in the path (within parenthesis), assign the directory name to the variable %%f, then rename the directory %%f to the name in the format you want (with your name attached). %%f holds the full pathname, which is fine for the first argument to the rename command, but for the second argument, we only want the filename+extension, thus the ~nx modifier prepended to our variable name.
By the way, when using this for loop on the command line (rather than part of a batch file) you only want to use one % instead of %% for your variable name. E.g. for %f in... instead of above.
See the following references from Microsoft for more details:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true
You can use the following command within your batch file:-
for /F "usebackq tokens=*" %%a in (`dir /ad /b %1`) do ren %1\%%a %%a%2
This is the DOS 'for' command, which iterates over given set of items, and for each element in the set, performs the given action. For the given requirement, we need to do the following:-
1) Accept name of folder which contains sub-folders to be renamed(in your example, it is Workspace).
2) Accept the string to be appended to the end(in your example, it is your name).
3) List the names of sub-folders in the folder.
4) Rename by appending the string to original name.
Let's see how this for command accomplishes that. The format of 'for' command used here is:-
for /F ["options"] %variable IN (`command`) do command [command-parameters]
The command here assumes that the required parent directory name and string to be appended are passed on as command line parameters. These are represented by %1 and %2 (first and second parameters).
To enable us to issue a dos command to be evaluated, we need to use the /F option. The option string is :-
"usebackq tokens=*"
usebackq specifies backquouted string is a command to be evaluated.(Note that the dir command is enclosed within backquotes(`) )
tokens=* means to consider each line as a single token and pass to the command
To list the sub-directories in parent directory, we use the command:-
dir /ad /b %1
/ad displays only directories (ignores files)
/b displays it in bare format, i.e., only names are returned and date, time and other info are not.
%1 is the command line variable referring to parent directory.
%%a is the variable which receives the sub-directory name in each iteration. Double percentage symbol is required since we use it in a batch file, otherwise, just one is required (like %a)
Finally, we specify the action to be performed:-
ren %1\%%a %%a%2
%1\%%a constructs absolute path to sub-directory
%%a%2 append second command line parameter to original name
For more info on for command, type following in a command prompt:-
for /?
For another usage example, refer Loopy loops: The DOS way
No need for a batch file. This will work from the command line
for /d %D in ("Workspace\*") do ren "%D" "%~nxD_myName"
If you do use a batch file, then %D must become %%D
If there is no need to perform folder/file renaming with a batch file, you can also use the Bulk Rename Utility for Windows. Check this: http://www.bulkrenameutility.co.uk/Download.php
For /D %%f in (*) do rename "%%f" "%%fWhatEverNameYouLike"
pause
The pause is to see it! Make a cmd of it and put it in the folder that you want to rename all it's subfolders! They'll rename to each one folders name, plus the WhatEverNameYouLike

How to blank files in batch

How would I go about reading an entire directory and blanking files with a specific extension? I have an application that reads the content of a specific folder and returns an error if a file is missing, however it does't check to see if the files are valid, so I want to make them NULL to get around the checks.
if by 'blanking' you mean truncating them, you could use the following:
for /f %%a in ('dir *.[my ext]') do (echo . > %%a)
note that the double % is for use within a batch file. if you are running this from a command line, use a single %.
EDIT:
to incorporate #Loadmaster's improvement:
for /f %%a in ('dir *.[my ext]') do (type nul > %%a)
First, create an empty file. Call it "blank". You can use Notepad to just save an empty file, for example.
Let's suppose the specific extension is ".xyz". Run this:
for %f in (*.xyz) do copy /y blank %f
The for loop sets variable "%f" to each file name in turn, and runs the copy command. The copy command copies the blank file on top of each matching file.
By the way, you can find out more about the for command using the help command:
help for
You could make the batch file take the filter and then it's much more useful.
#for %%i in (%1) do cd.>%%i
Usage (if you call it empty.bat):
empty *.c

Resources