PowerShell backup File Script

Source: Internet
Author: User
Tags exit

The program needs to overwrite the backup file because of the project.

(The original solution is to use VB6 to traverse the file, do not spit slot)


Requirement 1: Check the modification time of each file, if it is less than the date parameter, then backup (and delete);

Requirement 2: Keep the directory structure of the backed up files.


Backing up files under Linux is simple, but we only have Windows Server.


Baidu is less than.

It took 2 hours to write a PowerShell script for performance ...


The words MS is basically rubbish compared with the open source scheme.


For example C # than Java, Windows Server than Linux, PowerShell than bash, etc...

1 Xargs solved the thing I have a TMD to write a loop.

Mdzz.


$scriptName = "Archive.ps1"; if (! ( $args. Count-eq 3) {write-host ("Usage-" + $scriptName + "<<source path>> <<destination Path>&gt ;
  <<beforeDate>> ");
exit-1;
} $pathFrom = $args [0];
$pathTo = $args [1];
$dateBefore = [datetime]::P arseexact ($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;

$LenGap 2 = 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;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.