variables not carried over from this script - batch-file

this script takes a video file as input and after preliminary renaming extrapolates values such as title and year or episode along with a search strings strict for finding duplicate videos and loose for finding all episodes of a series. it then does a controlled filebot rename for the file.
set "filename=%~n1"
set "parentdir=%~2"
:: =============================================================================================================================
:: 1. FILENAME VALIDATOR
:: =============================================================================================================================
set "original=%filename%"
set "filename=%filename:!=%"
set "filename=%filename:&=and%"
setlocal enabledelayedexpansion
set "filename=!filename:%%=_percent!"
for /l %%s in (0,1,9) do set "filename=!filename:series.%%s=S0%%s!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof2=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof3=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof4=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof5=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof6=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof7=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof8=E0%%e!"
for /l %%e in (0,1,9) do set "filename=!filename:%%eof9=E0%%e!"
setlocal disabledelayedexpansion
if not "%filename%"=="%original%" ren "%~1" "%filename%%~x1" && echo validated "%original%"
:: =============================================================================================================================
:: 2. EXTRAS RENAMER
:: =============================================================================================================================
echo "%~p1" | find /i "extras" >nul || goto skip_extras
echo "%~p1" | find /i "featurettes" >nul || goto skip_extras
if not defined parentdir echo detected featurette but parentdir was not delivered && exit /b
call :extrapolate "%parentdir%"
ren "%filename%%~x1" "%name% extras - %filename%%~x1" && echo renamed extra "%name% extras - %filename%%~x1" && exit /b
:skip_extras
:: =============================================================================================================================
:: 3. EXTRAPOLATOR
:: =============================================================================================================================
:extrapolate
set "type=" & set "year=" & set "ss=" & set "ep=" & set "episode="
set "name=%filename%"
echo "%name%"| find " " >nul || set "name=%name:.= %" & rem if no spaces then then safer to replace dots
:: episode number: last tag first
SETLOCAL enabledelayedexpansion
FOR /L %%e IN (100,1,199) DO (
SET "modname=!name:E%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E%%e=& set ep=E%%e& set type=show& rem !"
GOTO gotep
)
)
FOR /L %%e IN (10,1,99) DO (
SET "modname=!name:E%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E%%e=& set ep=E%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:E0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
)
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:E0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:E00%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E00%%e=& set ep=E00%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:%%eof=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:%%eof=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:x0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:x0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
)
ENDLOCAL
GOTO skipep
:gotep
%setcmd%
ENDLOCAL & SET "type=%type%" & SET "ep=%ep%" & SET name=%name%
:skipep
:: season number: second to last tag second
SETLOCAL enabledelayedexpansion
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:S0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:S0%%e=& set ss=S0%%e& set type=show& rem !"
GOTO gotss
)
)
FOR /L %%e IN (10,1,99) DO (
SET "modname=!name:S%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:S%%e=& set ss=S%%e& set type=show& rem !"
GOTO gotss
)
)
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:series.%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:series.%%e=& set ss=S0%%e& set type=show& rem !"
GOTO gotss
)
)
ENDLOCAL
GOTO skipss
:gotss
%setcmd%
ENDLOCAL & SET "type=%type%" & SET "ss=%ss%" & SET name=%name%
:skipss
if defined ep if not defined ss set ss=S01
SET "episode=%ss%%ep%"
:: year number: first tag last
SETLOCAL enabledelayedexpansion
FOR /L %%e IN (1957,1,2057) DO (
SET "modname=!name:%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:%%e=& set year=%%e& if not defined type set type=film& rem !"
GOTO gotyear
)
)
ENDLOCAL
GOTO skipyear
:gotyear
%setcmd%
ENDLOCAL & SET "year=%year%" & SET "type=%type%" & SET name=%name%
:skipyear
:: finalize
:: changes for general use
set name=%name: - = %
:: trim last useless letter from name
set "name=%name%endtrimmer"
set "name=%name:(endtrimmer=endtrimmer%"
set "name=%name:[endtrimmer=endtrimmer%"
set "name=%name: endtrimmer=endtrimmer%"
set "name=%name: endtrimmer=%"
set "name=%name:_endtrimmer=%"
set "name=%name:endtrimmer=%"
:: set strings
if "%type%"=="film" set "strict=%name: =?%?(%year%)"
if "%type%"=="show" if defined year set "strict=%name: =?%?(%year%)?%episode%"
if "%type%"=="show" if not defined year set "strict=%name: =?%?%episode%"
if "%type%"=="show" if defined year set "loose=%name: =?%?(%year%)?s"
if "%type%"=="show" if not defined year set "loose=%name: =?%?s"
SETLOCAL enabledelayedexpansion
FOR %%a IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO SET "name=!name:%%a=%%a!"
endlocal & SET "name=%name%"
:: declare (disable never delete)
if "%type%"=="film" echo extrapolated film %strict:?= %
if "%type%"=="show" echo extrapolated episode %strict:?= %
:: =============================================================================================================================
:: 4. FILEBOT MODERATOR
:: =============================================================================================================================
if not defined type exit /b
:: prep fake temp file
set tempdir=%temp%\%date:~3,2%%date:~6,2%%date:~11,2%%time:~1,1%%time:~3,2%%time:~6,2%%time:~9,2%
mkdir "%tempdir%" && copy /y nul "%tempdir%\%filename%.mkv" >nul
:: variables (to shorten lines)
if "%type%"=="film" set cf=--db TheMovieDb --format "{n} ({y}) {director} {genres} r{rating} {vf} {vc} {ac} {imdbid}"
if "%type%"=="show" set cf=--db TheTVDb --format "{n} {s00e00} {t} {vf} {vc} {ac} {imdbid}"
:: rename fake temp file
filebot -rename "%tempdir%\%filename%%~x1" --q "%name% %year%" %cf% >nul 2>nul
:: new name into variable
for /r "%tempdir%" %%i in (*) do set "new=%%~ni"
:: declare failure
if "%filename%"=="%new%" echo filebot refused to rename "%~n1" && exit /b
:: if what expected rename 4real
echo %%new:^&=^^^&%% | findstr /i /b /c:"%strict:?= %" >nul && goto 4real
:: if not reformat and try again
set "new=%new:'=%"
set "new=%new:,=%"
echo %%new:^&=^^^&%% | findstr /i /b /c:"%strict:?= %" >nul && goto 4real
:: declare failure and confirm override
echo "%new%" does not match "%string%"
choice /t 20 /c yn /n /d n /m "override and approve rename? [Y]es or [N]o: "
if "%errorlevel%"=="1" goto 4real
exit /b
:4real
filebot -rename "%~dp1%filename%%~x1" --q "%name% %year%" %cf% >nul 2>nul
if not exist "%filename%" echo successfully renamed "%filename%"
exit /b
none of the variables are carried over to the script that called for them. I can't for the life of me figure out why except it must have something to do with endlocal or disabledelayedexpansion, which look fine to me as far as I can tell.

Related

The code is remove the empty lines from the file from .c files and .ini files

This code is used to replace a string within the file:
#echo off
Set _PathtoFile=D:\headercpp\revs.ini
Set _OldLine=subminor=5
Set _NewLine=subminor=6
:: End of Search parameters
Call :_Parse "%_PathtoFile%"
Set _Len=0
Set _Str=%_OldLine%
Set _Str=%_Str:"=.%987654321
:_Loop
If NOT "%_Str:~18%"=="" Set _Str=%_Str:~9%& Set /A _Len+=9& Goto _Loop
Set _Num=%_Str:~9,1%
Set /A _Len=_Len+_Num
PushD %_FilePath%
If Exist %_FileName%.new Del %_FileName%.new
If Exist %_FileName%.old Del %_FileName%.old
Set _LineNo=0
For /F "Tokens=* Eol=" %%I In (%_FileName%%_FileExt%) Do (
Set _tmp=%%I
Set /A _LineNo+=1
If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (
>>%_FileName%.new Echo %_NewLine%
) Else (
If !_LineNo! GTR 1 If "!_tmp:~0,1!"=="[" Echo.>>%_FileName%.new
SetLocal DisableDelayedExpansion
>>%_FileName%.new Echo %%I
EndLocal
))
Ren %_FileName%%_FileExt% %_FileName%.old
Ren %_FileName%.new %_FileName%.ini
::call :_check
if /i exist "%~1\" (set "Folder=%~1") else (exit)
PopD
Goto :EOF
:_Parse
Set _FilePath=%~dp1
Set _FileName=%~n1
Set `_FileExt`=%~x1
::Set _UpdatedFileName=Newtextupdated
Goto :EOF
However, the resulting output does not preserve empty lines.
How do I leave the empty lines in place?

Echo output for each value in given list in bat file

With below code I'm struggling to print calculation for each value in a given list. Currently it prints total combined time for both users, I want to echo individual time for each user which is listed in 2nd row of this code.\
Any help will be appreciated.
#Echo off
For %%U in (a3rgcw shukla) Do (
PushD "H:\Syslogs\" ||(Echo couldn't find dir & Pause & Exit /B 1)
Set "TotalSecs=0"
For %%F in ("*%U%*.txt") Do For /F "delims=" %%A in ('
findstr /I "system.log.created End.of.session" "%%F"
') Do (
Set "Flag="
Echo=%%A|findstr /I "system.log.created" 2>&1>Nul && Set "Flag=Start"
if defined Flag (
FOR /F "tokens=11" %%T in ("%%A") Do Call :TimeToSecs Start "%%T"
) Else (
FOR /F "tokens=8" %%T in ("%%A") Do Call :TimeToSecs Stop "%%T"
)
)
Echo TotalDuration for %%U:%TotalDur%
)
Echo:
PopD
Goto :Eof
:TimeToSecs
Set "%1_HMS=%~2"
Echo:%~2|Findstr "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]" 2>&1>Nul || (Echo wrong format %2&Goto :Eof)
For /F "tokens=1-3 delims=:" %%H in ("%~2"
) Do Set /A "%1=(1%%H-100)*60*60+(1%%I-100)*60+(1%%J-100)"
If %1 neq Stop Goto :Eof
Set /A "Diff=Stop-Start,TotalSecs+=Diff"
Call :Secs2HMS Dur %Diff%
Call :Secs2HMS TotalDur %TotalSecs%
::Echo Session from %Start_HMS% to %Stop_HMS% Duration:%Dur% TotalDuration:%TotalDur%
Goto :Eof
:Secs2HMS var value
setlocal
set /a "HH=%2/3600,mm=(%2-HH*3600)/60+100,ss=%2 %% 60+100"
Set "HHmmss= %HH%:%mm:~-2%:%ss:~-2%"
endlocal&set "%1=%HHmmss:~-10%
Goto :Eof
Current output:
TotalDuration for a3rgcw: 7:15:00
TotalDuration for shukla: 7:15:00
Desired output:
TotalDuration for a3rgcw: 5:15:00
TotalDuration for shukla: 2:00:00
Sample file named shukladfdf for 2nd user:
sdsdf system log created on ghg Thursday, 9 August 2018, 20:30:45 on India
Standard Time
dfg
drdwewed
end of session as 9 August 2018, 22:30:45 on India Standard Time
#Echo off
Setlocal
PushD "H:\Syslogs\" ||(Echo couldn't find dir & Pause & Exit /B 1)
For %%U in (a3rgcw shukla) Do (
Set "TotalDur="
Set "TotalSecs=0"
For %%F in ("*%%U*.txt") Do For /F "delims=" %%A in ('
findstr /I "system.log.created End.of.session" "%%F"
') Do (
Set "FileName=%%F"
Set "Flag="
Echo=%%A|findstr /I "system.log.created" 2>&1>Nul && Set "Flag=Start"
if defined Flag (
FOR /F "tokens=11" %%T in ("%%A") Do Call :TimeToSecs Start "%%T"
) Else (
FOR /F "tokens=8" %%T in ("%%A") Do Call :TimeToSecs Stop "%%T"
)
)
Set "UsrName=%%U: "
Call :Print UsrName
)
Echo:
PopD
Goto :Eof
:Print
If /i "%~1" == "UsrName" (
Echo TotalDuration for %UsrName:~,10% %TotalDur%
) else If /i "%~1" == "FileName" (
Echo Session (%FileName%^) from %Start_HMS% to %Stop_HMS% Duration:%Dur% TotalDuration:%TotalDur%
)
Goto :Eof
:TimeToSecs
Set "%1_HMS=%~2"
Echo:%~2|Findstr "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]" 2>&1>Nul || (Echo wrong format %2&Goto :Eof)
For /F "tokens=1-3 delims=:" %%H in ("%~2"
) Do Set /A "%1=(1%%H-100)*60*60+(1%%I-100)*60+(1%%J-100)"
If %1 neq Stop Goto :Eof
Set /A "Diff=Stop-Start,TotalSecs+=Diff"
Call :Secs2HMS Dur %Diff%
Call :Secs2HMS TotalDur %TotalSecs%
Call :Print FileName
Goto :Eof
:Secs2HMS var value
setlocal
set /a "HH=%2/3600,mm=(%2-HH*3600)/60+100,ss=%2 %% 60+100"
Set "HHmmss= %HH%:%mm:~-2%:%ss:~-2%"
endlocal&set "%1=%HHmmss:~-10%"
Goto :Eof
Changed %U% to %%U.
Changed Echo TotalDuration for %%U:%TotalDur% to
Call Echo TotalDuration for %%U:%%TotalDur%% which
delays expansion until time of execution, instead of
parse time.
Added missing double quote with 2nd last line to close.
Added Setlocal to top of script as perhaps a 2nd run
of the script in the same CMD session could set
predined variables with values.
Added label :Print to echo the output to avoid
use of delayed expansion.

How to split String with cmd bat using delimiter as entire word

How to split string with the delim=string
E.g:
split string
"sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz"
by
delim="MYSTR"
and result should be:
sssssMssss
___M___
zzzzzzMzzzz
Such code
for /f "tokens=1,2 delims=MYSTR" %%A in ("sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz") do (
set fn1=%%A
)
doesn't work/ It splits only using first letter by 'M'
How to split by word?
#echo off
setlocal EnableDelayedExpansion
set "string=sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz"
rem Do the split:
set i=1
set "fn!i!=%string:MYSTR=" & set /A i+=1 & set "fn!i!=%"
set fn
Output:
fn1=sssssMssss
fn2=___M___
fn3=zzzzzzMzzzz
You may review this topic for a detailed explanation on the method used...
try with split.bat (you can use both as a function or a separate bat file (does not handle well !,% and ")) :
#echo off
setlocal
call :split "sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz" MYSTR 1 spl1
echo %spl1%
call :split "sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz" MYSTR 2 spl2
echo %spl2%
call :split "sssssMssssMYSTR___M___MYSTRzzzzzzMzzzz" MYSTR 3
rem echo %spl2%
endlocal & exit /b %errorlevel%
:split [%1 - string to be splitted;%2 - split by;%3 - possition to get; %4 - if defined will store the result in variable with same name]
::http://ss64.org/viewtopic.php?id=1687
setlocal EnableDelayedExpansion
set "string=%~2%~1"
set "splitter=%~2"
set /a position=%~3
set LF=^
rem ** Two empty lines are required
echo off
for %%L in ("!LF!") DO (
for /f "delims=" %%R in ("!splitter!") do (
set "var=!string:%%~R%%~R=%%~L!"
set "var=!var:%%~R=%%~L!"
if "!var!" EQU "!string!" (
echo "%~1" does not contain "!splitter!" >&2
exit /B 1
)
)
)
if "!var!" equ "" (
endlocal & if "%~4" NEQ "" ( set "%~4=")
)
if !position! LEQ 0 ( set "_skip=" ) else (set "_skip=skip=%position%")
for /f "eol= %_skip% delims=" %%P in ("!var!") DO (
if "%%~P" neq "" (
set "part=%%~P"
goto :end_for
)
)
set "part="
:end_for
if not defined part (
endlocal
echo Index Out Of Bound >&2
exit /B 2
)
endlocal & if "%~4" NEQ "" (set %~4=%part%) else echo %part%
exit /b 0

Why is an exclamation mark not passed when calling other script?

This is the output of my two scripts. The first one calls the other and delivers a filename as %1
D:\VIDEOS\SHORTS>for %v in (*) do call namer "%v"
D:\VIDEOS\SHORTS>call namer "10 Products You’ll Never Buy Again Knowing How They Are Made!.mp4"
D:\VIDEOS\SHORTS>echo "10 Products You’ll Never Buy Again Knowing How They Are Made.mp4" | find /i ".tmp" 1>nul && exit /b
The echo uses "%~nx1".
My question is: Why does the exclamation mark disappear?
This started happening yesterday, but I'm not sure what I could've done to cause this. It's part of a massive batch script cluster, all of which I can't post here due to size limits so ask for additional details or generalize.
edit: requested file namer.cmd
set "full=" & set "file=" & set "name=" & set "type=" & set "year=" & set "episode=" & set "strict=" & set "loose="
echo "%~nx1" | find /i ".tmp" >nul && exit /b
set "file=%~n1"
:: =============================================================================================================================
:: 1. file VALIDATOR
:: =============================================================================================================================
set "original=%file%"
set "file=%file:!=%"
set "file=%file:#=%"
set "file=%file: _ = - %"
set "file=%file:’='%"
set "file=%file:“='%"
set "file=%file:”='%"
set "file=%file:[=(%"
set "file=%file:]=)%"
set "file=%file:_= %"
set "file=%file:—=-%"
set "file=%file: = %"
set "file=%file: = %"
set "file=%file:&=and%"
:: exclamation point has to be removed before we enable delayed expansion
if not "%file%"=="%original%" ren "%~1" "%file%%~x1" 2>nul && echo validated "%file%%~x1"
if not "%file%"=="%original%" if exist "%original%" ren "%~1" "%file% - copy%~x1" && echo validated "%file% - copy%~x1"
if not "%file%"=="%original%" if exist "%original%" ren "%~1" "%file% - copy 2%~x1" && echo validated "%file% - copy 2%~x1"
setlocal enabledelayedexpansion
set "original=!file!"
set "file=!file:%%=_percent!"
if not "%file%"=="%original%" ren "%~dp1%original%%~x1" "for /f "delims=" %%v in ('where /r "d:\videos" "%strict%*" 2^>nul') do" && echo validated "%original%"
set "original=!file!"
:: this needs to 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 "%~dp1%original%%~x1" "%file%%~x1" && echo validated "%original%"
:: =============================================================================================================================
:: 2. EXTRAPOLATOR
:: =============================================================================================================================
:extrapolate
set "type=" & set "year=" & set "ss=" & set "ep=" & set "episode="
set "name=%file%"
echo "%name%"| find " " >nul || set "name=%name:.= %" & rem if no spaces then then safer to replace dots
:: if short do complete skip of movie/series extrapolator breakdown
echo "%~n1" | find /i "trailer" >nul && set "type=misc" && goto skipyear
echo "%~p1" | find /i "\shorts\" >nul && set "type=misc" && goto skipyear
echo "%~p1" | find /i "\movies\" >nul && set "type=film" && goto skipss
echo "%~p1" | find /i "\series\" >nul && set "type=show"
:: episode number: last tag first
FOR /L %%e IN (100,1,199) DO (
SET "modname=!name:E%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E%%e=& set ep=E%%e& set type=show& rem !"
GOTO gotep
)
)
FOR /L %%e IN (10,1,99) DO (
SET "modname=!name:E%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E%%e=& set ep=E%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:E0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
)
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:E0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:E00%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:E00%%e=& set ep=E00%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:%%eof=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:%%eof=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
SET "modname=!name:x0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:x0%%e=& set ep=E0%%e& set type=show& rem !"
GOTO gotep
)
)
GOTO skipep
:gotep
%setcmd%
:skipep
:: season number: second to last tag second
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:S0%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:S0%%e=& set ss=S0%%e& set type=show& rem !"
GOTO gotss
)
)
FOR /L %%e IN (10,1,99) DO (
SET "modname=!name:S%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:S%%e=& set ss=S%%e& set type=show& rem !"
GOTO gotss
)
)
FOR /L %%e IN (0,1,9) DO (
SET "modname=!name:series.%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:series.%%e=& set ss=S0%%e& set type=show& rem !"
GOTO gotss
)
)
GOTO skipss
:gotss
%setcmd%
:skipss
if defined ep if not defined ss set ss=S01
set "episode=%ss%%ep%"
:: year number: first tag last (first with brackets so not to confuse with imdb tag)
FOR /L %%e IN (1960,1,2020) DO (
SET "modname=!name:(%%e)=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:(%%e)=& set year=%%e& if not defined type set type=film& rem !"
GOTO gotyear
)
)
FOR /L %%e IN (1960,1,2020) DO (
SET "modname=!name:%%e=!"
IF "!modname!" neq "%name%" (
SET "setcmd=set name=!name:%%e=& set year=%%e& if not defined type set type=film& rem !"
GOTO gotyear
)
)
GOTO skipyear
:gotyear
%setcmd%
:skipyear
if not defined type set type=misc
:: other trimming for misc etc
set name=%name: - copy=&rem %
set name=%name: (1)=&rem %
set name=%name: (2)=&rem %
set name=%name: (3)=&rem %
set name=%name: [1]=&rem %
set name=%name: [2]=&rem %
set name=%name:360p=&rem %
set name=%name:480p=&rem %
set name=%name:720p=&rem %
set name=%name:1080p=&rem %
set name=%name:2160p=&rem %
:: changes for general use
set "name=%name: - = %"
set "name=%name: = %"
:: trim last useless letter from name
set "name=%name%endtrimmer"
set "name=%name:(endtrimmer=endtrimmer%"
set "name=%name:[endtrimmer=endtrimmer%"
set "name=%name:.endtrimmer=endtrimmer%"
set "name=%name:-endtrimmer=endtrimmer%"
set "name=%name: endtrimmer=endtrimmer%"
set "name=%name: endtrimmer=%"
set "name=%name:_endtrimmer=%"
set "name=%name:endtrimmer=%"
:: set strings
if "%type%"=="misc" set "strict=%name: =?%"
if "%type%"=="film" if defined year set "strict=%name: =?%?(%year%)"
:: caution: this allows movie to be renamed without year tag
:: if "%type%"=="film" if not defined year set "strict=%name: =?%"
if "%type%"=="show" if defined year set "strict=%name: =?%?(%year%)?%episode%"
if "%type%"=="show" if not defined year set "strict=%name: =?%?%episode%"
if "%type%"=="show" if defined year set "loose=%name: =?%?(%year%)?s"
if "%type%"=="show" if not defined year set "loose=%name: =?%?s"
FOR %%a IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO SET "name=!name:%%a=%%a!"
endlocal & set "full=%~dp1%file%%~x1" & set "file=%file%%~x1" & set "name=%name%" & set "type=%type%" & set "year=%year%" & set "episode=%ss%%ep%" & set "strict=%strict%" & set "loose=%loose%"
:: declare (disable never delete)
echo extrapolated %type% "%strict:?= %"
:: =============================================================================================================================
:: 3. FILEBOT MODERATOR
:: =============================================================================================================================
:: if rename not needed exit
if "%type%"=="misc" exit /b
if "%~2"=="-norename" exit /b
:: if already renamed we are done here
echo "%file%" | find /i " tt" >nul && exit /b
:: prep fake temp file
set tempdir=%temp%\%date:~3,2%%date:~6,2%%date:~11,2%%time:~1,1%%time:~3,2%%time:~6,2%%time:~9,2%
mkdir "%tempdir%" && copy /y nul "%tempdir%\%file%" >nul
:: variables (to shorten lines)
if "%type%"=="film" set cf=--db TheMovieDb --format "{n} ({y}) {director} {genres} r{rating} {vf} {vc} {ac} {imdbid}"
if "%type%"=="show" set cf=--db TheTVDb --format "{n} {s00e00} {t} {vf} {vc} {ac}-mallSanta"
:: rename fake temp file
filebot -rename "%tempdir%\%file%" --q "%name% %year%" %cf% >nul 2>nul
:: new name into variable
for /r "%tempdir%" %%i in (*) do set "new=%%~nxi"
:: declare failure
if "%file%"=="%new%" echo failed, reason: && filebot -rename "%tempdir%\%file%" --log warning --q "%name% %year%" %cf% & exit /b
:: check all strings against renamed orphan fake file and if successful rename 4real
for /f "delims=" %%v in ('where "%tempdir%:%strict%*" 2^>nul') do goto 4real
for /f "delims=" %%v in ('where "%tempdir%:%strict:s?='s?%*" 2^>nul') do goto 4real
for /f "delims=" %%v in ('where "%tempdir%:%strict:?-?=?%*" 2^>nul') do goto 4real
:: declare failure and confirm override
echo.
echo file "%file%" was renamed
echo to a "%new%", which was unexpected,
:: try the interactive command prompt mode in filebot?
choice /t 20 /c yn /n /d n /m "override and approve this rename? [Y]es or [N]o: "
if "%errorlevel%"=="1" goto 4real
exit /b
:4real
filebot -rename "%~dp1%file%" --q "%name% %year%" %cf% >nul 2>nul
if not exist "%~dp1%file%" echo successfully renamed "%file%"
:: validate the new file (maybe just send to validate with a -norename? takes longer but clearer code
set "original=%file%"
set "file=%file:!=%"
set "file=%file:#=%"
set "file=%file: _ = - %"
set "file=%file:’='%"
set "file=%file:“='%"
set "file=%file:”='%"
set "file=%file:_= %"
set "file=%file:—=-%"
set "file=%file: = %"
set "file=%file: = %"
set "file=%file:&=and%"
if not "%file%"=="%original%" ren "%~1" "%file%" && echo validated "%original%"
setlocal enabledelayedexpansion
set "original=!file!"
set "file=!file:%%= percent!"
if not "%file%"=="%original%" ren "%~dp1%original%%~x1" "%file%" && echo validated "%original%"
endlocal
exit /b
it (1.) removes problematic characters from the filename (2.) from filename extrapolates video name, year, other variables, then (3.) does a supervised filebot rename. the setlocal and endlocal bits look good. could it be the problem is inherited from yet another script?
You have enabled delay environment variable expansion with setlocal EnableDelayedExpansion somewhere above the ECHO line in batch script namer.
Therefore the exclamation mark is not further interpreted as literal character, but as begin/end of a variable reference expanded delayed.
We would need the batch code of namer batch file to help you solving this issue.
It looks like the line is for checking if the file contains .tmp, i.e. has the file extension .tmp and if this is the case, ignore the file.
I suggest to change in your first batch file
for %%v in (*) do call namer "%%v"
to
for %%I in (*) do if /I not "%%~xI" == ".tmp" call "%~dp0namer.cmd" "%%I"
Then files with file extension .tmp are not processed at all by namer batch file.
Run in a command prompt window set /? for more information about delayed expansion and for /? for help on %~xI (file extension of file) as well as if /? for help on command IF and call /? for help on %~dp0.
It is also possible to use in namer.cmd:
if /I "%~x1" == ".tmp" goto :EOF
This exits processing of the batch file namer.cmd if the file extension of first argument is .tmp (compared case-insensitive). For this comparison it does not matter if delayed expansion is enabled or not. See also answer on Where does GOTO :EOF return to?
If .tmp is not the file extension of the file, but is just somewhere within file name, it is possible to use an IF with string substitution.
Example for batch file namer.cmd.
set "FileName=%~nx1"
if not "%FileName:.tmp=%" == "%FileName%" goto :EOF
The first line assigns file name with extension but without path to environment variable FileName.
The second line compares case-sensitive the file name string in which all occurrences of .tmp are replaced case-insensitive with an empty string with the file name string not modified at all. The two compared strings are not equal if the file name string really contains .tmp 1 or more times resulting in exiting processing of the batch file.
In other words the IF condition checks if a string assigned to an environment variable contains case-insensitive the string specified between : and =.
That is similar to using in C:
#include <string.h>
int main(int argc, char *argv[])
{
if(argc > 1)
{
/* Does first argument contain the string ".tmp"? */
if(strstr(argv[1],".tmp") != NULL) return 1;
}
return 0;
}
Function strstr is case-sensitive.
here is a method where you can use delayedexpansion without the use of escape characters.
#echo off
setlocal disabledelayedexpansion
set myfile="10 Products You’ll Never Buy Again Knowing How They Are Made!.mp4"
echo %DB_password%
setlocal enabledelayedexpansion
echo !myfile!

Multiplying error: * was unexpected at this time

I recently got a problem. I am making a game module in batch and I got a strange error:
Multiplying error "*!Map_PlayerLengthX! was unexpected at this time"
This is the whole code:
#echo off
setlocal enabledelayedexpansion
pause >nul
cls
call :Map_DefinePlayer 4 2 loloolol
echo %Px1y1%%Px2y1%%Px3y1%%Px4y1%
echo %Px1y2%%Px2y2%%Px3y2%%Px4y2%
pause >nul
:::::::::::::::::::::::::::
:: Map v0.10 By KKZiomek ::
:::::::::::::::::::::::::::
:Map_Init
set Map_Running=1
goto :eof
:Map_Load
if !Map_Running!==0 goto :eof
set Map_Load_FileToLoad=%~1
set Map_Load_BorderX=%~2
set Map_Load_BorderY=%~3
set Map_Load_BChar=%~4
set Map_Load_LineTotal=0
for /f "tokens=* delims= usebackq" %%l in (!Map_Load_FileToLoad!) do (
set /a Map_Load_LineTotal+=1
set Map_Line!Map_Load_LineTotal!=%%l
)
:Map_Load_ApplyCoords
for /l %%g in (1,1,!Map_Load_LineTotal!) do (
call :Map_Load_StrLen Map_Line%%g Map_Line%%g_Len
set /a Map_Load_ApplyCoords_DecidedLen+=!Map_Line%%g_Len!
)
set /a Map_Load_ApplyCoords_DecidedLen/=!Map_Load_LineTotal!
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set x%%xy%%y=!Map_Line%%y:~%%x,1!
)
)
goto :eof
:Map_Load_StrLen
setlocal disabledelayedexpansion
set Map_Load_StrLen_Len=0
if defined %~1 for /f "delims=:" %%n in (
'"(cmd /v:on /c echo(!%~1!&echo()|findstr /o ^^"'
) do set /a "Map_Load_StrLen_Len=%%n-3"
endlocal & if "%~2" neq "" (set %~2=%Map_Load_StrLen_Len%) else echo %Map_Load_StrLen_Len%
exit /b
:Map_Display
cls
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
set Map_Display_Line%%y=
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set Map_Display_Line%%y=!Map_Display_Line%%y!!x%%xy%%y!
)
)
for /l %%z in (1,1,!Map_Load_LineTotal!) do (
echo !Map_Display_Line%%z!
)
goto :eof
:Map_ReloadPos
set XPos=%~1
set YPos=%~2
set x!XPos!y!YPos!=!Map_Line%YPos%:~%XPos%,1!
goto :eof
:Map_DefinePlayer
set Map_PlayerLengthX=%~1
set Map_PlayerWidthY=%~2
set Map_DefinePlayer_Scheme=%~3
set /a Map_DefinePlayer_Modifier=!Map_PlayerLengthX!-1
for /l %%p in (1,1,!Map_PlayerWidthY!) do (
for /l %%q in (0,1,!Map_DefinePlayer_Modifier!) do (
set /a localq=%%q+1
set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
set Px%localq%y%%p=!Map_DefinePlayer_Scheme:~%modq%,1!
)
)
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
I get the error in the function :Map_DefinePlayer. I think it's mainly in this line: set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
Every function works fine instead of this function because of this weird multiplying error. I tried enbling delayed expansion again, changing !Map_PlayerLengthX! into %Map_PlayerLengthX% but then it only changed the error in *4 was unexpected at this time
Anyone has an idea what causes this and how to fix it?
try with :
set /a "modq=%%q+((%%p-1)*!Map_PlayerLengthX!)"
the brackets in the expression are taken as closing brackets for the FOR loop

Resources