Using cmd batch to find all the file with the same filename - windows

For example, in a directory, I have N .eps files named name1.eps, name2.eps, ... and also much more pdf files. But within these pdf files, there are exactly N pdf files with the same filename as eps files, that is name1.pdf, name2.pdf,....
So I want a batch that can scan the current dir, give a list of all eps files and corresponding list of pdf files.
finally I want another batch named convert.bat which takes two parameters to run a series of command as below
convert.bat name1.eps name1.pdf
convert.bat name2.eps name2.pdf
.....
.....
the convert.bat first compares the modified time of name.eps and name.pdf, and if the modified time of name.eps is newer than name.pdf, then it will run epstopdf name.eps, otherwise it will do nothing.
I am really a newbie in using cmd batch. could somebody teach me how to realize the whole process I mentioned above. Thank you so much!

I figure out a way. The following batch file do the whole job.
SETLOCAL ENABLEDELAYEDEXPANSION
for %%G in (*.eps) do #if not exist "%%~nG.pdf" (epstopdf "%%G") else (
(for /f "delims=" %%i in ('dir /B /O:D "%%G" "%%~nG.pdf"') do set newest=%%~xi) & (
if !newest!==.eps epstopdf "%%G"))

Related

Trying to make a recursive batch file that will go through folders and convert from .tif to .pdf

I am trying to make a recursive batch file that will go through a set of folders and convert the file from .tif to .pdf.
The folders are all setup by year, month date:
2016 -January (1-31)
-February (1-28)
...down through the months and days. I am trying to write a batch file that will go through each of these folders and find any .tif files and convert them to .pdf. I am new to batch and scripting so I don't know exactly what I would need to write to make this happen. Here is what I got and most of it I found online.
I am using a program called Image Magick for the conversion:
#Echo on
cd C:\Users\ars001\Downloads\January\*)
%%v = .tif
for /r %%v in (C:\Users\ars001\Downloads\January\*) do convert "*.tif" "*.pdf"
Questions:
Can you make an extension a variable? For example A= .pdf?
Why does the variable use two Percent signs? Does that mean echo it?
The path in the question is an example folder and path I made on my local computer before trying it on the server.
example Path:
January 1, 2
This line is what you're trying to do.
FOR /R "C:\Users\ars001\Downloads" %%a in (*.tif) DO convert "%%~a" "%%~dpna.pdf"
You can run this on the command line (instead of in a batch file) if you change %% to %. You need two %% in a batch file (because the cmd processor resolves all %%variables to %variables first, so if you didn't use two %%, the FOR syntax wouldn't work. But on the command line itself, you just need the one %.
Basically, it says to start in the Downloads folder, looking in all subfolders for any file that looks like *.tif. Then it converts each one it finds to PDF (assuming the ImageMagick convert.exe is in your path).
If you haven't picked it up, yes, you can specify parts of the filename. See the end of HELP FOR. In short, with %A, %~dpnxA means the drive, path, name, and extension of %A.
To delete the original after conversion, do this:
FOR /R "C:\Users\ars001\Downloads" %%a in (*.tif) DO (
convert "%%~a" "%%~dpna.pdf"
IF "%ERRORLEVEL%"=="0" del "%%~a"
)
Another way to write the same thing (and on one line) is like this.
FOR /R "C:\Users\ars001\Downloads" %%a in (*.tif) DO convert "%%~a" "%%~dpna.pdf" && del "%%~a"
The && says to only run the next bit if the command does not return an error (i.e. errorlevel 0).

How to write a batch file to crop all the pdfs in a directory and store them in a new directory?

I'm totally unfamiliar with batch files but I'm pretty sure I need one for the task at hand:
I want to run pdfcrop for all the files in a particular directory and store the cropped files in a new directory. New directory is called 'croppedfiles' and if it doesn't already exist in the location where the pdfs are stored then such a directory is created and the output files are stored there.
I'd like the output files to have the same name as old files with the addition of '_cpp' at the end.
syntax for pdfcrop is just pdfcrop input.pdf output.pdf
Referring to the poor material you provided for this question - I mean only your for cycle quotation - I can only suggest a pair of tips: first you can narrow the group of files on which the for cycle is going to work, using a piped find ".pdf" command. Then you can use another nested for cycle to obtain the name of the file to be processed by pdfcrop and to set it as a variable to be used for the output path. Here is the example script:
for /f "delims=" %%g in ('dir ^"[set your desired path]^" ^| find
^".pdf^"') do ( set VAR=%%g for /f "tokens=1,2 delims=." %%m in
('!VAR!') do (mkdir "[chose your subdirectory name]" pdfcrop "!VAR!" "[subdirectory]\%%m[set
your additional characters].%%n" ) )
I hope it works, because I have been not able to test it.

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.

Want to execute command on each file in directory one at a time through batch file 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

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
)

Resources