Page 1 of 1

single line in runonce

Posted: 24 Nov 2019 01:34
by aristosv
I want to put a RunOnce entry in my autounattend.xml, that will detect the USB drive, and run a batch file located on the USB drive.

My problem is that a RunOnce entry is a single line, I can't have 2 lines.

in my testing this works, but it's 2 lines

Code: Select all

for %%i in (A:,B:,D:,E:,F:,G:,H:,I:,J:,K:,L:,M:,N:,O:,P:,Q:,R:,S:,T:,U:,V:,W:,X:,Y:,Z:,) do (if exist %%i\autounattend.xml set usb=%%i)
echo the usb drive is %usb%
and this does not work

Code: Select all

for %%i in (A:,B:,D:,E:,F:,G:,H:,I:,J:,K:,L:,M:,N:,O:,P:,Q:,R:,S:,T:,U:,V:,W:,X:,Y:,Z:,) do (if exist %%i\autounattend.xml set usb=%%i) & echo the usb drive is %usb%
How can I make this work in a single line?

Thanks

Re: single line in runonce

Posted: 24 Nov 2019 03:37
by Blazer
Try including the echo inside the brackets like this:

Code: Select all

for %%i in (A:,B:,D:,E:,F:,G:,H:,I:,J:,K:,L:,M:,N:,O:,P:,Q:,R:,S:,T:,U:,V:,W:,X:,Y:,Z:,) do (if exist %%i\autounattend.xml set usb=%%i & echo the usb drive is %usb%)

Re: single line in runonce

Posted: 24 Nov 2019 04:46
by aGerman
If you want to expand a variable to its value in the same command line you have to enable delayed variable expansion using SETLOCAL ENABLEDELAYEDEXPANSION and enclose the variable in exclamation points rather than in percent signs. But that's not necessary since you already have the drive letter in %%i. Just use ECHO %%i in place of your SET statement.

Steffen

Re: single line in runonce

Posted: 24 Nov 2019 07:43
by Eureka!
aGerman wrote:
24 Nov 2019 04:46
If you want to expand a variable to its value in the same command line you have to enable delayed variable expansion [...]
Or:

Code: Select all

@for %%i in (A:,B:,D:,E:,F:,G:,H:,I:,J:,K:,L:,M:,N:,O:,P:,Q:,R:,S:,T:,U:,V:,W:,X:,Y:,Z:,) do @if exist "%%i\autounattend.xml" (set "usb=%%i" & call echo the usb drive is "%%usb%%")

Re: single line in runonce

Posted: 24 Nov 2019 07:53
by aGerman
Absolutely. But keep in mind that CALL leads to double parsing. No problem if you use it once. But doing that repeatedly in a loop has the potential to destroy the performance of your script.

Steffen