[英]Copy files to destination folder and also delete any that are not in source folder?
[英]Powershell - Copy directory and files from source folder to destination folder
我正在使用 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 = ""
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.