windows 如何通过Windows批处理操作递归替换名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1613644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to replace names recursively via windows batch operation
提问by Yaya
I want to process a batch operation in a big directory. Actually I have the batch script for that process. But here , I have a problem. Some of the directory names, files names contain " " (space character). So in the batch operation this names passed as 2 arguments . and those lines will not work. So Iwant to rename " " with "_" to overcome this problem .
我想在一个大目录中处理一个批处理操作。实际上,我有该过程的批处理脚本。但在这里,我有一个问题。一些目录名、文件名中包含“”(空格字符)。所以在批处理操作中,这个名称作为 2 个参数传递。这些线路将不起作用。所以我想用“_”重命名“”来克服这个问题。
Example:
例子:
process /MyDirectory/Ola and Me/Private/TopSecretPictures/
进程/MyDirectory/Ola and Me/Private/TopSecretPictures/
this gives error. the below one works fine
这给出了错误。下面一个工作正常
process /MyDirectory/Ola and Me/Private/TopSecretPictures
进程/MyDirectory/Ola and Me/Private/TopSecretPictures
My aim is: convert | Ola and Me |>> |Ola_And_Me recursively
我的目标是:转换 | Ola 和我 |>> |Ola_And_Me 递归
:)
:)
thanks in advance ..
提前致谢 ..
回答by system PAUSE
The following script renames all files and directories recursively, starting at a given directory, converting spaces to underscores.
以下脚本以递归方式重命名所有文件和目录,从给定目录开始,将空格转换为下划线。
spaces_to_underscores.bat
source:
spaces_to_underscores.bat
来源:
@echo off
setlocal
for /r "%~1" %%t in (.) do (
for /f "usebackq tokens=*" %%f in (`dir /b/a-d "%%~t" 2^>nul:`) do (
call :proc "%%~f" "%%~t"
)
for /f "usebackq tokens=*" %%d in (`dir /b/ad "%%~t" 2^>nul:`) do (
call :proc "%%~d" "%%~t"
)
)
exit /b 0
:proc
set fn=%~1
if "%fn: =_%"=="%fn%" exit /b 0
set fn=%~2\%fn: =_%
move "%~2\%~1" "%fn%" >nul:
exit /b 0
Usage:
用法:
spaces_to_underscores "My Directory"
Given this directory structure
鉴于此目录结构
My Directory
Ola and Me
Private
TopSecretPictures
it will rename the folder "Ola and Me
" to "Ola_and_Me
", and also rename any files such as "Photo 001.jpg
" to "Photo_001.jpg
". The starting directory "My Directory
" will not be renamed.
它会将文件夹“ Ola and Me
”重命名为“ Ola_and_Me
”,并将“ ”等任何文件重命名Photo 001.jpg
为“ Photo_001.jpg
”。起始目录“ My Directory
”不会被重命名。
WARNING:Do not run this script on standard windows directories, such as "C:\Documents and Settings
" or "C:\Program Files
" or "My Documents
" or "Application Data
". There is no "undo" functionality here. Make sure you have a backup.
警告:不要在标准 Windows 目录上运行此脚本,例如“ C:\Documents and Settings
”或“ C:\Program Files
”或“ My Documents
”或“ Application Data
”。这里没有“撤消”功能。确保您有备份。
回答by Chris J
You can do this in a batch file if you use a feature called "delayed exapansion" that isn't on by default. To switch it on, you need to start cmd.exe with the /v switch:
如果您使用默认未启用的名为“延迟扩展”的功能,则可以在批处理文件中执行此操作。要打开它,您需要使用 /v 开关启动 cmd.exe:
cmd.exe /v
Once this is on, the following batch script will replace all spaces in %%i with underscores, and spit the result out:
一旦启用,以下批处理脚本将用下划线替换 %%i 中的所有空格,并将结果输出:
for /f "usebackq tokens=*" %%i in (`dir /b`) do (
set S=%%i
set T=!S: =_!
echo !T!
)
***Vauge description...***Excluding the for loop itself, the interesting parts of this are:
*** Vauge 描述...*** 不包括 for 循环本身,其中有趣的部分是:
- String substitution using the
%var:str1=str2%
syntax - Delayed expansion using
!var!
instead of%var%
- 使用
%var:str1=str2%
语法进行 字符串替换 - 使用
!var!
代替的延迟扩展%var%
First: delayed expansion... without this, the command interpreter (for whatever reason Microsoft decided to code it as) will evaluate all the parameters first, and then run the script: so this version of the script does NOT work:
第一:延迟扩展......没有这个,命令解释器(无论出于何种原因微软决定将其编码为)将首先评估所有参数,然后运行脚本:所以这个版本的脚本不起作用:
for /f "usebackq tokens=*" %%i in (`dir /b`) do (
set S=%%i
set T=%S: =_%
echo %T%
)
With this version the variable 'T' is set to the last value of the for loop before the contents of the (...) block actually execute. Which makes no sense to me. So with delayed execution enabled, we can use the delayed execution variable marks, i.e., !var! rather than %var%. Which gives us the right result.
在这个版本中,变量 'T' 在 (...) 块的内容实际执行之前被设置为 for 循环的最后一个值。这对我来说毫无意义。因此,启用延迟执行后,我们可以使用延迟执行变量标记,即!var!而不是 %var%。这给了我们正确的结果。
The other clever bit then is the set T=!S: =_!
(which basically says set T to S, replacing every '' ' in S with '_'). Without delayed expansion, this would be written set T=%S: =_%
.
另一个聪明的地方是set T=!S: =_!
(基本上是说将 T 设置为 S,将 S 中的每个 '' ' 替换为 '_')。没有延迟扩展,这将被写入set T=%S: =_%
。