loop through subfolders in cmd - cmd

I am using the following in a CMD window to rename all the files in the directory to .pdf
ren *. *.pdf
How can I apply this at a higher level directory to batch through all sub-directories
tried google and found /r but can't make it run without errors.

A simple for loop from the command line will work
This will rename all files to *.pdf. Example: filename.txt => filename.txt.pdf
for /r %f in (*.*) do ren %f %~f.pdf
This will rename all files to .pdf. Example: filename.txt => filename.pdf
for /r %f in (*.*) do ren %f %~pnf.pdf

Related

Renaming multiple files from subdirectories using CMD for EX: 123_abc.pdf file to 123_abc_DOC.pdf

I tried with the below command but is cuts some part of file n replace the _TEST.pdf with it
FOR /R %f IN (*.pdf) DO REN "%f" *_TEST.pdf
EX:
Orgininal file name is TEST_123.pdf
Renamed file name is TEST_TEST.pdf
Expected result is TEST_123_TEST.pdf
use modifiers (see for/?):
FOR /R %f IN (*.pdf) DO REN "%~ff" "%~nf_TEST%~xf"
this is command line syntax. To use it in a batch file, double each %:
FOR /R %%f IN (*.pdf) DO REN "%%~ff" "%%~nf_TEST%%~xf"

How to copy all files found with script to another folder using Windows Shell

I want to copy the files that are found from a search into a folder.
The script
where /r C:\ *.jpg *.jpeg *.png *.gif >> C:\output.txt
gives a text document with the location of the files I want. How can I create a copy of the files found from the search into some output folder X:\output\
I want to get copies of all images in a folder including all subfolders into an output folder of all the images.
You could get the output of a command with a for /f loop:
for /f "delims=" %%A in ('where /r C:\ *.jpg *.jpeg') do echo working on %%A
or process a file with:
for /f "delims=" %%A in (C:\output.txt) do echo working on %%A
But for is capable to do it itself:
for /r "C:\" %%A in (*.jpg *.jpeg *.png *.gif) do ECHO copy "%%~fA" "X:\output\%%~nxA"
(remove the ECHO after troubleshooting to actually enable the copy command)
Note:
- this doesn't take care of possible duplicate names.
- this is batch file syntax. For use on command line, replace every %% with a single %

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")

How to exclude file extensions in the output filelist.txt, within a batch "dir" script in CMD

I am a CMD newbie and have a question with a batch script I am working on.
I have a parent directory with 30 sub-directories containing .pdf files, and I need a filelist.txt for each sub-directory, and have each filelist.txt save as the file name of the sub-directory it belongs too. This has been completed with the script below:
#echo off
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
DIR /B /ON /A-D "%%a" > %%a.txt.
move %%a.txt "%%a" >nul
)
My question is how, can I remove file extensions in the output of each filelist.txt. For ex. when I run the script now, the output .txt file shows 1111.pdf
1112.pdf
I need the ".pdf" removed
I know with a "for" command you can "do" echo %%~na to remove file extensions, but I have no clue how/where to factor this into the current script.
Any help is appreciated!
You are using DIR command to list all files. It does not have a switch to hide extension.
You can replace the DIR command with a FOR loop, like you pointed out.
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
for %%f in ("%%a\*") do #echo %%~nf >> %%a.txt.
move %%a.txt "%%a" >nul
)

Delete all files in \bin directories using the Windows command line

I want to clean up my binary files in my development directory. Is there a way I can recurse through the directory structure and delete all files that are in a bin directory using the Windows command line? (I am using Windows 7.)
Per Nathan, I tried to make a batch foo.bat:
#ECHO OFF
for /R %%x in (.) do call:myExistFunc %%x
GOTO:EOF
:myExistFunc
if exist %~1\.\bin call:myDeleteFunc %%~1\bin
GOTO:EOF
:myDeleteFunc
echo. Deleting files from %~1
del %~1*.* /Q
...and this worked.
Create a batch file that contains the following:
for /R %%x in (bin) DO del "%%x\\*.*" /Q
This will recursively walk through all child directories (from the current directory) and delete all files from every BIN folder. You could change the *.* to whatever file type you'd like to delete.
This should work in a batch file:
for /R %%x in (bin) do if exist "%%x" del /q "%%x\*.*"
If running this command directly from the command line, replace all instances of %%x with just %x.
It's been awhile since I've had use any DOS but..
DEL BIN\*.*
That should delete the files in the directory named BIN below the current directory.
You should write a bat file:
for /r %%f in (*) do 'WHATEVER YOU WANT TO DO'

Resources