powershell 備份檔案指令碼

來源:互聯網
上載者:User

由於項目需要改寫備份檔案的程式.

(原來的解決方案是用vb6 去遍曆檔案, 不吐槽了)


需求1: 檢查每個檔案的修改時間, 如果小於日期參數, 則備份(和刪除);

需求2:保留被備份檔案的目錄結構。


linux下備份檔案很簡單, 可惜我們只有windows Server。


百度不到。

為了效能花了2小時寫了個powershell 指令碼...


話說MS的東西跟開源方案比起來基本上都是垃圾。


例如C#比java, windows server 比linux, powershell比bash等等...

1個xargs解決的東西我tmd寫了個迴圈。

mdzz.


$scriptName = "archive.ps1";if (!($args.count -eq 3)){  write-host("Usage - " + $scriptName + " <<Source Path>> <<Destination Path>> <<beforeDate>>");  exit -1;}$pathFrom = $args[0];$pathTo = $args[1];$dateBefore = [datetime]::ParseExact($args[2],"yyyy-MM-dd",$null);write-host("source path is: " + $pathFrom);write-host("destination path is: " + $pathTo);write-host("archive files whose modified date is before: " + $dateBefore);if (!((Test-path -path $pathFrom) -and (Test-path -path $pathTo))){  write-host("source path or destination path is not existed!");  exit -1;}$pathCopyTo = "";$pathFromLen = $pathFrom.Length;$LenGap = 0;$LenGap2 = 0;ls -r $pathFrom | ?{$_.LastWriteTime -lt $dateBefore } | Foreach-Object{    $lenGap = $_.FullName.Length - $pathFromLen - $_.Name.Length - 1;    $pathCopyTo =  $PathTo + $_.FullName.Substring($pathFromLen, $lenGap);    write-host("copying " + $_.FullName + " to " + $pathCopyTo);    if(!(Test-path -path $pathCopyTo)){        write-host("creating folder " + $pathCopyTo );        mkdir -Pa $pathCopyTo;    }    copy-item $_.FullName $pathCopyTo -Force;}exit 0;


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.