这个角色去除器不能处理百分号

时间:2017-09-08 09:51:19

标签: batch-file invalid-characters

我有这个脚本可以从文件名中删除麻烦的字符(以及一些美妙的变化):

set "fullpath=%~dp1"
set "file=%~n1"
set "ext=%~x1"
echo "%~nx1" | find /i ".tmp" >nul && exit /b

:: replace problem characters (add additional dash replacements via perl)
set "original=%file%"
set "file=%file:!=%"
set "file=%file:#=%"
set "file=%file:’='%"
set "file=%file:“=%"
set "file=%file:”=%"
set "file=%file:at&t=ATT%"
set "file=%file:&=and%"

:: additional requirements
set "file=%file: _ = - %"
set "file=%file:   = %"
set "file=%file:  = %"

:: replace underscores and dots only if used as spaces
echo "%file%"| find " " >nul || set "file=%file:_= %"
echo "%file%"| find " " >nul || set "file=%file:.= %"

:: exclamation point has to be removed before we enable delayed expansion
if not "%file%"=="%original%" ren "%fullpath%%original%%ext%" "%file%%ext%" 2>nul && echo validated "%original%%~x1"

:: remove percent sign
setlocal enabledelayedexpansion
set "original=!file!"
:: this percent replacement produces an error (because was not removed)
set "file=!file:%%=_percent!"
if not "%file%"=="%original%" ren "%fullpath%%original%%ext%" "%file%%ext%" 2>nul && echo validated "%original%%ext%"

set "original=!file!"
:: this needs to remain separated in order to add s01 tag if no season tag present
@for /l %%s in (0,1,9) do set "file=!file:series.%%s.=S0%%s!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof2=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof3=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof4=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof5=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof6=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof7=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof8=E0%%e!"
@for /l %%e in (0,1,9) do set "file=!file:%%eof9=E0%%e!"
:: if file has changed but no season tag present add s01
if not "%file%"=="%original%" echo "%file%" | find /i "s0" >nul || set "file=!file:E0=S01E0!"
if not "%file%"=="%original%" ren "%fullpath%%original%%ext%" "%file%%ext%" && echo validated "%original%"
endlocal & set "file=%file%" & set "ext=%ext%" & set "fullpath=%fullpath%" & set "full=%fullpath%%file%%ext%"

这是第一个文件的日志。看起来原始文件名最初没有所有字符......

d:\VIDEOS\SHORTS\MEDIA RELATED>setlocal enabledelayedexpansion

d:\VIDEOS\SHORTS\MEDIA RELATED>set "original=!file!"

d:\VIDEOS\SHORTS\MEDIA RELATED>set "file=!file:%=_percent!"

d:\VIDEOS\SHORTS\MEDIA RELATED>if not "SUPERCUT_ Where's The F@ing Money__" == "SUPERCUT_ Where's The F@ing Money__" ren "d:\VIDEOS\SHORTS\MEDIA RELATED\SUPERCUT_ Where's The F@ing Money__.mp4" "SUPERCUT_ Where's The F@ing Money__.mp4"   2>nul  && echo validated "SUPERCUT_ Where's The F@ing Money__.mp4"

...因为这些是完整的文件名

SUPERCUT_ Where's The F#@%ing Money_!_!.mp4
Through The Fire And Flames 100% Expert Guitar Hero 3.mp4
Top 10 F_#% Ups in Video Game History.mp4

任何建议,欢迎改进。

1 个答案:

答案 0 :(得分:1)

问题在于您调用批次的方式。

call有一些令人讨厌的副作用,因为它第二次运行解析器,它还有一个特殊的规则,可以让插入符号加倍。

但你可以通过使用变量来避免它。

for /f "delims=" %%v in ('dir /b /a-d 2^>nul') do (
    set "myFile=%cd%\%%v"
    call retype
)

在retype.bat

@echo off
for /f "tokens=*" %%f in ("%myFile%") do (
  set "fullpath=%%~dpf"
  set "file=%%~nf"
  set "ext=%%~xf"
  echo "%~nxf" | find /i ".tmp" >nul && exit /b
)