如何从文件名中删除空格(批量)

如何从Windows中的数千个文件中删除空格(而不是用下划线replace)? 我可以从DOS命令做到这一点吗?

目前:

file one.mp3 file two.mp3 

所有文件需要成为:

 fileone.mp3 filetwo.mp3 

这是一个脚本,可以有效地批量重命名文件,剥离名称中的所有空格。

 :renameNoSpace [/R] [FolderPath] @echo off setlocal disableDelayedExpansion if /i "%~1"=="/R" ( set "forOption=%~1 %2" set "inPath=" ) else ( set "forOption=" if "%~1" neq "" (set "inPath=%~1\") else set "inPath=" ) for %forOption% %%F in ("%inPath%* *") do ( if /i "%~f0" neq "%%~fF" ( set "folder=%%~dpF" set "file=%%~nxF" setlocal enableDelayedExpansion echo ren "!folder!!file!" "!file: =!" ren "!folder!!file!" "!file: =!" endlocal ) ) 

假设脚本名为renameNoSpace.bat

renameNoSpace :(无参数)重命名当前目录中的文件

renameNoSpace /R :重命名以当前目录为根的文件夹树中的文件

renameNoSpace myFolder :重命名当前目录中的“myFolder”目录中的文件。

renameNoSpace "c:\my folder\" :重命名指定路径中的文件。 使用引号是因为路径包含空格。

renameNoSpace /R c:\ :重命名C:驱动器上的所有文件。

创建一个PowerShell文件 – *.ps1扩展名

写下这段代码:

 dir | Where-Object { $_.name.Contains(" ") } | Rename-Item -NewName { $_.name -replace " ","" } 

保存,然后右键单击 – >使用PowerShell运行

您可以编写一个简单的脚本来为一个文件/目录执行此操作,例如:

 @echo off setlocal enableextensions enabledelayedexpansion set "ARG=%~1" ren "%ARG%" "%ARG: =%" 

…然后如果你愿意,可以在你关心的所有文件和/或目录上运行它。 例如,如果您将上述脚本创建为myrenamingscript.cmd,则可以通过运行以下命令在当前目录中的所有非dir文件上运行:

 for %f in (*) do @myrenamingscript.cmd "%~f" 
 @echo off setlocal enableextensions enabledelayedexpansion for %%f in (*.*) do ( set ARG=%%~nxf rename "%%f" !ARG: =! ) 

我遇到的问题是,有可能已经有一个文件的名字,你试图给新的文件(例如,如果有2个文件名为“文件one.txt”和“file_one.txt文件“当您尝试用下划线替换空格时,一个文件将替换另一个文件)。 所以我做了这个脚本来检查新的名字是否已经存在,如果是这样,在文件名的后面加上一个数字(把数字加1,直到没有其他名字的文件)。 有关如何更改的说明位于顶部(表示为行)。 如果使用*。*选项,则不要将批处理文件存储在具有要重命名文件的相同文件夹中。 我希望这有帮助。

 @echo off REM Instructions REM This script repaces spaces from file names with underscores. REM If you want to just remove the spaces uncomment lines 30 and 52 and comment out the lines 29 and 51. REM set the following parameters. REM pb is the folder containing the files we want to rename (fullpath) REM tm is a temporary folder that will be created and deleted. Just put a folder that does not exist and is not used by anything else (fullpath). REM all is the file type you want to raname. Eg *.* for every file, *.txt for TXTs, *.pdf for PDFs etc REM you don't have to change anything else set pb=<folder containing the files to rename> set tm=<a temp folder that does not exist> set all=*.* set pa=%pb%%all% setlocal EnableDelayedExpansion cd /d %pa% set /a count=1 if not exist %tm% mkdir %tm% for /f %%F in (%pa%) do ( set name=%%~nF set name2=!name: =_! REM set name2=!name: =! set name3=!name2!%%~xF if !name2! == %%~nF ( move /y %%~dpF\!name3! %tm%\ >nul ) else ( if not exist %%~dpF\!name3! ( if not exist %tm%\!name3! ( ren "%%F" "!name3!" move /y %%~dpF\!name3! %tm%\ >nul ) ) ) ) :rename for /f %%F in (%pa%) do ( set name=%%~nF set name2=!name: =_! REM set name2=!name: =! set name4=!name2!%count% set name3=!name4!%%~xF if !name2! == %%~nF ( move /y %%~dpF\!name3! %tm%\ >nul ) else ( if not exist %%~dpF\!name3! ( if not exist %tm%\!name3! ( ren "%%F" "!name3!" move /y %%~dpF\!name3! %tm%\ >nul ) ) ) ) set /a count = %count% + 1 set /a loop = 0 for %%F in (%pa%) do (set /a loop = 1) if %loop% equ 1 goto rename move /y %tm%\%all% %pb% >nul rmdir /s /q %tm%