John Boe
John Boe

Reputation: 3611

failture in CMD batch file

In Windows cmd batch, I try to call a batch file:

call ".\%%R\scenery\list bgl.bat" ".\%%R\scenery\"

In the "list bgl.bat" I have:

IF "%1"=="" ( dir *.bgl /b > list_bgl.txt ) ELSE  ( dir "%1*.bgl" /b > list_bgl.txt )

and it crashes somewhere in the place of condition. The if true part works OK (if I run the batch "list bgl.bat" directly). But If I run it from a batch file, so the else block fails and the script crashes. Do see where is problem?

Update:

T:\test\scenery>call ".\list bgl.bat"
T:\test\scenery>IF "" == "" (dir *.bgl /b   1>list_bgl.txt )  ELSE (dir "*.bgl" /b   1>list_bgl.txt )
T:\test\scenery>ECHO DONE
DONE

T:\test\scenery>pause
Press any key to continue

T:\test\scenery>call ".\list bgl.bat" ".\"
T:\test\scenery>IF ".\" == ""
(dir *.bgl /b   1>list_bgl.txt )  ELSE (dir ".\*.bgl" /b   1>list_bgl.txt )
T:\test\scenery>ECHO DONE
DONE

T:\test\scenery>pause
Press any key to continue
T:\test\scenery>

Well this works. Command parametr no. 1 was tested from cmd-line

EDIT2: When I call the main batch (from parent directory) I see no error, but the file is not created in the specified directory but in the main directory from where the main run was performed:

:

@echo off
cls
for /F "tokens=*" %%R in ('dir * /A:D /b') do (
echo ".\%%R\scenery\list bgl.bat"
call ".\%%R\scenery\list bgl.bat" ".\%%R\scenery\"
)
pause

EDIT3: Yeah, I have it. Problem here:

 > list_bgl.txt

I miss the path there... This works:

IF "%~1"=="" ( dir *.bgl /b > list_bgl.txt ) ELSE ( dir "%~1*.bgl" /b > "%~1list_bgl.txt" )

Upvotes: 0

Views: 221

Answers (1)

dbenham
dbenham

Reputation: 130819

You are already enclosing your parameter in quotes during the call, then you add a second set of quotes within "list bgl.bat". That can cause problems. If you want to add enclosing quotes within "list bgl.bat" then you need to first remove any possible existing enclosing quotes using %~1.

IF "%~1"=="" ( dir *.bgl /b > list_bgl.txt ) ELSE  ( dir "%~1*.bgl" /b > list_bgl.txt )

update
You have shown an example of what works, but we need to see what is not working. The quote / %~ issue was real, but it looks like you have something else that is causing problems. I'm curious what the run-time value of %%R is? I'm also surprised that the path to your called batch file can vary.

Upvotes: 4

Related Questions