从文件名中删除特殊字符的脚本

发布于 2024-10-07 12:12:01 字数 214 浏览 0 评论 0原文

我有一个包含大量文件的文件夹。许多文件名都有“%”和/或“&”其中的人物。

e.g. Test&doc.pdf
e.g Test%doc.doc

有没有一种快速方法可以删除“%”和“&”使用 Windows 批处理文件、vbscript 或类似文件的字符?

我们将不胜感激所有帮助。

谢谢。

I have a folder containing a large number of files. A lot of the filenames have '%' and/or '&' characters in them.

e.g. Test&doc.pdf
e.g Test%doc.doc

Is there a quick way I could remove the '%' and '&' characters using a windows batch file, vbscript or something similar?

All help will be greatly appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒数 2024-10-14 12:12:01

这是批量执行此操作的方法(如果您好奇的话)。最大的限制是,如果文件名的符号超过百分之一,则它将不起作用,因为 shell 会将其扩展为变量。我不知道如何立即解决这个问题。

它从脚本所在的任何目录开始,并递归地处理所有子目录。

@echo off

setlocal enabledelayedexpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nN
  set var=!var:^&= !
  set var=!var:%%= !

  if not "!var!"=="%%~nN" (
    if not exist "%%~dpN!var!%%~xN" (
      echo "%%N" --^> "!var!%%~xN"
      ren "%%N" "!var!%%~xN"
    ) else (
      echo File "!var!%%~xN" ^(from %%N^) already exists.
    )
  )
)

例如,打印输出如下:

C:\batch\schar>schar
"C:\batch\schar\Test%doc.doc" --> "Test doc.doc"
"C:\batch\schar\Test%doc.pdf" --> "Test doc.pdf"
File "Test doc.pdf" (from C:\batch\schar\Test&doc.pdf) already exists.
"C:\batch\schar\subdir\FILE%here" --> "FILE here"

Here's how you can do it in batch (in case you're curious). The big limitation is that if you have filenames with more than one percent sign, it won't work because the shell expands it to a variable. I don't know immediately how to fix that.

It starts from whatever directory the script is in, and works recursively over all subdirectories.

@echo off

setlocal enabledelayedexpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nN
  set var=!var:^&= !
  set var=!var:%%= !

  if not "!var!"=="%%~nN" (
    if not exist "%%~dpN!var!%%~xN" (
      echo "%%N" --^> "!var!%%~xN"
      ren "%%N" "!var!%%~xN"
    ) else (
      echo File "!var!%%~xN" ^(from %%N^) already exists.
    )
  )
)

E.g., prints output like this:

C:\batch\schar>schar
"C:\batch\schar\Test%doc.doc" --> "Test doc.doc"
"C:\batch\schar\Test%doc.pdf" --> "Test doc.pdf"
File "Test doc.pdf" (from C:\batch\schar\Test&doc.pdf) already exists.
"C:\batch\schar\subdir\FILE%here" --> "FILE here"
贪了杯 2024-10-14 12:12:01

@indiv

如果有人可以生成没有字符限制的批处理解决方案,我将会印象深刻。

好的,我会尝试的。

@echo off

setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nxN
  setlocal EnableDelayedExpansion
  set "org=!var!"
  set "var=!var:&= !"
  set "var=!var:%%= !"

  if not "!var!"=="!org!" (
    if not exist "%%~dpN!var!" (
      echo "!org!" --^> "!var!"
      ren "!org!" "!var!"
    ) else (
      echo File "!var!" ^(from !org!^) already exists.
    )
  )
  endlocal
)

诀窍是,切换延迟扩展,因为 for-loop-vars (%%N) 的扩展应该在没有延迟扩展的情况下完成,否则您会丢失感叹号,并遇到插入符号问题。
但要处理和修改字符串,您应该使用延迟扩展。

但为什么?答案是了解批处理解析器的各个阶段。

我试图在这里解释一下。
how-does-the-windows-命令解释器-cmd-exe-解析脚本

@indiv

If someone can produce a batch solution without character limitations, I'll be massively impressed.

Ok, I'll try.

@echo off

setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nxN
  setlocal EnableDelayedExpansion
  set "org=!var!"
  set "var=!var:&= !"
  set "var=!var:%%= !"

  if not "!var!"=="!org!" (
    if not exist "%%~dpN!var!" (
      echo "!org!" --^> "!var!"
      ren "!org!" "!var!"
    ) else (
      echo File "!var!" ^(from !org!^) already exists.
    )
  )
  endlocal
)

The trick is, to toggle the delayed expansion, because expanding of for-loop-vars (%%N) should be done without delayed expansion, else you lose the exclamation marks, and got problems with carets.
But to handle and modify the strings you should use delayed expansion.

But why? The answer is to understand the phases of the batch parser.

I tried to explain it here.
how-does-the-windows-command-interpreter-cmd-exe-parse-scripts

烈酒灼喉 2024-10-14 12:12:01

我很快就把它们放在一起,但没有测试它,但这个 VBScript 应该可以解决问题。告诉我您是否需要文件夹递归替换等奇特的东西。

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Your folder here
objStartFolder = "X:\MYFOLDER"

Set objFolder = objFSO.GetFolder(objStartFolder)
Set regEx = New RegExp

'Your pattern here
regEx.Pattern = "[&%]" 

Set colFiles = objFolder.Files
For Each objFile in colFiles
    objFile.Rename(regEx.Replace(objFile.Name, "")
Next

I've quickly thrown that together and didn't test it, but this VBScript should do the trick. Tell me if you need fancy stuff like folder recursive replacing etc.

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Your folder here
objStartFolder = "X:\MYFOLDER"

Set objFolder = objFSO.GetFolder(objStartFolder)
Set regEx = New RegExp

'Your pattern here
regEx.Pattern = "[&%]" 

Set colFiles = objFolder.Files
For Each objFile in colFiles
    objFile.Rename(regEx.Replace(objFile.Name, "")
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文