Используйте метод COM Object для переименования элемента в сжатом / zip-файле - Powershell - PullRequest
0 голосов
/ 03 июля 2019

Как переименовать файл внутри сжатого файла.

$timestamp = (Get-Date).AddMonths(-1).ToString('yyyy-MM')
$newFolderTimeStamp = (Get-Date).AddMonths(-1).ToString('MM - MMM-yyyy')
$sourceFile = "D:\Testing\1.txt","D:\Testing\2.txt","D:\Testing\3.txt","D:\Testing\4.txt","D:\Testing\5.txt"
#$sharedrive = "\\fs2\Backups\it\users.xlsx"
$newFolder = "D:\Testing\bin\$newFolderTimeStamp.zip"


######Compressed File
function Add-Zip
{
    param([string]$zipfilename)

    if(-NOT (test-path($zipfilename)))
    {
        #create an empty zip file
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

        #make sure it is not set to ReadOnly
        (dir $zipfilename).IsReadOnly = $false  
    }


    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

    foreach($file in $input) 
    { 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
}

######Get file for today's date
Get-ChildItem $sourceFile -Force | Where-Object{$_.LastWriteTime -gt $date -and $_.extension -ne ".zip"} | add-Zip $newFolder 

Пробовал, но переименовывал только имя файла zip.

Get-ChildItem $sourceFile -Force | Rename-Item -NewName {$_.Basename + '_' + $timestamp + $_.Extension } | Where-Object{$_.LastWriteTime -gt $date -and $_.extension -ne ".zip"} | add-Zip $newFolder 

Ожидаемый результат

06 - Jun-2019.zip

1_2019-06.txt

2_2019-06.txt

3_2019-06.txt

4_2019-06.txt

5_2019-06.txt

...