简体   繁体   中英

Compress Files from list with PowerShell 5

I have a file with a list of files with directions.

Like this:

XXXXXX/sample.txt

XXXXXX/dog.txt

XXXXXX/cat.docx

ZZZZ/lamp.jpg

How can I to compress all files and save files with sub-directions.

Now, I can to compress all files but without directions.

Like this:

sample.txt

dog.txt

cat.docx

lamp.jpg

Sorry on my bad English.

foreach ($filename in Get-Content .\ListOfFilesToZip.txt)
{
    Compress-Archive -Update $filename .\ZipFile.zip
}

Edit If question is about getting filename from the path
The most efficient way would be with a regex, but they can be rather complicated if you are not used to working with them.

A simpler way would be to simply split each string and select the last part where the file name is:

$string = "c:\asd\qwe\zxc\dog.txt"

#Split the string on each \
    $string.Split("\") 

#This will output a list like this
c:
asd
qwe
zxc
dog.txt

Now we want to simply select the last entry in this list as the filename is always last in a path. So we use Select Object for that.

$string.Split("\") | Select-Object -Last 1 

This will return:

dog.txt

You can run this through a foreach on your list to get it for each item.

Not sure if there's any better way of doing this, but you can create a temp folder, copy the files to be archived along with desired folder structure over there and then just zip up the whole thing.

Take a look here (not my code, but seems to do exactly that) for a sample

The following function can compresses a whole folder structure into a single zip file and will keep the structure within the zip (even works with PowerShell <5).

function createZipFile($outputFileName, $sourceDirectory){
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourceDirectory, $outputFileName, $compressionLevel, $false)
}

Call it like that:

createZipFile "c:\temp\output.zip" "c:\folder\to\compress"
function DirNewTemp {
    $tmpdir = (New-TemporaryFile).FullName
    Remove-Item -Force -Path $tmpdir -ErrorAction 'SilentlyContinue'
    New-Item -Type Directory $tmpdir | out-null
    return $tmpdir
}

function ZipFiles {
    param(
        [string]$Zip,
        [string[]]$FileList
    )
    write-host ""
    write-host "====================================="
    write-host "ZipFile $Zip"
    write-host "====================================="
    
    $pwd      = (Get-Location).Path     
    $tmpdir   = DirNewTemp
        
    try {
        $count = $FileList.Count
        foreach ($file in $FileList) {
            $pwd_escaped = [Regex]::Escape($pwd)        
            $save_file   = $file -replace "^${pwd_escaped}\\", "${tmpdir}\"
            $save_path   = Split-Path $save_file -Parent
            New-Item  -Force -Path $save_path -ItemType Directory | out-null
            Copy-Item -Force $file $save_file           
        }
        set-location $tmpdir        
        $dest = "$pwd\$Zip";
        write-host "Creating Zip: $dest"        
        Compress-Archive -Force -Path * -DestinationPath $dest
    }
    finally {
        write-host "cleanup"
        set-location $pwd
        remove-item -Force -Recurse -Path $tmpdir -ErrorAction 'SilentlyContinue'
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM