我想将文件扩展名从 更改为.jpg
。.txt
这必须在当前文件夹和所有子文件夹中进行。我在批处理中做错了什么?
for /R %%G IN (.) DO (rename *.jpg *.txt)
答案1
我在这批货中做错了什么?
如果你真的想知道……
1.首先,设定/定义你的工作目录对于你的 bat 文件 **运行rename yours files.jpg ...
:
命令提示符需要使用单元/驱动器,以及工作目录/文件夹,这并不意味着您的 bat 被调用的驱动器/文件夹被假定/定义为unit/drive: directory/folder
与 bat 文件一样工作。通过不定义驱动器/文件夹,%systemdrive%:\WINDOWS\System32
将被假定在执行你的球棒时。
观察:1这解释了cd /d "%~dp0"
存在于您可以在超级用户答案/问题中找到的许多代码中,其中代码告诉 cmd 命令行解释器将 bat 文件本身的驱动器/路径作为工作目录,并在那里执行相关工作。
2.你可以告诉cmd.exe
(命令行解释器)在您需要的文件夹中运行代码,如果它与file.bat
,或者在其他地方。
@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.为了避免使用双引号引起任何特殊字符或空格的问题,这不会造成任何损害,正如有人在这个社区中说过的那样,“...通过使用它们来让自己找到平静!...”,您甚至可能不需要它们,但无论如何都要使用它们……
4.请注意,要执行您的rename "*.jpg" "*.txt"
命令在当前文件夹中,不需要循环for
,并且rename "*.jpg" "*.txt"
在当前文件夹中就足够了......
5.命令For /r .. (*.jpg)...
将导致对每个递归找到的文件执行重命名命令。
如果目标目录与你运行的bat文件相同..
@echo off cd /d "%~DP0" for /R %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
或者...如果目标目录与你正在运行的 bat 文件不一样..
@echo off cd /d "D:\Folder\Imags\" for /R %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
或者...明确 for 循环操作的目录...
@echo off
cd /d "D:\Folder\Imags\"for /R "D:\Folder\Imags\" %%G IN (*.jpg)DO ren "%%~G" "%%~nG.txt"
观察:3您还可以将执行减少到递归找到的每个文件夹,这与执行/文件到执行/文件夹不同,可以更快地读取和操作文件夹队列,而不是按文件夹读取和操作队列。
@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"
观察:4- 使用For
循环可以扩展变量:
%~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
观察5:在同一个 for 循环中使用For / R
with :For / D
FOR /R - Loop through files (recursively) FOR /D - Loop through several folders/directories
The option /D /R is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames) Note: Source linked to ss64.com
观察6.:关于使用%%~x
在directory
名称观察注释中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
观察:7读这个答案
答案2
我想将文件扩展名从 .jpg 更改为 .txt
for /R %%G IN (.) DO (rename *.jpg *.txt)
你已经接近目标了。在批处理文件中使用以下命令:
for /R %%G IN (*.jpg) DO (ren "%%G" "%%~nG.txt")
从 cmd 行 shell:
for /R %G IN (*.jpg) DO (ren "%G" "%~nG.txt")
笔记:
"
在参数周围添加了 s,以防文件ren
名包含空格。%~n1
- 扩展%1
为文件名,不包含文件扩展名或路径