none
move older files(for example more than 30 days) to another path with directory by ps RRS feed

  • Question

  • Hello, Expert,

    I make a powershell to move older files to a path. I like to how to keep original directory? 

    The below script I am using. how to add new-item cmdlet to achieve that task?

    +++++

    $Sourcefolder= "G:\2. Common Procedures & Processes"

    $Targetfolder= "G:\test\New folder"
    Get-ChildItem -Path $Sourcefolder -Recurse|
    Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-180)
    }| move-Item -Destination $Targetfolder

    ++++

    brgds

    LiuWei

    Monday, July 22, 2019 7:05 AM

Answers

  • For this you should use RoboCopy.

    RoboCopy /?


    \_(ツ)_/

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 7:29 AM
  • Hi LiuWei,

    You can use

    $Sourcefolder = "C:\temp"
    $Targetfolder= "C:\temp2\New folder"
    Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinue

    All Files ... and directories are copied to the destination Path. The Tree is maintained.

    When you run, if  files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.

    Olivier

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 9:00 AM
  • Hi LiuWei,

    You can use

    $Sourcefolder = "C:\temp"
    $Targetfolder= "C:\temp2\New folder"
    Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinue

    All Files ... and directories are copied to the destination Path. The Tree is maintained.

    When you run, if  files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.

    Olivier

    How does this copy only files older than 30 days?  Perhaps you should read the whole thread before guessing at an answer.


    \_(ツ)_/

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 10:28 AM

All replies

  • For this you should use RoboCopy.

    RoboCopy /?


    \_(ツ)_/

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 7:29 AM
  • Hi LiuWei,

    You can use

    $Sourcefolder = "C:\temp"
    $Targetfolder= "C:\temp2\New folder"
    Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinue

    All Files ... and directories are copied to the destination Path. The Tree is maintained.

    When you run, if  files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.

    Olivier

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 9:00 AM
  • Hi LiuWei,

    You can use

    $Sourcefolder = "C:\temp"
    $Targetfolder= "C:\temp2\New folder"
    Copy-Item -Path $Sourcefolder -Destination $Targetfolder -Recurse -ErrorAction SilentlyContinue

    All Files ... and directories are copied to the destination Path. The Tree is maintained.

    When you run, if  files/Dir are already existing in the destination path : Error. Then use the parameter -ErrorAction Silently Continue to manage this.

    Olivier

    How does this copy only files older than 30 days?  Perhaps you should read the whole thread before guessing at an answer.


    \_(ツ)_/

    • Marked as answer by lwsamcn Wednesday, July 24, 2019 6:14 AM
    Monday, July 22, 2019 10:28 AM