0

I have the following batch file contents:

REM Newest folder
FOR /F "delims=" %%I IN ('DIR \\cc_server\CruiseControl\TFS_Root\Release\2017.V1\Deploy\Store\Company\MyApp\* /B /O:D /AD') DO SET NewestDir=%%I
REM Newest file of newest folder
IF Defined NewestDir (
    FOR /F "delims=" %%I IN ('DIR "\\cc_server\CruiseControl\TFS_Root\Release\2017.V1\Deploy\Store\Company\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.7z" "\\cc_server\CruiseControl\TFS_Root\Release\2017.V1\Deploy\Store\Company\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.zip" /B /O:D /A-D') DO SET NewestFile=%%I
    IF Defined NewestFile (
        SET ExistsInStore=TRUE
    )
) ELSE (
    FOR /F "delims=" %%I IN ('DIR \\backups\Builds\Release\2017.V1\MyApp\* /B /O:D /AD') DO SET NewestDir=%%I
    IF Defined NewestDir (
         FOR /F "delims=" %%J IN ('DIR \\backups\Builds\Release\2017.V1\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.7z \\backups\Builds\Release\2017.V1\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.zip /B /O:D /A-D') DO SET NewestFile=%%J
         PAUSE
    )
)

IF Defined NewestFile (
    IF Defined ExistsInStore (
        copy "\\cc_server\CruiseControl\TFS_Root\Release\2017.V1\Deploy\Store\Company\MyApp\%NewestDir%\%NewestFile%" .
    ) ELSE (

        copy "\\backups\Builds\Release\2017.V1\MyApp\%NewestDir%\%NewestFile%" .
    )

    "C:\Program Files\7-Zip\7z" x "%NewestFile%" -o"MyApp 2017"
    del "%NewestFile%"
)


Set NewestDir=
SET NewestFile=
SET ExistsInStore=

Given it does not exist the file in in cc_server it then falls through the backups machine where it exists.

The problem is: if I run the script, I will see a whole bunch of SET NewestDir= lines, and after:

File Not Found
Press any key to continue . . .

which corresponds to the PAUSE command line.

I know the files exist there, as the next paragraph will make it clear.

If I copy the instructions of the relevant part and replace the %% occurrences by % as follows:

FOR /F "delims=" %I IN ('DIR \\backups\Builds\Release\2017.V1\MyApp\* /B /O:D /AD') DO SET NewestDir=%I
IF Defined NewestDir (
     FOR /F "delims=" %J IN ('DIR \\backups\Builds\Release\2017.V1\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.7z \\backups\Builds\Release\2017.V1\MyApp\%NewestDir%\[RL]*[x64]*[SuperPro]*.zip /B /O:D /A-D') DO SET NewestFile=%J
)

and run it directly in the command prompt, the problem does not occur, and the NewestFile variable will be set.

What am I wrong doing?

sergiol
  • 4,122
  • 4
  • 47
  • 81
  • Take a look at [delayed expansion](http://ss64.com/nt/delayedexpansion.html), which is required here as you write *and* read the same variables within the same block of code... – aschipfl Nov 26 '18 at 12:53
  • @aschipfl: Is that the reason why "my" script is in two separate `IF` blocks instead of them being nested? – sergiol Nov 26 '18 at 14:07
  • I don't think so; your script is nested with respect to variables; for instance, in the `else` branch of your first `if`/`else` block you set `NewestDir` and you read it as `%NewestDir%`; however, `if defined` can be used without problems... – aschipfl Nov 26 '18 at 14:18

0 Answers0