1

I have this script, which works:

  FOR /F "delims=\" %%i IN ('dir /b /ad-h /o-d') DO (
    SET a=%%i
    GOTO :found
)
echo No subfolder found
goto :eof

:found
echo Most recent video created: &echo. & echo."%a%"
if /i "%a:~-3%"=="ESP" goto:next else goto:GBR
:GBR
if /i "%a:~-3%"=="GBR" goto:next else goto:SPE
:SPE
if /i "%a:~-3%"=="SPE" goto:next 

cd %a%
for %%a in (*) do rename "%%a" "%%~na-%a%%%~xa"
cd ..\
ren "%a%" "%a% - GBR"
echo.
echo %a% video has been processed
echo.
pause
exit /b

:next
echo.
echo. %a% Video already processed
echo.
pause

:exit
exit /b

I have tried to get an IF ELSE working however I cannot. I have had to use labels but it is not a clean way if I were to expand the search criteria.

Am I missing something?

This is what I tired, which does NOT work:

echo Most recent video created: &echo. & echo."%a%"
if /i "%a:~-3%"=="ESP" goto:next else 
if /i "%a:~-3%"=="GBR" goto:next else
if /i "%a:~-3%"=="SPE" goto:next
Nick
  • 1,417
  • 1
  • 14
  • 21
Arthor
  • 666
  • 2
  • 13
  • 40

2 Answers2

2

Try not to use a variable name similar to one already assigned with %something%:

for %%b in (*) do rename "%%a" "%%~nb-%a%%%~xb"

Update: (May not work. Just a concept)

setlocal enabledelayedexpansion

FOR /F "delims=\" %%i IN ('dir /b /ad-h /o-d') DO (
    SET a=%%i
    GOTO :found
)
echo No subfolder found
goto :eof

:found
echo Most recent video created: &echo. & echo."!a!"
if /i "%a:~-3%"=="ESP" goto:next else goto:GBR
:GBR
if /i "%a:~-3%"=="GBR" goto:next else goto:SPE
:SPE
if /i "%a:~-3%"=="SPE" goto:next 

cd !a!
for %%b in (*) do rename "%%b" "%%~nb-!a!%%~xb"
cd ..\
ren "!a!" "!a! - GBR"
echo.
echo !a! video has been processed
echo.
pause
exit /b

:next
echo.
echo. !a! Video already processed
echo.
pause

:exit
exit /b
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • And wow this is actually knew to me: `"%a:~-3%"`. I don't know what it does. – konsolebox Jul 09 '14 at 18:57
  • Looks at the last 3 characters? I am still trying to work it out. – Arthor Jul 09 '14 at 19:18
  • @Arthor Yeah it's kind of like 8 or 9 years already since the last time I did cmd scripting. Reading the help infos a couple of times I must have known that only that I forgot it already :) – konsolebox Jul 09 '14 at 19:24
  • @Arthor How do you assign the values for %a%? – konsolebox Jul 09 '14 at 19:25
  • I have provided the full code for that part. Thanks – Arthor Jul 09 '14 at 19:27
  • @Arthor Seeing your last edit, I think enabling delayed expansion can help you. See http://stackoverflow.com/questions/6679907/setlocal-and-enabledelayedexpansion-usage-question. – konsolebox Jul 09 '14 at 19:28
  • @Arthor I did used that form before when I created multi-script applications like an automatic downloader, and a hidden bot. You can check my update and see if some of it may work. – konsolebox Jul 09 '14 at 19:33
0
if /i "%a:~-3%"=="ESP" goto:next else goto:GBR
:GBR

the else goto:GBR is not needed here, because, if the comparison is not met, the script would continue with the next line anyway:

if /i "%a:~-3%"=="ESP" goto:next 
if /i "%a:~-3%"=="GBR" goto:next 
if /i "%a:~-3%"=="SPE" goto:next
echo found none of them.
Exit /b
:next
echo found one of them.

If you want to use else, the command has to be on the same line (the ( works as "start of a commandblock" and has to be on the same line than else):

if /i "%a:~-3%"=="ESP" goto:next else (
  if /i "%a:~-3%"=="GBR" goto:next else (
    if /i "%a:~-3%"=="SPE" goto:next
  )
)
:next

if fact, all of if, <comparison>, <command>, else and <command> have to be on the same line. You can outsmart the parser by using (blocks) instead of single commands:

if a == a (
  echo they are the same.
  echo yes, they are.
) else (
  echo they are different,
  echo not the same.
)
Stephan
  • 53,940
  • 10
  • 58
  • 91