Powershell variable expansion when calling other programs
Asked Answered
H

3

5

I have a small problem trying to unzip a file using the 7za command-line utility in Powershell.

I set the $zip_source variable to the zip file's path and the $unzip_destination to the desired output folder.

However the command-line usage of 7za needs arguments specified like this:

7za x -y <zip_file> -o<output_directory>

So my current call looks like this:

& '7za' x -y "$zip_source" -o$unzip_destination

Due to the fact that there can be no space between -o and the destination it seems that PowerShell will not expand the $unzip_destination variable, whereas $zip_source is expanded.

Currently the program simply extracts all the files into the root of C:\ in a folder named $unzip_destination. Setting different types of quotes around the variable won't work neither:

-o"$unzip_destination" : still extracts to C:\$unzip_destination
-o'$unzip_destination' : still extracts to C:\$unzip_destination
-o $unzip_destination  : Error: Incorrect command line

Is there any way to force an expansion before running the command?

Hephaestus answered 7/5, 2012 at 14:34 Comment(0)
I
3

try like this:

-o $($unzip_destination)

Editor's note: This solution only works with a space after -o (in which case just -o $unzip_destination would do) - if you remove it, the command doesn't work as intended.
This approach is therefore not suitable for appending a variable value directly to an option name, as required by the OP.

Inappropriate answered 7/5, 2012 at 14:45 Comment(4)
There can be no space between -o and the unzip_destinationProgenitive
When I leave out the space after the -o that works - thanks. Does $() start some kind of subshell in PowerShell (sorry I am new to Windows scripting from a Unix-background).Hephaestus
$() is called a subexpression, and it causes everything inside the parenths to be returned as one value: $("Hello, " + "world!") returns literally: Hello, world! These are useful when wanting the output of some expression to be translated to a string in the middle of another string.Crispation
Removing the space does not work - unless you enclose the whole argument in "...", but then you can just use jonZ's simpler answer. Good hint, @SpellingD; worth adding that, in the interest of robustness, $(...) as part of a string should always be used with explicit double-quoting, because with an unquoted token it doesn't always work - as is the case here, because the token starts with -; see github.com/PowerShell/PowerShell/issues/6467Introrse
P
6

Try this:

& '7za' x -y "$zip_source" "-o$unzip_destination" 
Progenitive answered 7/5, 2012 at 14:49 Comment(1)
Works as well - I guess I should have kept on experimenting with quotes.Hephaestus
I
3

try like this:

-o $($unzip_destination)

Editor's note: This solution only works with a space after -o (in which case just -o $unzip_destination would do) - if you remove it, the command doesn't work as intended.
This approach is therefore not suitable for appending a variable value directly to an option name, as required by the OP.

Inappropriate answered 7/5, 2012 at 14:45 Comment(4)
There can be no space between -o and the unzip_destinationProgenitive
When I leave out the space after the -o that works - thanks. Does $() start some kind of subshell in PowerShell (sorry I am new to Windows scripting from a Unix-background).Hephaestus
$() is called a subexpression, and it causes everything inside the parenths to be returned as one value: $("Hello, " + "world!") returns literally: Hello, world! These are useful when wanting the output of some expression to be translated to a string in the middle of another string.Crispation
Removing the space does not work - unless you enclose the whole argument in "...", but then you can just use jonZ's simpler answer. Good hint, @SpellingD; worth adding that, in the interest of robustness, $(...) as part of a string should always be used with explicit double-quoting, because with an unquoted token it doesn't always work - as is the case here, because the token starts with -; see github.com/PowerShell/PowerShell/issues/6467Introrse
T
0

This should work:

& '7za' x -y $zip_source -o${unzip_destination} 
Thwart answered 30/7, 2012 at 14:20 Comment(1)
Perhaps surprisingly, that would only work if the token didn't start with -.Introrse

© 2022 - 2024 — McMap. All rights reserved.