1

I have a series of files in a folder that contain large log files

-20180907 1229 debug.log
-20180907 1229 system.log

I want to write a PowerShell script to archive them down in size. Here's my script:

dir *.log | % {
    $archive = '"' + $_.Name + '.7z"'
    $archive
    $source = '"' + $_.Name + '"'
    $source
    &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z $archive $source -WhatIf
}

When I run it, however, I get the following output:

"-20180907 1229 debug.log.7z"
"-20180907 1229 debug.log"
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs.ps1:6 char:5
+     &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Command Line Error:
Unknown switch:
-20180907 1229 debug.log.7z
"-20180907 1229 system.log.7z"
"-20180907 1229 system.log"
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs.ps1:6 char:5
+     &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Command Line Error:
Unknown switch:
-20180907 1229 system.log.7z

Even though I am wrapping the file name in double quotes it appears as 7-Zip is ignoring the double quotes.

Any ideas?


I tried this variant (wrapping the file names in single quotes):

dir *.log | % {
    $archive = "'" + $_.Name + ".7z'"
    $archive
    $source = "'" + $_.Name + "'"
    $source
    &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z $archive $source
}

And this time I got this output:

'-20180907 1229 debug.log.7z'
'-20180907 1229 debug.log'

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Open archive: '-20180907 1229 debug.log.7z'
--
Path = '-20180907 1229 debug.log.7z'
Type = 7z
Physical Size = 32
Headers Size = 0
Solid = -
Blocks = 0

Scanning the drive:
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs-B.ps1:6 char:5
+     &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

WARNING: The system cannot find the file specified.
'-20180907 1229 debug.log'
0 files, 0 bytes

Updating archive: '-20180907 1229 debug.log.7z'

Add new data to archive: 0 files, 0 bytes


Files read from disk: 0
Archive size: 32 bytes (1 KiB)

Scan WARNINGS for files and folders:

'-20180907 1229 debug.log' : The system cannot find the file specified.
----------------
Scan WARNINGS: 1

It's created files like '-20180907 1229 debug.log.7z', but with no content.

4
  • Wrap it in single quotes as to not activate the operator. Commented Apr 1, 2019 at 5:04
  • @Drew - I have tried that and that didn't work either. I'll post the code and output. Commented Apr 1, 2019 at 6:01
  • What is your working directory ? Is it the same where the .log files are ? Commented Apr 1, 2019 at 8:02
  • @Fourat - Yes, it is. Commented Apr 1, 2019 at 11:53

3 Answers 3

1

Try this:

 $executable = "C:\Program Files\7-Zip\7z.exe"

 dir *.log | % {
    $archive = "$($_.Name).7z"
    $archive
    $source = $_.Name
    $source 
    $oneliner = "a",".\$($archive)",".\$($source)", "-mx4"
    & $executable @oneliner
}
Sign up to request clarification or add additional context in comments.

3 Comments

Add more explanation so the user that asked can understand your answer.
An explanation would be nice - especially covering the syntax of the $onliner value.
For $onliner I used method called splatting. More information about running executables in Powershell here.
0

I think it don't needs to be enclosed in quotes.
What if I do the following?

dir *.log | % { &"C:\Program Files\7-Zip\7z.exe" -mx9 a -t7z "$_.7z" $_ }

Comments

0

7-Zip has a switch for files starting with -.

-- (Stop switches parsing) switch

Disables switch parsing after -- on the command line. This is to allow 7-Zip to use file names that start with -.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.