[英]How to automate copy files from one server location to another and move from the source location?
[英]How can i create a powershell script to move thousands of log files from one location to another?
我在服務器上有一個包含數千個日志文件的文件夾。 每秒都會填充新文件,文件夾會繼續增長。 每周一次我想要獲取這些文件,復制它們並將它們粘貼到另一個文件夾中。 然后我將運行python腳本來處理新文件夾中的日志。 我的腳本每周處理大約70K日志,需要一個多小時。
如何讓它更高效/更快?
$ScriptProperties = @{
"FolderDir" = "\\server\folder1\Temp";
"FolderName" = "Orignal_Folder";
"OldFolderName" = "New_Folder";
"TempFolderName" = "Temp_Folder";
}
#$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
# if (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)"){
# #write-host "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName) folder folder"
# Copy-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -Destination "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)-$($DateStamp)" -force -recurse
# #New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory -force
# }#else{
# #write-host "folder not found."
# New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory
#}
write-host "Start"
if (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)"){
#Renaming folder to Temp directory
Rename-Item "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -NewName "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)"
#Creating new Log directory
New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.FolderName)" -type directory
#Creating new Move folder if not found
if (-Not (Test-Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)")) {New-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)" -type directory -force}
#Moving content to move directory
Move-Item -Path "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)\*.*" -Destination "$($ScriptProperties.FolderDir)\$($ScriptProperties.OldFolderName)" -force
#Removing temp directory
Remove-Item "$($ScriptProperties.FolderDir)\$($ScriptProperties.TempFolderName)" -Force
}
write-host "Complete"
@Ansgar推薦robocopy; 為什么?! 您已經在PowerShell中,如果可以避免,請不要再使用cmd和批處理腳本十年!
此外,小心一招! 如果某些內容中斷您的轉移,則表示您的數據已損壞或丟失。 你甚至沒有檢查你轉移的文件大小是否相等; 怎么會失敗? 在這里你無論如何刪除它。 我會考慮添加某種邏輯來檢查復制前后的文件大小。
考慮使用BITS傳輸來避免這種情況,並在確認文件大小相等后刪除您移動的文件。 另一個好處是,如果因任何原因中斷,它將從你離開的地方繼續復制。
您可以在復制完成之前執行異步操作並可能啟動處理數據。 這應該會為您提供有關您所擁有的選項的更多詳細信息: https : //msdn.microsoft.com/en-us/library/ee663885(v = vs。85).aspx 。 這是Start-BitsTransfer Cmdlet的TechNet頁面: https ://technet.microsoft.com/en-us/library/dd819420.aspx,用於參考語法和參數選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.