添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hi Guys

i have been looking for the net about what im trying to do, i have an script where i picked some files with bad data on it and send them to another folder, once are there sort them by LastWriteTime -descending and only select the -First 3 files, zip them and send the zip file. The issue is that im getting the error “The Path is not of a legal form” every time i tried to zip the files.

-Code-

var1 = “C:\Temp”

outputFile2 = “C:\Temp\EmptyTRX.zip”

$Files = Gci $var1 -File -Filter *.TRX | Sort LastWriteTime -Descending | select -First 3

Add-Type -Assembly “system.io.compression.filesystem”

[System.io.compression.zipfile]::CreateFromDirectory($Files, $outputFile2)

-End Code-

Any suggestions? im running out of ideas.

Hi and Welcome if you post code, please use the ‘Insert Code’ button. codebutton_small.png

According to the documentation for that method, you can only specify a Directory Path - you cannot specify a list of files. The solution in your case would be to move the files you want to compress into their own directory and provide that path to the method.

To quote:"

Parameters

sourceDirectoryNameString

The path to the directory to be archived, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

destinationArchiveFileNameString

The path of the archive to be created, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

ZipFile.CreateFromDirectory Method (System.IO.Compression) | Microsoft Docs: https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile.createfromdirectory?view=netcore-3.1

compress archive command will do it, you must think about upgarding powershell

 $Source= Get-ChildItem C:\scripts -Filter *.TRX | Sort LastWriteTime -descending | select -first 3 
 $DestinationPath = "D:\test"
 $DestinationFileName = "Test.zip"
Compress-Archive -DestinationPath (Join-Path -Path $DestinationPath -ChildPath $DestinationFileName) -Path $Source.FullName -CompressionLevel Optimal

check for some ideas here How to create, update, and extract zip files with PowerShell | The PoSh Wolf for ps version below 5