[wpkg-users] [OT?] Nested for on a cmd script...

Rainer Meier r.meier at wpkg.org
Mon Feb 2 20:46:11 CET 2015


Hi Marco,

On 02.02.2015 17:45, Marco Gaiarin wrote:
...
>   for /F "eol=; tokens=1,2,3*" %%l in ( %WPKGROOT%\packages\srp.dirs ) do (
>
>          set level=%%l
>          set type=%%m
>          set pathrule=%%n
>
>          for /F "tokens=1" %%u in ( '%WPKGROOT%\tools\Uuidgen.Exe' ) do (
>                  set UUID=%%u
>          )
>
>          reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\%UUID%" /f /v "ItemData" /t %type% /d "%pathrule%"
>          reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\%UUID%" /f /v "LastModified" /t REG_QWORD /d 130673587356114114
>          reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\%UUID%" /f /v "SaferFlags" /t REG_DWORD /d "0"
>          reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\%UUID%" /f /v "Description" /t REG_SZ /d ""
>   )
>
>
> And works, but the %UUID% variable seems to be defined only the first
> time, so only the *LAST* rule in file srp.dirs get inserted (or,
> better, get all inserted but with the same UUID, so the latter modify
> the former and only the last remain).


I think you should enable delayed expansion and use !UUID! in the for loop instead.

Therefore try:


setlocal enabledelayedexpansion
  for /F "eol=; tokens=1,2,3*" %%l in ( %WPKGROOT%\packages\srp.dirs ) do (

         set level=%%l
         set type=%%m
         set pathrule=%%n

         for /F "tokens=1" %%u in ( '%WPKGROOT%\tools\Uuidgen.Exe' ) do (
                 set UUID=%%u
         )

         reg add 
"HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\!UUID!" 
/f /v "ItemData" /t %type% /d "%pathrule%"
         reg add 
"HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\!UUID!" 
/f /v "LastModified" /t REG_QWORD /d 130673587356114114
         reg add 
"HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\!UUID!" 
/f /v "SaferFlags" /t REG_DWORD /d "0"
         reg add 
"HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\%level%\Paths\!UUID!" 
/f /v "Description" /t REG_SZ /d ""
  )
endlocal

Well, this is a guess since I didn't try the whole thing but as I remember the 
%UUID% inside the for loop is inserted before for loop is executed normally, so 
changes applying to UUID variable will not be propagated to further for loop 
iterations.

br,
Rainer



More information about the wpkg-users mailing list