首页 文章

检查具有不同扩展名的文件夹中的相同名称文件(如果存在),移出一个

提问于
浏览
0

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

出现了一个新情况,即1个文件没有每日传输,但它将是每周(一周中的随机日期),并且相关的非压缩文件必须保留在文件夹中,直到该zip文件被传输 .

我如何修改我的代码来检查非压缩文件的压缩文件名,如果有匹配,在解压缩其余文件之前只移动那些文件?

我当前的代码:首先 if 语句测试不存在的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中,并继续处理匹配的文件 .

2 回答

  • 0

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

    $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"
            }
        }
    }
    
  • 1

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

    $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
    }
    

相关问题