【问题标题】:How to fix a working script to be able to use it in multiple directories?如何修复工作脚本以便能够在多个目录中使用它?
【发布时间】:2019-03-27 20:20:09
【问题描述】:

首先让我说这个地方是一个很好的资源,它帮助我开发了一个运行良好的脚本,我正在考虑扩展它,但我在这样做时遇到了一些麻烦。

所以,基本上我可以根据这些年份的年份和月份将包含日志文件的目录组织到多个目录中。当我指向那些目录目录时,我可以这样做。

我要做的是将这些脚本应用于多个目录,基本上是(每个)多个目录中的数百个日志,但所有这些目录都在一个主目录中。

我尝试使用 foreach cmdlet 应用脚本,但它似乎不起作用。

#--------
# Creation of an "Archive" folder within the root directory of the targeted dir.
# Comments: Archives Folder is created in each targeted directory.
#--------

foreach($folder in (Get-ChildItem '\\drive\software\logstoreage    \directories' -Directory)){
    New-Item -ItemType directory -Path ($folder.fullname+"\Archive")
}

#--------
# Organization of Logs into Yearly and Monthly directories.  
# Comments: Folders are created based on the Year capturing logs through the end of 2018. 
#--------

$date = (Get-Date -Month 1 -Day 1 -Year 2019).ToString("01-01-2019")

$files = Get-ChildItem '\\drive\software\logstoreage\directories\SoftwareA' -Recurse | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer} 

$files

$targetPath = '\\drive\software\logstoreage\directories\SoftwareA\Archive'

foreach ($file in $files){
    $year = $file.LastWriteTime.Year.ToString()
    $month = $file.LastWriteTime.Month.ToString()

    $file.Name
    $year
    $month

    $Directory = $targetPath + "\" + $year + "\" + $month

    if (!(Test-Path $Directory)){
        New-Item $directory -type directory
    }

    $file | Move-Item -Destination $Directory
}

【问题讨论】:

  • 请提供一组目录和日志示例,以及脚本的预期结果。实际上,我猜这不起作用,因为您遍历目录,但实际上除了创建存档文件夹之外什么都不做。您需要扩展该循环以包含脚本的其余部分,而不是硬编码 $targetdirectory$files = 行中的路径。
  • 一旦您使用带有-Directory 参数的gci,然后使用gci 和Where-Object 检查!$_.PSIsContainer 而不是gci 的-File 参数?当您使用该 var 与 LastWriteTime 进行比较时,也不要强制转换 $date 字符串
  • @TheMadTechnician 例如,我将日志存储在 Logstorage 目录中,这些日志都与属于它们的软件相关联。我有 softwareA、SoftwareB、SoftwareC 等等(我有数百个)。例如:C:\Logstorage\Windows\SoftwareA\log1.csv;当我直接指向目录时脚本有效,但我没有时间一一输入。

标签: powershell subdirectory organization


【解决方案1】:

所以你有一个循环来获取所有目标文件夹,你只需要将脚本的其余部分包含在该循环中,并根据它在循环中进行一些修改。

foreach($folder in (Get-ChildItem '\\drive\software\logstoreage\directories' -Directory)){
    #--------
    # Creation of an "Archive" folder within the root directory of the targeted dir.
    # Comments: Archives Folder is created in each targeted directory.
    #--------
    $ArchiveFolder = New-Item -ItemType directory -Path ($folder.fullname+"\Archive") -Force


    #--------
    # Organization of Logs into Yearly and Monthly directories.  
    # Comments: Folders are created based on the Year capturing logs through the end of 2018. 
    #--------

    $date = Get-Date -Month 1 -Day 1 -Year 2019

    $files = Get-ChildItem $folder -Recurse -File | where {$_.lastwritetime -lt $date -and !$_.PsIsContainer} 

    $files

    $targetPath = $ArchiveFolder.FullName

    foreach ($file in $files){
        $year = $file.LastWriteTime.Year.ToString()
        $month = $file.LastWriteTime.Month.ToString()

        $file.Name
        $year
        $month

        $Directory = $targetPath + "\" + $year + "\" + $month

        if (!(Test-Path $Directory)){
            New-Item $directory -type directory
        }

        $file | Move-Item -Destination $Directory
    }
}

【讨论】:

  • 这行得通,非常感谢,现在对我来说更有意义了!
猜你喜欢
  • 2020-05-25
  • 2010-10-28
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2021-06-28
  • 2021-02-25
相关资源
最近更新 更多