简体   繁体   English

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

[英]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.exeqwe_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?如何使用?

  • Save both replace.bat and recursive.bat in same directory.replace.batrecursive.bat保存在同一目录中。
  • Replace .ext with desired file extension to match (like .mp4 ) in replace.bat .替换.ext具有期望的文件扩展名匹配(像.mp4中) replace.bat
  • Double click (run) ONLY recursive.bat file.双击(运行)ONLY 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.

相关问题 如何在目录树中递归地将所有文件和文件夹名称中的所有空格替换为下划线? - How to replace all spaces by underscores in all file and folder names recursively in a directory tree? 批处理文件用文件名中的空格替换下划线 - Batch File to replace underscores with spaces in a filename 查找文件夹中的所有图像,并用单个图像替换它们,但保留原始文件名 - Find all images in a Folder and replace them with a single image but keeping original file names 通过批处理将单个文件的文件名中的空格替换为下划线 - Replace Underscores with Spaces in Filename of Single File via Batch 从所有文件名中删除括号和空格的批处理脚本 - Batch script to remove parentheses and spaces from all file names 如何在批处理文件中使用for循环列出文件夹中所有文件和目录的名称 - How to list the names of all the files and directories in a folder using for loop in a batch file 在第一个下划线之前重命名文件夹中所有文件名的开头 - Renaming start of all file names in folder before first underscore 如何重命名文件夹下的所有文件名以在Windows中添加修改日期作为前缀 - How to rename all file names under a folder to add modified date as prefix in Windows 如何在Windows中删除/替换目录名称中的空格 - How to remove/replace spaces in directory names in Windows 将所有文件重命名为小写,替换空格 - Rename all files to lowercase, replace spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM