Search code examples
batch-filecmdgoto

Batch Script Error: "goto was unexpected at this time"


Im getting the error in the script below when draging a file onto it. The error is "goto unexpected at this time" on the line goto CONTINUE.

I read that this error can in some cases be prevented by using enabledelayedexpansion however that doesnt apply here.

I also read that this error can occur when the errorlevel is not set. I found that the 2>nul pushd is setting the error level when %~1 is a file, so before the goto FILE im using the command cmd /c exit 0 to reset the error level. This did reset the error level however the error is still occuring.

Im hoping someone will be able to point out the error of my ways. Thanks!

@echo off
set rootDir=%~dp0
cd "%rootDir%"

del BatchTask.log

echo |TIME|FIND /i "current">>BatchTask.log
echo |DATE|FIND /i "current">>BatchTask.log

:CONTINUE
if %1 == "" goto END
set inDir="%~1"

if exist "%~1" (2>nul pushd "%~1" && (popd&goto FOLDER) || (cmd /c exit 0)&goto FILE)

:FILE
set outDir=%~dp1
IF %outDir:~-1%==\ (SET outDir=%outDir:~0,-1%)
goto CONVERT

:FOLDER
set outDir=%~1
goto CONVERT

:CONVERT
set outDir="%outDir%_Out"
shift
"%rootDir%\out\ReleaseWithSymbols\Converter.exe" %inDir% %outDir% 

goto CONTINUE

:END
cd "%rootDir%"

echo |TIME|FIND /i "current">>BatchTask.log
echo |DATE|FIND /i "current">>BatchTask.log

Solution

  • I think it is because of the "shift" call. I tried giving two arguments to this file and got error only for the second file.

    You could probably try putting the goto statement in an if condition. like:

    if NOT "%1" == "" goto CONTINUE