1

Tried many combinations using this script to compress a lot of files into individual files using 7zip, but I keep getting errors.

Get-ChildItem *.smc | ForEach-Object { 7z a -yx9 -x9 "$_.Name+.7z" $_.Name }

Keeps coming up with "Too Long Switch" or "Too Short Switch". What's going on here?

Jim Kieger
  • 901
  • 1
  • 8
  • 13

2 Answers2

3

There are two separate problems with your command:

  • You're incorrectly embedding property access $_.Name inside "...": you must use "$($_.Name)+.7z" - note the use of $(...), the subexpression operator.

    • In short: In order to embed expressions in an expandable string ("..."), you must enclose them in $(...). Notably, this includes property and indexed access (e.g., $($var.property), $($var[0])). Only variables as a whole do not require this (e.g., $var, $env:USERNAME). See this answer for more information.
  • Your options are meant to specify file-analysis levels and compression levels, which means that they must be passed as option arguments to the -m option.

    • Therefore, use -myx9 -mx9
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I used cmd /c to solve my problem with -x, eg:

& { cmd /c "7z a -x!.vs ..\snakepas_vs2022.zip ." }

Or:

& { 7z a "-x!.vs" ..\snakepas_vs2022.zip . }
Aaron West
  • 187
  • 2
  • 5