I would like to change the file extension from .jpg to .txt. This must happen both in the current folder and in all sub-folders. What did I do wrong in my batch?
for /R %%G IN (.) DO (rename *.jpg *.txt)
I would like to change the file extension from .jpg to .txt. This must happen both in the current folder and in all sub-folders. What did I do wrong in my batch?
for /R %%G IN (.) DO (rename *.jpg *.txt)
What did I do wrong in my batch?
If you really want to know...
1. First, set/define your work directory for your bat file **run rename yours files.jpg ... :
The command prompt requires the use of a unit/ drive, and also, a working directory/folder, which does not imply that the drive/folder that your bat is or was invoked is assumed/defined as the to work unit/drive: directory/folder as the same as bat file have. By not defining a drive/folder, the %systemdrive%:\WINDOWS\System32 will be assumed in the execution of your bat.
Obs.: 1 This explaining the reason for cd /d "%~dp0" is present in some many codes you can find in superuser answer/question, where the code is saying to cmd command line interpreter to assume the Drive/Path from the bat file itself as the working directory, and to do the pertinent job there.
2. You can to tell to the cmd.exe (command line interpreter) to run your code in the folder where you need it, if it is the same as file.bat, or if it is elsewhere.
@echo off
rem :: Option 1) Enter in your bat (%0) Dive:\Path\folder ::
cd /d "%~DP0"
rem :: Option 2) Enter in your target Dive:\Path\folder ::
cd /d "D:\Folder\Imags\"
rem :: Option 3) Run your command in your target Dive:\Path\folder ::
@echo off
for /R "D:\Folder\Imags\" %G in ...(
3. To avoid problems with any and all special characters or spaces using double quotes, it doesn't hurt, and as someone already said in this community, "..find peace with yourself by using them!..", you may not even need them, but use them anyway...
4. Note that to execute your rename "*.jpg" "*.txt" command in the current folder, you do not need a for loop, and rename "*.jpg" "*.txt" is enough in the current folder...
5. The command For /r .. (*.jpg)... will cause execution of the rename command to occur for each file found recursively.
If the target directory is the same as your bat file is running..
@echo off
cd /d "%~DP0"
for /R %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
Or... if the target directory is not the same as your bat file is running..
@echo off
cd /d "D:\Folder\Imags\"
for /R %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
Or... explicitly the directory for the for loop action...
@echo off
cd /d "D:\Folder\Imags\"
for /R "D:\Folder\Imags\" %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
Obs.: 3 You can also reduce that execution to each folder recursively found, which is different from an execution/file to an execution/folder, faster to read and manipulate folder queue, which read and manipulate queue by folder.
@echo off
cd /d "D:\Folder\Imags\"
:: run your command in your root folder 1st ::
ren "*.jpg" "*.zip"
:: run your command in your all your sub-directories ::
for /R /D %%G IN (*)DO 2>nul ren "%%~dpnxG\*.jpg)" "*.txt"
Obs.: 4 - Using a For loop you can expand your variable:
%~i - expands %i removing any surrounding quotes (")
%~fi - expands %i to a fully qualified path file/dir name only
%~ni - expands %i to a file/dir name only
%~xi - expands %i to a file/dir extension only
%%~nxi => expands %%~i to a file/dir name and extension
Use the FOR variable syntax replacement:
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
The modifiers can be combined to get compound results:
%~pnI - expands %I to a path and file name only
%~pnxI - expands %I to a path, file name and extension only
Obs 5.: Using For / R with For / D in the same for loop:
FOR /R - Loop through files (recursively)
FOR /D - Loop through several folders/directories
Obs 6.: About using %%~x in directory name observation note in ss64.com:
Full Stop Bug
Although Win32 will not recognise any file or directory name that begins or ends
with a '.' (period/full stop) it is possible to include a Full Stop in the middle
of a directory name and this can cause issues with FOR /D.
Parameter expansion will treat a Full Stop as a file extension, so for a directory
name like "Sample 2.6.4" the output of %%~nI will be truncated to "Sample 2.6" to
return the whole folder name use %%I or %%~nxI
Obs.: 7 Read this answers
Some further reading:
[√] For Loop
[√] For /D Loop
[√] For /R Loop
for /R %%G IN (.) DO (rename *.jpg *.txt)
You are close. Use the following command in a batch file:
for /R %%G IN (*.jpg) DO (ren "%%G" "%%~nG.txt")
From a cmd line shell:
for /R %G IN (*.jpg) DO (ren "%G" "%~nG.txt")
Notes:
"s added around the arguments to ren in case case the filenames contain spaces.%~n1 - Expand %1 to a file Name without file extension or path