【问题标题】:Unable to convert text files into an array无法将文本文件转换为数组
【发布时间】:2019-09-12 05:08:30
【问题描述】:

我在 powershell 中做一个简短的脚本,应该删除超过 60 天的文本文件,但是我无法继续,因为脚本只删除了一堆中的一个随机文件,我认为原因是它不在数组中,这就是它没有遍历所有文本文件的原因。

这是整个脚本:

cls
#clearscreen

$ilogs = get-childitem D:\IMPlogs -recurse
#get all items inside IMPlogs folder

$List = $ilogs | where name -like *.txt.4
#filter items having .txt.(num)

#Write-Host ($List |Format-Table| Out-String)
#list all items in the terminal

$Today = (Get-Date -UFormat %s)
#date to seconds

$ilogsD2S = [datetime]($List).LastWriteTime
#get last write time of file and conv to datetime
#}

$conv2s = (Get-Date $ilogsD2S -UFormat %s)
#conv write time to seconds


#$conv2s
#$Today

$datediff = [int](($Today-$conv2s)/(3600*24))
#substracting today's date and files write time and getting only the whole number
$datediff

Write-Host ($List | Format-Table| Out-String)

#$List | Foreach-Object {Write-Host $_}

这是我需要处理的文本文件。 (它会自动填充此文件夹中的文本文件)

【问题讨论】:

    标签: arrays powershell scripting


    【解决方案1】:

    试试这样的:

    Get-ChildItem "c:\temp" -file | where {$_.Name -like '*.txt.?*' -and $_.LastWriteTime -le (Get-Date).AddDays(-60)} | Remove-Item
    

    【讨论】:

      【解决方案2】:

      我不确定您的示例脚本,但您可以使用这样的 2 行脚本来实现您的目标。

      $DelFromDate = (Get-Date).AddDays(-60)
      Get-ChildItem -Path "X:\path\to\folder" -Recurse | 
          Where-Object {$_.LastWriteTime -lt $DelFromDate} | Remove-Item
      

      希望对你有帮助!!!

      【讨论】:

        【解决方案3】:

        您的脚本的主要问题是您没有遍历文件列表。 [grin] 接下来是您正在将 datetime 对象转换为日期 strings ...并且这些对象不能优雅地相互比较。将它们保留为日期时间对象,它们确实可以非常整齐地进行比较。

        这是这个想法的演示......它针对您的临时目录中超过 10 天的 *.tmp 文件。

        $SourceDir = $env:TEMP
        $Filter = '*.tmp'
        $MaxDaysOld = 10
        $Today = (Get-Date).Date
        
        $FileList = Get-ChildItem -LiteralPath $SourceDir -Filter $Filter -File -Recurse
        
        foreach ($FL_Item in $FileList)
            {
            $DaysOld = ($Today - $FL_Item.LastWriteTime).Days
            if ($DaysOld -gt $MaxDaysOld)
                {
                # remove or comment out the next two lines to stop showing the target info
                '=' * 30
                '[ {0} ] is [ {1} ] days old & can be removed.' -f $FL_Item.Name, $DaysOld
                # remove the "-WhatIf" to do this for real
                Remove-Item -LiteralPath $FL_Item.FullName -Force -WhatIf
                }
            }
        

        截断输出...

        ==============================
        [ hd410B2.tmp ] is [ 20 ] days old & can be removed.
        What if: Performing the operation "Remove File" on target "C:\Temp\hd410B2.tmp".
        ==============================
        [ hd412FA.tmp ] is [ 17 ] days old & can be removed.
        What if: Performing the operation "Remove File" on target "C:\Temp\hd412FA.tmp".
        ==============================
        
        [*...snip...*] 
        
        ==============================
        [ hd4FB42.tmp ] is [ 12 ] days old & can be removed.
        What if: Performing the operation "Remove File" on target "C:\Temp\hd4FB42.tmp".
        ==============================
        [ hd4FF36.tmp ] is [ 18 ] days old & can be removed.
        What if: Performing the operation "Remove File" on target "C:\Temp\hd4FF36.tmp".
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-14
          • 1970-01-01
          相关资源
          最近更新 更多