Page 1 of 1

was unexpected at this time???

Posted: 15 Apr 2012 08:17
by tinfanide

Code: Select all

@ECHO ON

SET base=3
SET power=4

:Control
FOR /L %%Z IN (1,1,%power%) DO CALL :CreateDirectories %%Z
PAUSE
GOTO :EOF


:CreateDirectories index
IF NOT EXIST %~dp0log.log (
   CALL :Make
) ELSE (
   SET /A times=%1-1
   CALL :CountLines
   CALL :ToPower %base% %times%
   SET /A skip=%lines%-%n%
   
   IF %skip% EQU 0 (
      FOR /F "tokens=*" %%A IN ("%~dp0log.log") DO (
      CD %%A
      CALL :Make
      )
   ) ELSE (
      FOR /F "tokens=*" %%A IN ('MORE +%skip% "%~dp0log.log"') DO (
      CD %%A
      CALL :Make
      )   
   
   )

)

GOTO :EOF


:Make
FOR /L %%B IN (1,1,%base%) DO (
   IF NOT EXIST %CD%\%%B (
      MD %%B
      ECHO %%~dpB%%B>>%~dp0log.log
   )
)
GOTO :EOF


:CountLines
FOR /F %%D IN ('FINDSTR /R /N "^.*" "%~dp0log.log" ^| FIND /C ":"') DO SET lines=%%D
GOTO :EOF

:ToPower base power
SET n=1
FOR /L %%C IN (1,1,%2) DO SET /A n*=%1
GOTO :EOF


GOTO :EOF


To the best I can, I couldn't tell why the problem is even debugging it in CMD.
It says
"0 was unexpected at this time."
I couldn't figure out why the variable %skip% was processed at the first time even the log.log file does not exist at that time.

Thanks in advance for any help.

Re: was unexpected at this time???

Posted: 15 Apr 2012 08:33
by Fawers
Change the first line to ::@echo off (so it won't disable echoing), run it again and copy/paste the last lines.

Re: was unexpected at this time???

Posted: 15 Apr 2012 08:38
by tinfanide

Code: Select all

::@echo off

REM SET /P base="How many directory(s) at each level? "
REM SET /P power="How many time(s) / level(s) of directories? "

SET base=3
SET power=4

:Control
FOR /L %%Z IN (1,1,%power%) DO CALL :CreateDirectories %%Z
PAUSE
GOTO :EOF


:CreateDirectories index
IF NOT EXIST %~dp0log.log (
   CALL :Make
) ELSE (
   SET /A times=%1-1
   CALL :CountLines
   CALL :ToPower %base% %times%
   SET /A skip=%lines%-%n%
   
   IF %skip% EQU 0 (
      FOR /F "tokens=*" %%A IN ("%~dp0log.log") DO (
      CD %%A
      CALL :Make
      )
   ) ELSE (
      FOR /F "tokens=*" %%A IN ('MORE +%skip% "%~dp0log.log"') DO (
      CD %%A
      CALL :Make
      )   
   
   )

)

GOTO :EOF


:Make
FOR /L %%B IN (1,1,%base%) DO (
   IF NOT EXIST %CD%\%%B (
      MD %%B
      ECHO %%~dpB%%B>>%~dp0log.log
   )
)
GOTO :EOF


:CountLines
FOR /F %%D IN ('FINDSTR /R /N "^.*" "%~dp0log.log" ^| FIND /C ":"') DO SET lines=%%D
GOTO :EOF

:ToPower base power
SET n=1
FOR /L %%C IN (1,1,%2) DO SET /A n*=%1
GOTO :EOF


GOTO :EOF




Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Tin>CD C:\test

C:\test>ZBatchCreateDirectories.bat>test.log
0 was unexpected at this time.
C:\test>



C:\test>REM SET /P base="How many directory(s) at each level? "

C:\test>REM SET /P power="How many time(s) / level(s) of directories? "

C:\test>SET base=3

C:\test>SET power=4

C:\test>FOR /L %Z IN (1 1 4) DO CALL :CreateDirectories %Z

C:\test>CALL :CreateDirectories 1

C:\test> IF EQU 0 (

Re: was unexpected at this time???

Posted: 15 Apr 2012 08:39
by aGerman
Variable skip is assigned inside of the else block. Each environment variable assigned in the same command line or block does not expand to the value except you would use EnableDelayedExpansion ...

Regards
aGerman

Re: was unexpected at this time???

Posted: 15 Apr 2012 08:49
by tinfanide
aGerman wrote:Variable skip is assigned inside of the else block. Each environment variable assigned in the same command line or block does not expand to the value except you would use EnableDelayedExpansion ...

Regards
aGerman


Yes, a common mistake.
Thanks, aGerman.

Re: was unexpected at this time???

Posted: 15 Apr 2012 08:54
by Fawers
That line:

Code: Select all

C:\test>   IF EQU 0 (

You are trying to compare <nothing> to 0. That's why it's returning an error.
You've probably used a variable that didn't expand to anything at all. To avoid this problem, enclose both variable and 0 in anything you want.
I like to use brackets:

Code: Select all

if [%var%] == [0]

There are people that enclose them in quotes:

Code: Select all

if "%var%" == "0"

If the variable happen to be not defined, this line will be interepreted as following:

Code: Select all

if [] == [0]

or

Code: Select all

if "" == "0"