2
votes

Let me first explain what I am trying to accomplish: I have a GoPro camera and when recording multiple time-lapses you end up with a big pile of img files, two time-lapses are divided by a number in the file name. Gxxxyyyy is how the files are labeled, the y stands for which time-lapse the picture belongs to and x what picture in the time-lapse.

I am trying to create a program that puts every time-lapse in a different folder (sorting by the numbers on the x position); I am using batch because I already had some experience with it.

This is the program I wrote:

@echo off
set loop=0
set fn=1
set session=1

:sort
if %loop% LSS 1000 (

if %fn% GTR 99 (

    ROBOCOPY D:\Bureaublad\Output\Pictures D:\Bureaublad\Output\Pictures\Session%session% G%fn%*.JPG
    if %ERRORLEVEL% EQU 1 set /A session=%session%+1
)
if %fn% LSS 10 (

    ROBOCOPY D:\Bureaublad\Output\Pictures D:\Bureaublad\Output\Pictures\Session%session% G00%fn%*.JPG
    if %ERRORLEVEL% EQU 1 set /A session=%session%+1        
) ELSE (

    if %fn% LSS 100 (
ROBOCOPY D:\Bureaublad\Output\Pictures D:\Bureaublad\Output\Pictures\Session%session% G0%fn%*.JPG
    if %ERRORLEVEL% EQU 1 set /A session=%session%+1
    )
)

set /A fn=%fn%+1
set /A loop=%loop%+1
goto sort
)

I was trying to use the errorlevel value to determine if the copy was successful, because if it was the next copy had to be to another folder which is what i used the session variable for. But it always copies everything into the "session1" folder and not into separate folders even though i have files with different numbers on the x position.

I tried figuring out what the problem was and used "echo %ERRORLEVEL%" followed by a pause, right after the robocopy commands and every time it just said 0 even though files were copied.

That is the issue and I can't figure out why the errorvalue doesn't change.

3
An errorlevel of 0 indicates success.indiv
@indiv according to this page an errorvalue of 1 is what I am trying to acomplish: ss64.com/nt/robocopy-exit.htmlrwk1997
I see, thank you for the clarification.indiv
ERRORLEVEL is a built-in value, not an environmental variable, so you don't need the % around it. IF ERRORLEVEL 1 is the usual way you test for it being set; note there are no % and it doesn't use EQU.Ken White
@KenWhite Thanks a lot!, this solved the problemrwk1997

3 Answers

2
votes

The problem is that you try to expand errorlevel inside of a block.

The percent expansion is done by the parser when the block is parsed, before the block is executed.

You should change the expansion to delayed expansion.

@echo off
setlocal EnableDelayedExpansion
set loop=0
set fn=1
set session=1

:sort
if %loop% LSS 1000 (

    if !fn! GTR 99 (
        ROBOCOPY D:\Bureaublad\Output\Pictures D:\Bureaublad\Output\Pictures\Session!session! G!fn!*.JPG
        if !ERRORLEVEL! EQU 1 set /A session=session+1
    )
....
)
1
votes

ERRORLEVEL is a built-in variable, not an environmental variable, and so you shouldn't use the percent signs (%) around it, and simply test directly for the value. The proper way to use it is simply

IF ERRORLEVEL 1 DoSomething
0
votes

Please see robocopy exit codes

look at this:

Bugs
Version XP026 returns a success errorlevel even when it fails.