使用 Powershell 自动删除不起作用的文件

问题描述

我们处理一千多个文件。在检查存档位置中是否存在文件后,我想使用 Powershell 自动删除每个文件夹路径中超过一年的文件。确实如此,它会从备份中删除。如果没有,它将文件从备份移动到存档位置。我不确定我的代码有什么问题。它没有给我一个我可以继续下去的错误信息。当我找到每个变量的值时,我觉得一切都很好。发生的情况是文件没有被移动。

Function Delete-Files ($src)
    {
        #finding date to remove i.e,if today's date is 5/19/2021,then the code would only be looking at files that are before 5/19/2020 12:00:00 AM
        $date2remove = (Get-Date).Date.AddYears(-1)
        $clientLocation = "\\fileshare\test\test\test\Backup\Client\"
        #getting Client folder #
        $clients = (Get-ChildItem $clientLocation -Directory).name
        foreach ($client in $clients)
        {
            $srcparent = $clientLocation + $client + $src -join "\"
            #ignoring the paths that do not contain $src
            if (Test-Path $srcparent)
            {
            #getting files that are older than a year
            $files2remove = Get-ChildItem $srcparent | Where-Object {( $_.LastWriteTime -lt $date2remove)}
                foreach ($file2remove in $files2remove)
                {
                    #getting the year for the last write time for each file
                    $year2remove = $file2remove.LastWriteTime.Year
                    $archive = '\\fileshare\test\test\test\archive\' + $year2remove + '\Client\' + $client + $src -join '\'
                    #if archive folder doesn't exist,create
                    if (!(Test-Path $archive))
                    {
                        New-Item -Path $archive
                    }
                    #getting the name of each file that needs to be removed
                    $fileName2remove = $file2remove.name
                    $archiveFile = $archive + $fileName2remove -join '\'
                    $srcFile = $srcparent + $fileName2remove -join '\'
                    #if file exists in archive,remove
                    if (Test-Path $archiveFile)
                    {
                        Remove-Item -Path $srcFile
                        "$srcFile"
                    }
                    #if file does not exist,move file from backup to archive
                    else
                    {
                        Move-Item -Path $srcFile -Destination $archive
                        "$archiveFile"
                    }
                }
            }
            }
    }
    
    $src = "\folder1\"
    Delete-Files $src
    
    $src = "\folder1\folder2\"
    Delete-Files $src
    
    $src = "\folder3\"
    Delete-Files $src

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...