Directing files in different folders through couple programs with .bat - windows

Basically, I'm trying to batch-compress .png images which are residing in various folders. For that, I use pngquant and pngout. Every image should go through these apps this way:
pngquant.exe --force --speed 1 --verbose image.png -o step1.png
pngout.exe step1.png step2.png
I want all images compressed at once. To gather a list of all images in folders, I search with *.png query in root folder. The aim is to just throw all images to batch file and wait for the result.
I looked around a bit and come up with this
for %%i in (*.png) do (
"...\pngquant.exe" --force --speed 1 --verbose "%%~ni.png" -o "%%~ni2.png"
"...\pngout.exe" "%%~ni2.png" "%%~ni3.png" )
So I'm dragging the images onto the .bat file, but only the images from the first folder would go through, the batch file ignores images from the subsequent folders. How can I fix that? Thanks.

for /f "tokens=*" %%i in ('dir *.png /s /b') do (
"pngquant.exe" --force --speed 1 --verbose "%%i" -o "%%~di%%~pi%%~ni2.png"
"pngout.exe" "%%~di%%~pi%%~ni2.png" "%%~di%%~pi%%~ni3.png" )
try this. but note that PNGs will be created in the same folders. don't know however if it was your intent.

Dropping files on the batch file is the same as providing them as arguments on the command line. Your script ignores its arguments and processes just its working directory.
Replace the for argument (*.png) with (%*), which contains the list of all arguments.
Also, instead of %%~ni, which returns just the name of the file, you'll need to say %%~dpni, to include the drive and path, if the files are not in the same folder as the script.

Related

Converting multiple PDF files to Text files in subdirectory using Ghostscript in Windows

OS: Windows 10 Professional
I'd like to convert multiple PDF files to text files in main and sub-directories using GhostScript on Windows command line. Here's my command below:
gswin64c -sDEVICE=txtwrite -o test1.txt "test1.pdf"
This code converts "test1.pdf" to "test1.txt" using Ghostscript but I would like to perform the following:
Look for all pdf files in main directory and all sub-directories
Execute Ghostscript on all PDF files
Give the same file name to the output text file. (test1.pdf -> test1.txt)
I appreciate your time and consideration on this!
Build a batch or .cmd file like this:
#echo off
REM Replace these with your actual location
D:
cd "\Main Directory"
for /R %%F in (*.pdf) do call :DOPDF "%%F"
goto ENDIT
:DOPDF
for %%X in (%1) do set PDF_TXTFNM=%%~dpnX.txt
gswin64c -sDEVICE=txtwrite -o "%PDF_TXTFNM%" %1
goto :EOF
REM Clean up
:ENDIT
set PDF_TXTFNM=
EDIT TO ADD:
Side Note: If you do not wish to change your working directory to the main directory, be aware that for /R %%F in ("D:\Main Directory\*.pdf") ... will only work if there actually is a .pdf file in D:\Main Directory. The two main workarounds are to make it the current working directory (the solution chosen in my example) or to force a dummy .pdf file to exist in that directory and then choose to not process it in the subroutine using an IF statement. Holler if you need an example of that latter technique.

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.

How to add a file in multiple zip files?

Im trying to move a picture in every zip folder in current directory. I searched online for some guidance on how to do it using batch (.bat) and the only solution I figured out is to make a macro of it but it takes too long for it to complete.
Edit:
I have 50 zip folders which I want to add a picture inside each one in a faster way other than draging the picture each time inside each of the 50 zip folders.
I would appreciate any other ideas and help you could offer.
Test this on some sample zip files in a test folder. It assumes 7-zip is in the folder shown.
It's not so much faster as it is easier, without manually manipulating the files.
The actual zipping speed will be pretty much the same.
#echo off
for /f "delims=" %%a in ('dir *.zip /b /a-d') do (
"C:\Program Files\7-Zip\7z.exe" a "%%a" "my-picture.jpg"
)
pause
Adds (or updates) README.txt in the zipfiles foo.zip and bar.zip from README.txt in the current directory:
$ for f in foo.zip bar.zip ; do zip -u $f README.txt ; done
You can package files into a zip file using the Winzip utility. It must be installed on your computer. Follow these steps:
Select the files you need and click on any of the selected ones.
Select Add to Zip
In the Add window, in the Add to Archive field, specify the path and name of the archive to be created.
The ZIP file is saved at the path you specified.

Windows batch copy files from subfolders to one folder

I had tried to make batch script that copies all *.tif files located in D:\images(random named subfolders here) to d:\all.
xcopy D:\Downloads\*.TIF D:\temp\ /s
works, but it copies with all folder tree. I tried to use other keys, but its dont works.
Thanks for help!
FOR is your friend. Read HELP FOR on the /R option and the %~nx variable substitution; and then try this very simple code.
pushd d:\downloads
for /r %%a in (*.tif) do (
echo COPY "%%a" "d:\temp\%%~nxa"
)
popd
watch carefully the results and then remove the ECHO command.
You will have to refine the code to cope with errors, duplicate names, edge cases, names with reserved characters, race conditions, cosmic events...
Searched files using windows file explorer for e.g. *.gif , I got files in search window, used Edit=>Select All , copy and then pasted to desired folder. This copied all the gif files in all sub directories to single folder.
For large number of files, it sometimes hangs/not responding, but otherwise works ok.
pushd D:\Source
for /r %%a in (*.?*) do (
MOVE "%%a" "D:\Destination folder\%%~nxa"
)
popd
You can also use the XXCOPY freeware. Works like XCOPY, but when you use a /SG parameter, it flattens the sub-directories. See how to use it here.

Batch script to pngcrush all files in all subfolders

I have a folder structure with one main parent folder containing many subfolders, and in these some PNGs, something like:
.../data
.../data/013523/
.../data/345343/
.../data/395338/
.../data/013523/filex.png
.../data/013523/filey.png
.../data/345343/filea.png
.../data/345343/fileb.png
.../data/345343/filec.png
I'd like to crush all these PNGs with a Windows batch-script knowing only the location of the parent data folder (ie the folder names and png names are unknown, it should just crush all PNGs in all folders).
I took a look at Drag and drop batch file for multiple files? but this didn't seem to be quite what I was after.
Oh and no fancy naming options required, crushing in-place is fine.
Well
for /r ...\data %%x in (*.png) do pngcrush "%%x"
should do it.
If the path to your data directory contains spaces somewhere, the following should work better, though:
pushd "...\data"
for /r %%x in (*.png) do pngcrush "%%x"
popd

Resources