且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除文件名中的空格(散装)

更新时间:2022-10-23 16:59:38

下面是一个脚本,它可以有效地批量重命名文件,从名字上剥离所有空格。

 :renameNoSpace [/ R] [FOLDERPATH]
关闭@echo
SETLOCAL disableDelayedExpansion
如果/我%〜1==/ R(
  设置forOption =%〜1%2
  设置INPATH =
)其他(
  设置forOption =
  如果%〜1NEQ,(集INPATH =%〜1 \\),否则设置INPATH =

针对%forOption%%% f由于(%INPATH%*。*)做(
  如果/我%〜F0NEQ%%〜FF(
    设置文件夹= %%〜DPF
    设置文件= %%〜NXF
    SETLOCAL enableDelayedExpansion
    回声仁!的文件夹!!文件! !文件:=!
    仁!文件夹!!文件! !文件:=!
    ENDLOCAL
  )

假设脚本名为 renameNoSpace.bat

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

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

renameNoSpace MyFolder中:重命名在当前目录中的MyFolder文件目录中的文件

renameNoSpaceC:\\我的文件夹\\:重命名指定的路径文件。行情的使用,因为路径包含空格。

renameNoSpace / R C:\\ :重命名在C中的所有文件:驱动器

How to remove spaces (not replace with underscores) from several thousand files in bulk in Windows? Can I do this from the DOS command?

Currently:

file one.mp3
file two.mp3

All files need to become:

fileone.mp3
filetwo.mp3

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 : (no arguments) Renames files in the current directory

renameNoSpace /R : Renames files in the folder tree rooted at the current directory

renameNoSpace myFolder : Renames files in the "myFolder" directory found in the current directory.

renameNoSpace "c:\my folder\" : Renames files in the specified path. Quotes are used because path contains a space.

renameNoSpace /R c:\ : Renames all files on the C: drive.