-->

SETLOCAL ENABLEDELAYEDEXPANSION , Interrupt SETLOC

2019-10-05 07:08发布

问题:

NOTE:THIS IS NOT A DUPLICATE!! OF "Temporarily interrupt SETLOCAL"

i want to be able to SETLOCAL ENABLEDELAYEDEXPANSION then do something, then ENDLOCAL ENABLEDELAYEDEXPANSION do something else (7z command) then SETLOCAL ENABLEDELAYEDEXPANSION once again in a .bat! because 7z.exe does not allow SETLOCAL ENABLEDELAYEDEXPANSION. see?

code:

SETLOCAL ENABLEDELAYEDEXPANSION
Echo hi!
7z e -o"C:\test" -i!*.jar "C:\*.zip"

output:
hi!
Error: incorrect command line

when i change my code to:

Echo hi!
7z e -o"C:\test" -i!*.jar "C:\*.zip"

it works!!!!!!

here is my code so far to interrupt SETLOCAL ENABLEDELAYEDEXPANSION, unfortunately it doesn't work.

SETLOCAL ENABLEDELAYEDEXPANSION
Echo hi!
Endlocal
7z e -o"C:\test" -i!*.jar "C:\*.zip"
SETLOCAL ENABLEDELAYEDEXPANSION
Echo hi!

i want to put this in a different .bat which needs SETLOCAL ENABLEDELAYEDEXPANSION to run

回答1:

You may solve your problem this way:

set bang=!
SETLOCAL ENABLEDELAYEDEXPANSION
Echo hi!
7z e -o"C:\test" -i!bang!*.jar "C:\*.zip"

Just be sure that the set bang=! command is executed when delayed expansion is disabled.



回答2:

7zip hasn't problems with delayed expansion.
It's the exclamation mark, it will be removed from the line before 7z see it.

You only need to escape them.

SETLOCAL ENABLEDELAYEDEXPANSION
Echo hi^^!
7z e -o"C:\test" -i^^!*.jar "C:\*.zip"