[wpkg-users] [OT] 'for' madness...

Stefan Pendl stefan.pendl.71 at gmail.com
Thu Mar 21 23:44:16 CET 2013


Am 21.03.2013 10:58, schrieb Marco Gaiarin:
>
> I'm trying to build a cmd script, doing what i'm think are simple
> tasks, but i'm hitting some trouble doing a nested 'for'.
>
> Example:
>
>
>   for %%G in (%APPDATA%\Mozilla\Firefox\Profiles %APPDATA%\Thunderbird\Profiles) do (
>          echo %%G
>          if exist "%%G" (
>                  if defined SQLITE (
>                          echo %SQLITE%
>                          for /d /r %%X in (%%G\*.sqlite) do (
>                                  echo %SQLITE% "%%X" VACUUM
>                                  echo %SQLITE% "%%X" REINDEX
>                          )
>                  )
>          )
>   )
>
>
> Inside the first for, 'echo %%G' works, the exist check works, but the
> use of %%G in the second for not, and if i try to define a variable,
> eg:
> 	set test=%%G
>
> %test% is empty.
>
>
> What i'm missing? Thanks.
>

The problem is that a bracket is starting a separate part, which gets 
variable expansion before it is executed.
You need to use delayed variable expansion to allow assigning inside a 
bracket to work.
It is much easier to work with procedures, see below.

'---code start (watch for line wraps)

for %%G in ("%APPDATA%\Mozilla\Firefox\Profiles" 
"%APPDATA%\Thunderbird\Profiles") do call :checkIt "%%~G"

goto :EOF

:checkIt

     echo "%~1"

     if not exist "%~1" goto :EOF
     if not defined SQLITE goto :EOF

     echo %SQLITE%

     for /r "%~1" %%X in (*.sqlite) do call :doSQL "%%~X"

goto :EOF

:doSQL

     echo %SQLITE% "%~1" VACUUM
     echo %SQLITE% "%~1" REINDEX

goto :EOF

'---code end

--
Stefan P.

Top-posting:
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?



More information about the wpkg-users mailing list