was unexpected at this time???

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

was unexpected at this time???

#1 Post by tinfanide » 15 Apr 2012 08:17

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: was unexpected at this time???

#2 Post by Fawers » 15 Apr 2012 08:33

Change the first line to ::@echo off (so it won't disable echoing), run it again and copy/paste the last lines.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: was unexpected at this time???

#3 Post by tinfanide » 15 Apr 2012 08:38

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 (

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: was unexpected at this time???

#4 Post by aGerman » 15 Apr 2012 08:39

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: was unexpected at this time???

#5 Post by tinfanide » 15 Apr 2012 08:49

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: was unexpected at this time???

#6 Post by Fawers » 15 Apr 2012 08:54

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"

Post Reply