single line in runonce

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

single line in runonce

#1 Post by aristosv » 24 Nov 2019 01:34

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

Blazer
Posts: 14
Joined: 20 Dec 2018 09:47

Re: single line in runonce

#2 Post by Blazer » 24 Nov 2019 03:37

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%)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: single line in runonce

#3 Post by aGerman » 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 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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: single line in runonce

#4 Post by Eureka! » 24 Nov 2019 07:43

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%%")

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: single line in runonce

#5 Post by aGerman » 24 Nov 2019 07:53

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

Post Reply