windows 如何从文件名中删除空格(批量)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11270453/
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 remove spaces from file names (in bulk)
提问by Nick Kahn
How to remove spaces (not replace with underscores) from several thousand files in bulk in Windows? Can I do this from the DOS command?
如何从 Windows 中的数千个文件中批量删除空格(不替换为下划线)?我可以从 DOS 命令执行此操作吗?
Currently:
目前:
file one.mp3
file two.mp3
All files need to become:
所有文件都需要变成:
fileone.mp3
filetwo.mp3
回答by dbenham
Here is a script that can efficiently bulk rename files, stripping all spaces from the name.
这是一个脚本,可以有效地批量重命名文件,从名称中去除所有空格。
: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
)
)
Assume the script is called renameNoSpace.bat
假设脚本名为renameNoSpace.bat
renameNoSpace
: (no arguments) Renames files in the current directory
renameNoSpace
:(无参数)重命名当前目录中的文件
renameNoSpace /R
: Renames files in the folder tree rooted at the current directory
renameNoSpace /R
: 重命名以当前目录为根的文件夹树中的文件
renameNoSpace myFolder
: Renames files in the "myFolder" directory found in the current directory.
renameNoSpace myFolder
:重命名在当前目录中找到的“myFolder”目录中的文件。
renameNoSpace "c:\my folder\"
: Renames files in the specified path. Quotes are used because path contains a space.
renameNoSpace "c:\my folder\"
: 重命名指定路径中的文件。使用引号是因为路径包含空格。
renameNoSpace /R c:\
: Renames all files on the C: drive.
renameNoSpace /R c:\
: 重命名 C: 驱动器上的所有文件。
回答by bogudoby
Create a powershell file - *.ps1
extension
创建一个 powershell 文件 -*.ps1
扩展名
Write this code:
写下这段代码:
dir |
Where-Object { $_.name.Contains(" ") } |
Rename-Item -NewName { $_.name -replace " ","" }
save, then right click -> run with powershell
保存,然后右键单击 -> 使用 powershell 运行
回答by Javiers
In Windows:
在 Windows 中:
- Open a Command Prompt.
- Go to the folder with the cd command (eg.: cd "paht of your folder").
- Open a powershell by typing: powershell
- Then input this: get-childitem *.mp3 | foreach {rename-item $_ $_.name.replace(" ","")}
- 打开命令提示符。
- 使用 cd 命令转到文件夹(例如:cd "paht of your folder")。
- 键入以下命令打开 powershell:powershell
- 然后输入: get-childitem *.mp3 | foreach {rename-item $_ $_.name.replace(" ","")}
回答by reuben
You can write a simple script that does this for one file/directory, e.g.:
您可以编写一个简单的脚本来为一个文件/目录执行此操作,例如:
@echo off
setlocal enableextensions enabledelayedexpansion
set "ARG=%~1"
ren "%ARG%" "%ARG: =%"
...and then if you'd like, run it over all the files and/or directories you care about. For instance, if you create the above script as myrenamingscript.cmd, you can run it over all non-dir files in the current dir by running:
...然后,如果您愿意,可以在您关心的所有文件和/或目录上运行它。例如,如果将上述脚本创建为 myrenamingscript.cmd,则可以通过运行以下命令在当前目录中的所有非目录文件上运行它:
for %f in (*) do @myrenamingscript.cmd "%~f"
回答by pathe3
@echo off
setlocal enableextensions enabledelayedexpansion
for %%f in (*.*) do (
set ARG=%%~nxf
rename "%%f" !ARG: =!
)
回答by Mike_Gre
The problem i have faced is that there is a possibility that there is already a file with the name you try to give to the new file (eg if there are 2 files in the folder named "file one.txt" and "file_one.txt" when you try to replace the spaces with underscores, one file will replace the other). So I made this script that checks if the new name already exists and if so places a number at the end of the file name (adds 1 to the number until there is no other file with that name). Instructions about what to change are at the top (commended out lines). Do not store the batch file in the same folder you have the files to be renamed if you use *.* option. I hope this helps.
我遇到的问题是,可能已经有一个文件与您尝试给新文件的名称相同(例如,如果文件夹中有 2 个文件名为“file 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. E.g. *.* 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%