Windows batch command: multiple folders - windows

Following Windows batch command converts all tif images in the folder C:\RootFolder\Folder1.
for %%i in (C:\RootFolder\Folder1\*.tif) do "Tiff2Pdf.exe" -o C:\RootFolder\Folder1\%%~ni.pdf %%i
How can I do it for all the folders available in RootFolder?
RootFolder
-Folder1
-Folder2
-Folder3
.
.
Thanks for your time

There's another way - just to add it:
#echo off
for /r "c:\rootfolder\folder1" %%a in (*.tif) do "Tiff2Pdf.exe" -o "%%~dpna.pdf" "%%a"
I also changed the loop variable to a because i is close to l and I and 1 in many fonts.

FOR /F "delims=" %%i IN ('dir /b /s C:\RootFolder\Folder1\*.tif') DO "Tiff2Pdf.exe" -o "%%~dpi%%~ni.pdf" "%%i"
Use dir /s /b to do a full recursive enumeration
Use FOR /F "delims=" to parse the results and handle paths with spaces.
Use the %%~dpi%% to get the directory of each file.
Use %%~ni to get the file's name with out an extension.

Related

Is there any way for windows batch script or command prompt to list the directories from a certain folder only without including the hard drive?

I tried using dir /b/s *.png to list the directories of all my png files. the results were like this:
D:\Newfolder\test\images\approved\11.png
D:\Newfolder\test\images\approved\12.png
D:\Newfolder\test\images\approved\13.png
D:\Newfolder\test\images\approved\14.png
D:\Newfolder\test\images\approved\15.png
D:\Newfolder\test\images\approved\16.png
D:\Newfolder\test\images\approved\17.png
D:\Newfolder\test\images\approved\18.png
D:\Newfolder\test\images\approved\19.png
D:\Newfolder\test\images\approved\20.png
I want it to be shortened so it will display only starting from the images folder;
images\approved\11.png
images\approved\12.png
images\approved\13.png
images\approved\14.png
images\approved\15.png
images\approved\16.png
images\approved\17.png
images\approved\18.png
images\approved\19.png
images\approved\20.png
is it possible to do?
if it's not, is there any way for me to edit the directories by deleting the first few folder from a generated text file?
say i put dir /b/s *.png > path.txt how do i edit the texts since the list got no whitespaces.
i'm still new to this so i'm not so familiar with much commands but this is as far as my understanding can do.
Assuming this comment in your question:
"I want it to be shortened so it will display only starting from the "images" folder;"
you always want from the images folder, and also assuming the images folder always exists in the path:
#echo off & setlocal enabledelayedexpansion
for /R %%i in (*.png) do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
Or to explicitly ensure you only include paths with \images\ in the path:
#echo off & setlocal enabledelayedexpansion
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
example of using pushd:
#echo off & setlocal enabledelayedexpansion
pushd "%~1"
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
popd
So the above you can run from cmd and simply pass the path you want the script to run in as script_name.cmd "C:\some path\here"

Adding file extensions to files without them in Windows

I am trying to add file extensions to a large number of files located in a series of folders and subfolders in Windows. For some reason, these files do not have a file extension on them and I need them to have the extension .ddd so I can convert them to PDFs using a separate program.
cd R:\PRODUCTION\92
ren *. *.ddd
Note that this command does indeed work but only on folders that actually contain the files I need, and no subfolders. What could I add or change to hit all files in all subfolders? Thanks in advance.
Please try the following command. If it looks like it will do the correct rename, remove the ECHO from the REN command line.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%~f" "%~nf.ddd")
In a .bat file script, double the percent character on the variable.
CD /D R:\PRODUCTION\92
FOR /F "delims=" %%f IN ('DIR /S /B /A:-D "*."') DO (ECHO REN "%%~f" "%%~nf.ddd")

Win cmd rar each file separately while keeping the name, with spaces

I'm using Windows and I have a huge list of files
something nextthing.ext
other something.ext
banana apple.ext
I'm trying to generate the following:
something nextthing.rar
other something.rar
banana apple.rar
I've done some research and I couldn't find how to do this. I know WinRAR has a built in way to do this, but it won't let me keep the names. It just renames them to Foldername1.rar, Foldername2.rar ....
I tried using the following command line:
for /f "delims=|" %f in ('dir /b "C:\Users\Adam\
Desktop\Files"') do rar a %f *.rar
However, it doesn't generate all files and they seem to be named wrongly...
For example, what should be banana apple.rar becomes banana.rar and apple.rar
Okay, found how....
#echo off &setlocal
set "path=C:\Program Files\WinRAR;%path%"
:indiv
echo(
echo(
FOR %%i IN (*) do (
rar a "%%~ni.rar" "%%~i" || echo Error building archive!
)
You can use in a command prompt window to compress each file in a directory into a separate archive:
for /f "delims=" %f in ('dir "%USERPROFILE%\Desktop\Files" /B /A-D') do "%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idcdp "%~nf.rar" "%f"
Important are the double quotes around %f as some file names contain 1 or more spaces and in this case the file names must be enclosed in double quotes.
Above command for usage in a batch file:
#echo off
for /f "delims=" %%f in ('dir "%USERPROFILE%\Desktop\Files" /B /A-D') do "%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idcdp "%%~nf.rar" "%%f"
This command ignores subfolders because of /A-D. It compresses only each file in directory Files on desktop of current user.

List only files from subfolders of specified folder

This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.

Recursive Unzipping with 7z.exe

I stumbled across the below line as a means of extracting every .zip file in every subfolder.
FOR /F "usebackq" %a in (`DIR /s /b *.zip`) do 7z.exe e %a
I've tried this on a WinXP cmd.exe prompt and I get the error:
"a was unexpected at this time."
Can somebody please tell me whats wrong with the above line. Is this a 7z.exe error or is there something wrong with the batch script syntax. I did cut and paste this into a .bat file.
Cheers
Try to change %a with %%a:
FOR /F "usebackq" %%a in (`DIR /s /b *.zip`) do 7z.exe e %%a
Building on #PA.'s answer (remember to remove the #echo when you've verified that the output is what you want), if you want to preserve the directory structure inside the zip file, use the x command instead of e:
FOR /R %a IN (*.zip) DO #echo 7z x "%a"
And if you want to extract the files into a folder with the same name as their respective zip file, use the -o switch and the %~n filename extractor prefix:
FOR /R %a IN (*.zip) DO #echo 7z x "%a" -o"%~na"
Finally, if you want to do all of the above and overwrite any existing files, use the -aoa switch:
FOR /R %a IN (*.zip) DO #echo 7z x "%a" -o"%~na" -aoa
Helpful resources
http://www.dotnetperls.com/7-zip-examples
Batch file
Here it is, all condensed. The following is a batch script that will work for all zip files in the current folder (assuming 7zip is installed). It defaults to echoing what commands would run and only runs when you pass in /y (as in, yes, please do the unzipping now).
:: To actually include the path expansion character (tilde), I had to give valid numbers; see http://ss64.com/nt/rem.html for bug reference. Also, try call /? for more info.
#REM The %~n0 extracts the name sans extension to use as output folder. If you need full paths, use "%~dpn0". The -y forces overwriting by saying yes to everything. Or use -aoa to overwrite.
#REM Using `x` instead of `e` maintains dir structure (usually what we want)
#FOR /R %%a IN (*.zip) DO #(
#if [%1] EQU [/y] (
#7z x "%%a" -o"%%~dpna" -aoa
) else (
#echo 7z x "%%a" -o"%%~dpna" -aoa
)
)
#echo USAGE: Use /y to actually do the extraction
read HELP FOR and then try the following in a command prompt...
FOR /R %a IN (*.zip) DO #ECHO 7z e "%a"
note that we have enclosed %a in "
Once you have checked the validity of the output, remove the ECHO. I would suggest to move the command into a BAT file. In that case, change %a to %%a
#echo off
FOR /R %%a IN (*.zip) DO (
7z e "%%a"
)

Resources