Want to execute command on each file in directory one at a time through batch file windows - windows

I am currently doing this to execute a single command on a particular type of files in directory.
COPY *.prn /B \\\\{$PC}\\{$PRINTER}
The PC And Printer Part is redundant no need to understand that
Instead of executing all files at once I want to be able to do one file at a time through a loop

try this:
for %%i in (*.prn) do COPY "%%~i" /B \\\\{$PC}\\{$PRINTER}

Im not entirely sure what you mean but try this, it will execute the command once for each file in the current directory and (all subdirectories, but this exact snipets not ideal for subdirectories) ending with the extension .prn:
for /r %%a in (*) do (
if %%~xa == .prn (
copy %%~na%%~xa /B \\\\{$PC}\\{$PRINTER}
)
)
Tell me if this doesn't work or you want to do this for subdirectories as well.
Yours, Mona

Related

Moving files with .bat script on windows 10

Recently I wanted to make .bat script that would move .avi files from subfolders in specified directory to another directory.
e.g.
H:\MAINDIRECTORY\dir1\avi1.avi
H:\MAINDIRECTORY\dir2\avi2.avi
H:\MAINDIRECTORY\dir3\avi3.avi
....
To one directory called e.g.
H:\Movies
I've made script which looks something like this
#echo off
move H:\Pobrane\*\*.avi H:\Filmy
But when I'm tryin to execute it I got something like this:
the file name, directory name or volume lable syntax is incorrect
Please help me find a way out from this situation.
As move does not support wild cards you can try with for /r
#echo off
for /r "H:\Pobrane\" %%# in (*.avi) do (
move /y "%%~f#" "H:\Filmy"
)

Windows Batch script to rename files with it's folder name within a for loop

I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"

Bulk renaming files in relation to the folder names

I am very new to coding and bulk processes but i am looking for a command line SPECIFICALLY for windows command prompt and i am wondering if such a thing exists. So I have a folder containing 111 subfolders, with each subfolder containing between 20 and 40 png image files. Each subfolder is named 001-111 accordingly and the png files are ordered how i want them, however i am looking for a command line that would be able to quickly and efficiently name all the pngs in the folders to the name of the folder followed by the png number in brackets
e.g. for folder 037, i would want the png's to be renamed to: 037(1), 037(2), 037(3) etc...
I am hoping for the best although i am unsure such a code may not be possible or be simply done.
Also if you come up with a code that achieves this process, it would be great if you could reply with the simple command line that i could use rather than a full explanation because i am new to coding and far from fluent with the language or terms or how things work. I know this same process can be achieved by going select all>rename (ctrl a>f2) and renaming to the folder name however i need to use this process frequently and dont want to have to open each folder, i would rather have a command line for cmd that would do it swiftly
Thank you and a simple answer would be greatly appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "parentdir=u:\parent"
FOR /l %%a IN (1001,1,1111) DO (
SET dir=%%a&SET "dir=!dir:~1!"
FOR /f "delims=" %%i IN ('dir /a-d /b "%parentdir%\!dir!\*.png" 2^>nul') DO (
ECHO REN "%parentdir%\!dir!\%%~nxi" "!dir!(%%~ni)%%~xi"
)
)
GOTO :EOF
Test results:
Starting directory :
u:\parent\001\1.png
u:\parent\037\1.png
u:\parent\037\2.png
u:\parent\111\999 with spaces in name.png
Script response
REN "u:\parent\001\1.png" "001(1).png"
REN "u:\parent\037\1.png" "037(1).png"
REN "u:\parent\037\2.png" "037(2).png"
REN "u:\parent\111\999 with spaces in name.png" "111(999 with spaces in name).png"
Obviously, you'd need to replace the value assigned to parentdir with your actual target directory name.
The script will report the renames it proposes to do. To actually invoke the rename remove the ECHO keyword.
I would create a batch file like so:
renamepng.bat:
cd %%1
if ERRORLEVEL 1 goto end
for %f in *.png do mv "%f" "%%1(%f).png"
cd ..
:end
This will attempt to cd to the directory name provided on the command line, abort if that fails, then rename all the .png files and return to the previous directory
then call it like so:
for %d in ??? do call renamepng.bat %d
which will loop through all 3-character file and directory names in the current directory, can call the batch file on each one. Using call instead of just the batch file name causes execution to return to the loop when the batch finishes.

Windows Batch File Looping Through Directories to Process Files?

I need to write/use a batch file that processes some imagery for me.
I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.
The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.
All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Aacini's solution works but you can do it in one line:
for /R %%f in (*.tif) do echo "%%f"
Jack's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:
for /R "\\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"
The original tip for this method is here
Posting here as it seems to be the most popular question about this case.
Here is an old gem I have finally managed to find back on the internet: sweep.exe. It executes the provided command in current directory and all subdirectories, simple as that.
Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):
:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever
So, you want to run this program in current directory and all subdirectories:
:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd
:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages
Pros: You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.
Con: Might fail with some commands (containing spaces, quotes, I don't know). See this thread for example.
I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:
for /F "delims=" %%i in ('dir /ad /on /b /s') do (
pushd %%i
dir | find /i "Directory of"
popd
)
Replace the 3rd line with whatever command you may need.
dir /ad - list only directories
The cool thing is pushd does not need quotes if spaces in path.
rem Replace "baseline" with your directory name
for /R "baseline" %%a in (*) do (
echo %%a
)

DOS command to Copy a file from parent dir to specific folder within many child directories

I'm trying to teach myself some simple DOS commands and have used relatively simple commands to copy or move files, however this specific request is presenting a challenge for me and would appreciate some expertise from this forum.
C:\Parent\library.eds (location of my source file)
Any time I update library.eds in the parent directory, I would like to copy that file into every Child directory that contains a folder named "LIB". I have standardized the Child directories to the following:
C:\Parent\Child1\INPUT
C:\Parent\Child1\OUTPUT
C:\Parent\Child1\LIB {paste library.eds here}
C:\Parent\Child2\INPUT
C:\Parent\Child2\OUTPUT
C:\Parent\Child2\LIB {paste library.eds here}
and loop through until all children with LIB directories contain the updated file "library.eds"
Thank you for your help!
Mark
Here's a command that can get you started:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO #ECHO COPY "C:\Parent\library.eds" "%%~D"
Once you get it working the way you want, remove the #ECHO part to actually do the copy:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO COPY "C:\Parent\library.eds" "%%~D"
Extra help for these commands
HELP FOR
HELP DIR
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
This lets you execute command, and loop over its output. Each line will be stuffed into %variable, and can be expanded out in otherCommand as many times as you like, wherever you like. %variable in actual use can only have a single-letter name, e.g. %V.
"delims="
This lets you ignore any whitespace output by 'command', so it properly handles directories that have spaces in their names.
DIR /b /a:D /s C:\Parent\LIB
This will search for all files under C:\Parent that have the name LIB. It will recursively go through subdirectories because of /s. It will only find directories (because of /a:D). It will output them in a way that is useful for looping, because of /b.
%%D instead of %D
This is required in batch files. If you did this on the command prompt, you would use %P instead.
if you know all the child directories before hand the simplest solution is batch files tutorial on batch files
I dont know how loops work in ms dos but in linux bash shell loops are simple to program..
Any ways batch files are the simplest option
if you can download cygwin then you can use the following bash script to do the job without hardcoding it
The code
P.S you will need to change the name of the folder from tmp to LIB and the filename from LOL to ur file name .. The script is pretty self explanatory.
Unix shells are better than MS-DOS so it might be a good idea to get cygwin anyways

Resources