windows 如何在文件夹的所有文件名中用下划线替换所有空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20791264/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:12:26  来源:igfitidea点击:

How to replace all spaces by underscores in all file names of a folder?

windowsreplacecmd

提问by Matias Elorriaga

I'm trying to rename all the files inside a folder (all .exe files). I want to replace all the spaces with underscores, e.g. qwe qwe qwe asd.exeto qwe_qwe_qwe_asd.exe.

我正在尝试重命名文件夹中的所有文件(所有 .exe 文件)。我想用下划线替换所有空格,例如qwe qwe qwe asd.exeto 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.

我还需要在“一行”/“一个命令”中执行此操作,但我会接受所有有效的答案。

回答by MC ND

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 文件,将空格替换为下划线并使用新名称重命名文件

回答by Gray

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. Place that batch file in the folder with all the .exe's and it will replace the spaces with underscores when you run it.

*.bat使用上述内容创建批处理文件 ( )。将该批处理文件放在包含所有 .exe 的文件夹中,当您运行它时,它将用下划线替换空格。

回答by n8b

Simple as:

简单如:

set filename=qwe qwe qwe asd.exe
set filename=%filename: =_%

回答by Jimadine

Using forfiles:

使用 forfile:

forfiles /m *.exe /C "cmd /e:on /v:on /c set \"Phile=@file\" & if @ISDIR==FALSE ren @file !Phile: =_!"

Add /safter forfilesto recurse through subfolders.

添加/safterforfiles以递归遍历子文件夹。

回答by Hamza Rashid

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.batcontains script to replace spacewith underscore
  • recursive.batcontains script to do recursion in all subdirectories
  • replace.bat包含脚本,以取代spaceunderscore
  • recursive.bat包含在所有子目录中进行递归的脚本

How to use?

如何使用?

  • Save both replace.batand recursive.batin same directory.
  • Replace .extwith desired file extension to match (like .mp4) in replace.bat.
  • Double click (run) ONLY recursive.batfile.
  • 保存这两个replace.batrecursive.bat在同一目录下。
  • 替换.ext为所需的文件扩展名以匹配(如.mp4replace.bat
  • 双击(运行)ONLYrecursive.bat文件。

回答by Alex

Save the following 2 commands in a .batfile. It will replace " "with "_"in all files and folders, recursively, starting from the folder where the file is stored.

将以下 2 个命令保存在一个.bat文件中。它将取代" ""_"中的所有文件和文件夹,递归,从该文件所在的文件夹开始。

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.

注意:第一行是对文件执行此操作,第二行是对文件夹执行此操作。每条线都可以单独使用。

回答by DuckCowMooQuack

Minor tweak to Hamza Rashid's answer. Specifically his recursive.bat script.

对 Hamza Rashid 的回答稍作调整。特别是他的 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 保持不变,说明保持不变。

回答by ricarela

set data=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2% set data=%data: =0%

设置数据=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6, 2%组数据=%数据:=0%