Geremy Gibson
Geremy Gibson

Reputation: 21

Batch File: Multiple Commands on one line with Set

I have a framework where I can only run stuff through PowerShell, but I need to run batch file commands. I'm trying to run a PowerShell Script, something like:

cmd /c blah

for blah I want to do something like:

set myPath = c:\theDir && if not exist %myPath% mkdir %myPath%

This will not work the first time I run it as the set command doesn't seem to take affect until the second line. Any ideas?

Upvotes: 2

Views: 2591

Answers (4)

Blackkatt
Blackkatt

Reputation: 13

You can also do like this

SET _KillElevated=1& SET _KillWithGrace=1

Upvotes: 0

Mark Deven
Mark Deven

Reputation: 579

You can also define the delayed expansion before you run multiple commands. That way you would not have to open a new CMD instance:

Setlocal EnableDelayedExpansion
set say=Hello && echo !say! && echo Done!

Upvotes: 0

Joey
Joey

Reputation: 354356

This is because cmd evaluates variables when a line is parsed, not when it's run. To get the latter behaviour you'll have to use delayed expansion:

cmd /c /v:on "set MyPath=C:\theDir&& if not exist "!myPath!" mkdir "!myPath!"

Note also that you must not have spaces around the = in a set, otherwise you're creating a variable name with a space at the end (which is to say, your approach would never have worked anyway).

Upvotes: 2

Lumi
Lumi

Reputation: 15264

for %d in (some\path and\maybe\another\one) do @if not exist "%d" md "%d"

Upvotes: 0

Related Questions