微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?