this character remover can't handle percent sign

50 views Asked by At

I have this script that removes troublesome characters from filenames (along with some esthetic changes):

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%"

this is the log for the first file. looks like the original filename doesn't have all the characters originally...

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"

...because these are the full filenames

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

any suggestions, improvements welcomed.

1

There are 1 answers

2
jeb On BEST ANSWER

The problem is the way you call your batch.

call has some nasty side effects, as it runs the parser a second time and it has also a special rule for carets to double them.

But you can avoid it by using a variable instead.

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

And in 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
)