【问题标题】:Check same name files in folder with different extensions, if exists, move one out检查不同扩展名的文件夹中的同名文件,如果存在,移出一个
【发布时间】:2017-01-19 05:13:26
【问题描述】:

每天早上,一个文件夹都会收到相同名称的 zip 文件。我目前有脚本可以检查非压缩文件,如果它们存在,则会创建一个存档文件夹,并将它们移到那里。然后解压缩 zip 文件,然后删除 zip 文件。

出现了一个新条件,即 1 个文件不会每天传输,而是每周传输(一周中的随机日期),并且关联的非压缩文件必须保留在文件夹中,直到该 zip 文件被转入。

我将如何修改我的代码以检查压缩文件名与非压缩文件名,如果匹配,则在解压缩其余文件之前仅移动这些文件?

我当前的代码: 第一个 if 语句测试不存在的 zip 文件。如果存在,则使用日期创建一个新的存档目录。然后将那些非 zip 文件移动到此文件夹中。

第二个if语句检查zip文件,如果存在,它会解压每个,然后删除zip文件。

$dir = "D:\test"
$date = Get-Date -Format "yyyy-MMM-dd"

if (Test-Path "$dir\*" -Exclude *.zip -PathType Leaf) {
    $newDir = New-Item -Type Directory -Path "$dir\Archive\$date" -Force
    $nozip = gci -Recurse $dir -Exclude *.zip | where { ! $_.PSIsContainer }

    foreach ($file in $nozip) {
        Move-Item $file $newDir
    }
}
if (Test-Path "$dir\*.zip" -PathType Leaf) {
    $list = gci -Recurse $dir -Include *.zip
    $shell = New-Object -COM Shell.Application

    foreach ($file in $list) {
        $zip = $shell.NameSpace($file.FullName)
        foreach ($item in $zip.Items()) {
            $shell.NameSpace($dir).CopyHere($item)
        }
        Remove-Item $file
    }
} else {
    exit
}

示例场景:

文件夹 D:\Test 包含文件 A.txt、B.txt 和 C.txt。
每天早上,包含新的 B.txt 和 C.txt 的文件 B.zip 和 C.zip 被传输到同一个文件夹 D:\Test。

zip文件需要解压,但是对应的已经在这个文件夹下的B.txt和C.txt需要移出到D:\Test\Archive,然后B.zip和C.zip就可以了解压缩,在 D:\Test 中留下一个新的 B.txt 和 C.txt,然后删除那些 zip 文件。

我遇到的问题是 A.zip 不是每天传输的。每周一次,在 A.zip 进入文件夹之前,A.txt 需要保留在 D:\Test 中。

现在,我的脚本每天都会将所有非 zip 文件移动到 D:\Test\Archive。因此,当 B.zip 和 C.zip 进入 D:\Test 时,该过程完成后将没有 A.txt,它与其余文件一起移动到 D:\Test\Archive。

所以我需要一种方法来对照 .zip 文件检查所有 .txt 文件,如果文件名匹配,而不是扩展名,则将该非压缩文件移动到 D:\Test\Archive。如果没有匹配,则将该非压缩文件留在 D:\Test 中并继续处理那些匹配的文件。

【问题讨论】:

  • 考虑添加一些具有适当文件名的示例场景。像这样,foo.zip arrives, there already are files bar and zof -> foo will be extracted and now there are files foo, bar...
  • 好的,@vonPryz,我添加了流程的场景。谢谢。

标签: powershell zip


【解决方案1】:

像这样修改你的第一部分:

$dir = "D:\Test"
$date = Get-Date -Format "yyyy-MMM-dd"
$newDir = New-Item -Type Directory -Path "$dir\Archive\$date" -Force
$files = (Get-ChildItem "$dir\*.txt").Name | foreach {$_.Split(".")[0]}
$zips = (Get-ChildItem "$dir\*.zip").Name | foreach {$_.Split(".")[0]}
foreach ($file in $files)
{
    foreach ($zip in $zips)
    {
        if($file -match $zip)
        {
            Move-Item "$dir\$file.txt" $newDir
            Write-Output "$file matches $zip"
        }
        else
        {
            Write-Output "$file does not match $zip"
        }
    }
}

【讨论】:

    【解决方案2】:

    谢谢马特!我稍微改变了一些东西,但你的比较循环是关键。

    $dir = "D:\Test"
    $date = Get-Date -Format "yyyy-MMM-dd"
    $newDir = New-Item -Type Directory -Path "$dir\Archive\$date" -Force
    $fileName = gci $dir -exclude *.zip | ? { ! $_.PSIscontainer} | % { [IO.Path]::GetFileNameWithoutExtension($_) } | Sort
    $zips = gci -recurse $dir -Include *.zip
    $zipName  = $zips | % { [IO.Path]::GetFileNameWithoutExtension($_) } | Sort
    
    foreach ($file in $fileName){
        foreach ($zip in $zipName){
            if($file -match $zip){
                Move-Item "$dir\$file.txt" $newDir
            }
        }
    }
    
    if (Test-Path "$dir\*.zip" -pathType leaf){
        $shell = New-Object -com shell.application
        foreach($file in $zips){ 
           $zip = $shell.NameSpace($file.FullName)
           foreach($item in $zip.items()){
               $shell.Namespace($dir).copyhere($item)
           }   
           Remove-Item $file
        } 
    } else {
      Exit
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2015-04-04
      • 2021-03-18
      相关资源
      最近更新 更多