Shell.Execute and multiple commands

I need Shell.Execute to execute multiple commands. But I noticed that every time a Shell.Execute gets executed, even without a Shell.Close, it executes it in it’s own, new environment with no ‘memory’ of the previous executed command.

I need to go to a specific director first, with a “CD \MyDirectory\MySecondDirectory\TargetDirectory” first before I can use a Shell.Execute in this “Targetdirectory” where an executable resides.

How to execte multiple commands in one session?

just concat two or more commands:

var s as new Shell
s.Execute("touch test.txt && mv test.txt test2.txt")

this works for Linux, macOS aswell in DOS/Windows of course with OS specific commands only

Use an Asynchronous shell which allows you to check each command for errors.

1 Like

Wayne’s note gets you half way there. You also need to launch a shell and then use theShell.WriteLine to execute additional commands.

Thanks for your solution but doesn’t work.

would you please specify, what commands you’re trying to execute?

I use 7Z open source library to extract .CBR and CBZ (comic book) files which are essentialy RAR and ZIP files.

The && concatenation works fine. The problem I have is putting quotes around path parameters. My command string build-up (it is complicated):
CommandString = "CD " + UnZipCommandPath + " && 7z.exe E """ + MyComicBookFolder.Child(ComicFile.Name).NativePath + """ -O" + ""+ MyComicBookFolder.NativePath + ""

The literal I get from that is:
CD C:\Users\slind\SynologyDrive\Dev\ComicReader\Source\DebugStrip\DebugStrip libs\ && 7z.exe E "C:\Users\slind\AppData\Roaming\ComicReader\Cache\Bob Morane - 19 - De Draak Van De Fenstones\Bob Morane - 19 - De Draak Van De Fenstones.cbr" -OC:\Users\slind\AppData\Roaming\ComicReader\Cache\Bob Morane - 19 - De Draak Van De Fenstones\

which works OK. Except the last parameter -O etc -OC:\Users\slind\AppData\Roaming\ComicReader\Cache\Bob Morane - 19 - De Draak Van De Fenstones) should get quotes around it.
It works with the first parameters, but not with the last. This double/triple quote thing is confusing to say the least.

[EDIT] Solved. I needed 4 quotes to get the correct result:
CommandString = "CD " + UnZipCommandPath + " && 7z.exe E """ + MyComicBookFolder.Child(ComicFile.Name).NativePath + """ -O" + """"+ MyComicBookFolder.NativePath + """"

You’re missing some quotes - to put a quote as a standalone element, you must use 4 quotes, so your command would be:

CommandString = "CD """ + UnZipCommandPath + """ && 7z.exe E """ + MyComicBookFolder.Child(ComicFile.Name).NativePath + """ -O """ + MyComicBookFolder.NativePath + """"

You also left a space out after the -O argument and the quote can be part of that section. Try it that way.