简体   繁体   English

PowerShell 部分 zip 文件名

[英]PowerShell partial zip file name

Currently I have a code that extracts a zip file that is uploaded nightly and it has the name CallRecording_1-000-XXXXXXXX the X's represent the date and time that the zip file was generated.目前我有一个代码可以提取每晚上传的 zip 文件,它的名称为CallRecording_1-000-XXXXXXXX ,X 代表生成 zip 文件的日期和时间。 What I would like to do is have a powershell script that looks for the partial name.我想要做的是有一个 powershell 脚本来查找部分名称。 So for example it would look for just CallRecording_1-000 or CallRecording .例如,它只会查找CallRecording_1-000CallRecording

At the moment I have the following script:目前我有以下脚本:

#expand archive into folder
expand-archive ("Y:\CallRecording_1-000.zip") -destinationPath $folder

#rename zip file with yesterdays date
$yesDateName = $yesDate + ".zip"
Rename-Item "Y:\CallRecording_1-000.zip" -NewName $yesDateName

The scripts that I have found previously that use partial names seems to focus mostly on the extension rather than the name itself.我之前发现的使用部分名称的脚本似乎主要关注扩展名而不是名称本身。

Any help would be appreciated!任何帮助,将不胜感激!

It sounds like you only expect 1 zip file however I tailored this answer around the possibility of having more than 1听起来您只期望 1 个 zip 文件,但是我围绕可能有 1 个以上的 zip 文件定制了此答案

  • We are going to use Get-ChildItem to get any zip files from y:\\ that match 'CallRecordings*.zip'我们将使用Get-ChildItem从 y:\\ 中获取与“CallRecordings*.zip”匹配的任何 zip 文件
  • We then pipe these files one at a time to the ForEach-Object cmdlet where we然后,我们一次一个地将这些文件通过管道传送到ForEach-Object cmdlet,在那里我们
    • assign the extraction folder分配提取文件夹
    • unzip the file解压文件
    • and then rename the file.然后重命名文件。

$i is used to allow us different names for our renamed zip file in case there are more than 1 being processed. $i用于允许我们为重命名的 zip 文件使用不同的名称,以防处理超过 1 个。

    $i = 0
    Get-ChildItem -Path 'Y:\' -Filter 'CallRecording*.zip' | ForEach-Object -Process {
        $extractFolder = "C:\temp\$($_.BaseName)"
        $_ | Expand-Archive -DestinationPath $extractFolder 
        
        # ($? tells us if the last command completed successfully)
        if ($?) {
            # only rename file if Expand-Archive was successful
            $_ | Rename-Item -NewName ((Get-Date).AddDays(-1).ToString('yyyyMMdd') + "_$((++$i)).zip")
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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