【问题标题】:Powershell - Copy directory and files from source folder to destination folderPowershell - 将目录和文件从源文件夹复制到目标文件夹
【发布时间】:2020-01-20 21:51:27
【问题描述】:

我正在使用 PowerShell 设计一个场景。

截至目前,我正在尝试解决我有一个批处理作业的场景,该批处理作业在成功执行后首先为该特定日期创建一个日期文件夹,然后在其下创建一个 .CSV 文件。文件夹结构如下所示。

\\Server\SourceA\Processed\20200120\TestA.CSV

当作业在第二天运行时,它将创建另一个文件夹和文件,如下所示。

\\Server\SourceA\Processed\20200121\TestB.CSV

这就是过去已经创建了这么多文件夹的原因。

我的 PS 脚本可以在批处理作业完成后每天运行。我读了一个日期,附加到路径,它将文件从源文件夹复制到目标文件夹。但我想让我的 PS 脚本读取在

下创建的所有以前的 date 文件夹

\\Server\SourceA\Processed\

另一个棘手的部分是 - 在日期文件夹下几乎没有其他子文件夹。即

\\Server\SourceA\Processed\20191010\Log
\\Server\SourceA\Processed\20191010\Charlie
\\Server\SourceA\Processed\20191010\Alpha
\\Server\SourceA\Processed\20191010\Delta

其中,我只需要从Log 文件夹中读取文件。 因此,我的实际源路径变得像

\\Server\SourceA\Processed\20191010\Log\TestA.CSV

这是我的脚本(现在是静态的,无法读取过去的现有日期文件夹)。

$fullSourceFileName = "\\Server\SourceA\Processed\"
$date = Get-Date -format "yyyyMMdd"
$fullSourceFileName = "$($fullSourceFileName)$($date)\Log

$destination = "\\Server\DestA\Processed\"
$destination = "$($destination)$($date)\"

get-childitem -path $fullSourceFileName -recurse | copy-item -destination "$($destinationFolder)$($date)\"

非常感谢您的帮助。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我不知道我可以在 Powershell 中使用 foreach 循环。 所以,这里是读取我给定路径下所有动态日期文件夹的答案。 我希望这对社区有所帮助。

    $fullSourceFileName = "\\Server\SourceA\Processed\"
    $DirToRead = "\Log\"
    $dates = Get-ChildItem -Path $fullSourceFileName -Directory
    $destination = "\\Server\DestA\Processed\"
    
    foreach ($date in $dates){
            $ActualPath = "$($fullSourceFileName)$($date)$($DirToRead)"
            if(!(Test-Path $ActualPath))
            {
                 Write-Output $($ActualPath)$(" source path does not exist")
            }
            else
            {
                 get-childitem -path $ActualPath -recurse | copy-item -destination "$($destinationFolder)$($date)\"
            }
            $ActualPath = ""
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多