powershell - Powershell - 将目录和文件从源文件夹复制到目标文件夹
问题描述
我正在使用 PowerShell 制定一个方案。
截至目前,我正在尝试锻炼一个场景,其中我有一个批处理作业,在成功执行后首先为该特定日期创建一个日期文件夹,然后在下面创建一个 .CSV 文件。文件夹结构如下所示。
\\Server\SourceA\Processed\20200120\TestA.CSV
当作业在第二天运行时,它将创建另一个文件夹和文件,如下所示。
\\Server\SourceA\Processed\20200121\TestB.CSV
这就是过去已经创建了这么多文件夹的原因。
我的 PS 脚本可以在批处理作业完成后每天运行。我读了一个日期,附加到路径,它将文件从源文件夹复制到目标文件夹。但我想让我的 PS 脚本读取所有以前创建的日期文件夹
\\Server\SourceA\Processed\
另一个棘手的部分是 - 在日期文件夹下几乎没有其他子文件夹。IE
\\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 中使用 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 = ""
}
推荐阅读
- angular - Angular 7 Reactive Forms:重置验证器和整个表单包含的问题
- spring - Spring JCache 记录缓存命中
- png - 加载 PNG RGBA 通道,如交错的 JPG RGB 通道
- python - 对称函数的 numpy.gradient 的分量不同
- java - Java 检查递增/递减模式
- rocksdb - 是否可以不覆盖 RocksDB 中的早期值?
- python - 使用 OpenVINO 运行 Facenet
- xamarin.forms - 更改列表视图中的所有 xamarin 表单按钮背景
- javascript - SocketIO:如何从前端克服 CORS?
- api - 谷歌地图 api - 添加 alt 属性以关闭标记按钮