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

标签 windows batch-file renaming

如何在 Windows 中批量删除数千个文件中的空格(而不是替换为下划线)?我可以从 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: 驱动器上的所有文件。

关于windows - 如何从文件名中删除空格(批量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270453/

相关文章:

windows - Windows 服务器重启后运行批处理脚本

batch-file - 如何在 Batch 中拆分指向其组成部分的 URL?

sql-server - 我应该如何重命名许多存储过程而不破坏内容?

c++ - 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - vector 下标超出范围

python - 尝试安装 Python 包时出现 "iconv.h: No such file"

c++ - MSVC 2010 : Allow right-click menu in console during input

windows - 如何在 localhost Windows 上释放端口 80?

linux - 在 Linux : Output Issues 上以批处理模式运行 R

windows - 嵌套 for 循环,带有嵌套循环窗口批处理中使用的变量

javascript - 使用参数重命名函数而不重写它们 - Javascript