[英]How to replace all spaces by underscores in all file names of a folder?
I'm trying to rename all the files inside a folder (all .exe files).我正在尝试重命名文件夹中的所有文件(所有 .exe 文件)。 I want to replace all the spaces with underscores, eg
qwe qwe qwe asd.exe
to qwe_qwe_qwe_asd.exe
.我想用下划线替换所有空格,例如
qwe qwe qwe asd.exe
到qwe_qwe_qwe_asd.exe
。
I need to do this using the command line.我需要使用命令行来执行此操作。 I tried a lot of possible solutions I found on internet and even on this site, but I can't make it work.
我尝试了很多我在互联网上甚至在这个网站上找到的可能的解决方案,但我无法让它工作。
I also need to do this in "one single line" / "one command", but I'll accept all the working answers.我还需要在“一行”/“一个命令”中执行此操作,但我会接受所有有效的答案。
A one liner一个班轮
cmd /e:on /v:on /c "for %f in ("* *.exe") do (set "n=%~nxf" & set "n=!n: =_!" & ren "%~ff" "!n!" )"
Spawn a cmd instance, with extensions and delayed expansion enabled, and for each exe file with spaces in name, replace spaces with underscores and rename the file with the new name生成一个 cmd 实例,启用扩展和延迟扩展,并且对于名称中有空格的每个 exe 文件,将空格替换为下划线并使用新名称重命名文件
Adapted from here:改编自这里:
https://stackoverflow.com/a/16129486/2000557 https://stackoverflow.com/a/16129486/2000557
@echo off
Setlocal enabledelayedexpansion
Set "Pattern= "
Set "Replace=_"
For %%a in (*.exe) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Pause&Exit
Create a batch file ( *.bat
) with the above contents.使用上述内容创建一个批处理文件 (
*.bat
)。 Place that batch file in the folder with all the .exe's and it will replace the spaces with underscores when you run it.将该批处理文件放在包含所有 .exe 的文件夹中,当您运行它时,它将用下划线替换空格。
Simple as:简单如:
set filename=qwe qwe qwe asd.exe
set filename=%filename: =_%
Using forfiles:使用 forfile:
forfiles /m *.exe /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==FALSE ren @file !Phile: =_!"
Add /s
after forfiles
to recurse through subfolders.在
forfiles
后添加/s
以递归子文件夹。
Based on @Gray answer, I have extending it to replace filenames recursively in all subdirectories.基于@Gray 的回答,我将其扩展为递归替换所有子目录中的文件名。
File 1: replace.bat文件1: replace.bat
setlocal enabledelayedexpansion
set "pattern= "
set "replace=_"
for %%I in (*.ext) do (
set "file=%%~I"
ren "%%I" "!file:%pattern%=%replace%!"
)
File 2: recursive.bat文件 2: recursive.bat
for /d /r . %%D in (*) do (
copy replace.bat "%%D\replace.bat"
cd "%%D"
replace.bat
del replace.bat
cd..
)
Files文件
replace.bat
contains script to replace space
with underscore
replace.bat
包含用underscore
替换space
脚本recursive.bat
contains script to do recursion in all subdirectories recursive.bat
包含在所有子目录中进行递归的脚本How to use?如何使用?
replace.bat
and recursive.bat
in same directory.replace.bat
和recursive.bat
保存在同一目录中。.ext
with desired file extension to match (like .mp4
) in replace.bat
..ext
具有期望的文件扩展名匹配(像.mp4
中) replace.bat
。recursive.bat
file.recursive.bat
文件。设置数据=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6, 2%组数据=%数据:=0%
Save the following 2 commands in a .bat
file.将以下 2 个命令保存在
.bat
文件中。 It will replace " "
with "_"
in all files and folders, recursively, starting from the folder where the file is stored.它将从存储文件的文件夹开始,递归地将所有文件和文件夹中的
" "
替换为"_"
。
forfiles /s /m *.* /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==FALSE ren @file !Phile: =_!"
forfiles /s /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==TRUE ren @file !Phile: =_!"
Note: First line is doing this for files, and second one is doing this for folders.注意:第一行是对文件执行此操作,第二行是对文件夹执行此操作。 Each line can be used separately.
每条线都可以单独使用。
Minor tweak to Hamza Rashid's answer.对 Hamza Rashid 的回答稍作调整。 Specifically his recursive.bat script.
特别是他的 recursive.bat 脚本。
recursive.bat递归.bat
set orig=%cd%
for /d /r . %%D in (*) do (
copy replace.bat "%%D\replace.bat"
cd "%%D"
replace.bat
del replace.bat
cd %orig%
)
replace.bat stays the same and the instructions stay the same. replace.bat 保持不变,说明保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.