locked
Archiving old file from multiple folders RRS feed

  • Question

  • I have been using this script to archive old file to a new location. However we are getting more files to archive from different drives. My script works fine when it one location but when I try using Get-Content from a txt file with the list of path i am getting  "The given path's format is not supported."

    Below is the one that works for one location:

    Powershell
    $SourceFolder = "H:\pizza\"
    $Files = Get-ChildItem -Path $SourceFolder -Recurse -File | where {$_.LastWriteTime -lt (Get-Date).AddDays(-180)}
    $DestinationFolder = "\\ARCHIVED\ColdPizza\"
    
    foreach ($File in $Files)
    {
        $NewPath = $File.DirectoryName.Replace($SourceFolder,"")
    
        if (!(Test-Path -LiteralPath "$DestinationFolder\$NewPath"))
        {
            New-Item -Path "$DestinationFolder\$NewPath" -ItemType Directory
        }
    
        $File | Move-Item -Destination "$DestinationFolder\$NewPath"
    }


    So This is what I am trying to accomplish. I believe I need foreach sourcefile in sourcefile some where in there. Also the text file has ex:

    e:\pizza1\

    d:\pizza2\

    g:\pizza3\

    $folder = Get-Content "C:\pizza.txt"
    $Files = Get-ChildItem -Path $folder -Recurse -File | where {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
    $DestinationFolder = "\\ARCHIVED\cold pizza"
    
    
    foreach ($File in $Files)
    {
    
        $NewPath = $File.DirectoryName.Replace($folder,"")
    
        if (!(Test-Path -LiteralPath "$DestinationFolder\$NewPath"))
        {
            New-Item -Path "$DestinationFolder\$NewPath" -ItemType Directory
        }
    
    $File|Move-Item-Destination"$DestinationFolder\$NewPath"}
    
    

    Monday, May 13, 2019 11:06 PM

All replies

  • When posting a question it is necessary to post the complete error message.

    Folder names should not end with a "\". 

    Your logic for getting the new file name is faulty.  You cannot use an array of names in a replace statemen.



    \_(ツ)_/

    Monday, May 13, 2019 11:42 PM
  • Sorry about that, I'm new to this. I have also use it without the "\" prior to submitting this question. I'm not sure if this help but I run selection for $sourcefolder and the output showed the first path only. So the script partial works but it does not see the other paths. Excuse me but you lost me with " logic getting new file name is faulty" it not suppose to create new file names. It use the name from the old destination to create a new "folder" in the new location.
    Monday, May 13, 2019 11:55 PM
  • $folder is an array of folder names.  It cannot be used with "Replace".

    You have to decide what you are trying to replace.


    \_(ツ)_/

    Tuesday, May 14, 2019 12:12 AM
  • I got you. What would be the best approach cause my original script works fine for one path but since this server is getting more video file they needed to use more drives. Also they want to files archived every six months. Will I need to just run a script for each individual drive? Even though it not a big deal I just wanted to see if I can automated the process with one script.

    • Edited by SykooL Tuesday, May 14, 2019 12:38 AM
    Tuesday, May 14, 2019 12:37 AM
  • Enumerate the folders one at a time in a loop.


    \_(ツ)_/

    Tuesday, May 14, 2019 12:42 AM
  • Ok I'm new to powershell scripting but I seen some reference on loops. I will do some research and see what I can do. Thanks!
    Tuesday, May 14, 2019 1:05 AM
  • Hi,

    Thanks for your question.

    1. You need to use foreach loop twice. Firstly, Loop source files in a text file.Secondly, loop through files in one source path folder.

    2. You can create the new file path by the script below:

    $NewPath = $File.Directory.name
        $NewFilePath="$DestinationFolder\$NewPath"

    For example:

    $folder = Get-Content "C:\pizza.txt"
    foreach ($folder in $folder)
    {
      $Files = Get-ChildItem -Path $folder -Recurse -File | where {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
      $DestinationFolder = "\\ARCHIVED\cold pizza"
      foreach ($File in $Files)
      {
        $NewPath = $File.Directory.name
        $NewFilePath="$DestinationFolder\$NewPath"
        if (!(Test-Path -LiteralPath "$DestinationFolder\$NewPath"))
        {
            New-Item -Path $NewFilePath -ItemType Directory
        }
        $File|Move-Item-Destination "$DestinationFolder\$NewPath"
      }
    }

    Best regards,

    Lee


    Just do it.

    Tuesday, May 14, 2019 2:26 AM
  • Hi,

    Thanks for your question.

    1. You need to use foreach loop twice. Firstly, Loop source files in a text file.Secondly, loop through files in one source path folder.

    2. You can create the new file path by the script below:

    $NewPath = $File.Directory.name
        $NewFilePath="$DestinationFolder\$NewPath"

    For example:

    $folder = Get-Content "C:\pizza.txt"
    foreach ($folder in $folder)
    {
      $Files = Get-ChildItem -Path $folder -Recurse -File | where {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
      $DestinationFolder = "\\ARCHIVED\cold pizza"
      foreach ($File in $Files)
      {
        $NewPath = $File.Directory.name
        $NewFilePath="$DestinationFolder\$NewPath"
        if (!(Test-Path -LiteralPath "$DestinationFolder\$NewPath"))
        {
            New-Item -Path $NewFilePath -ItemType Directory
        }
        $File|Move-Item-Destination "$DestinationFolder\$NewPath"
      }
    }

    Best regards,

    Lee


    Just do it.

    Thanks, I tried this out but it just creates the folder in the destionation but it does not move the files. I provided the error.

    Move-Item-Destination : The term 'Move-Item-Destination' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path 
    was included, verify that the path is correct and try again.
    At C:\cleanup\test.ps1:15 char:11
    +     $File|Move-Item-Destination "$DestinationFolder\$NewPath"
    +           ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Move-Item-Destination:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Tuesday, May 14, 2019 3:41 PM
  • Hi,

    Sorry. It missed one space.

    $File|Move-Item -Destination "$DestinationFolder\$NewPath"

    Best regards,

    Lee


    Just do it.


    Tuesday, May 14, 2019 3:44 PM
  • Hi,

    Sorry. It missed one space.

    $File|Move-Item -Destination "$DestinationFolder\$NewPath"

    Best regards,

    Lee


    Just do it.


    Hey thanks for the help. So with the modification there was no error but it only did one path. When I run the selection for $folder it e:\pizza but prior to the last change it was seeing all the drives. We are get some where though!!! 

    Tuesday, May 14, 2019 5:34 PM
  • Get-Content "C:\pizza.txt" 
    If I run this part I shows all the path. If that helps
    • Edited by SykooL Tuesday, May 14, 2019 6:25 PM
    Tuesday, May 14, 2019 6:25 PM
  • So that's the thing...  Sometimes some people will expect you to understand PowerShell on this forum.  If you don't understand it, you either need to learn it or pay someone to do this for you.

    This was given to you previously:

    $folder = Get-Content "C:\pizza.txt" foreach ($folder in $folder)

    ...

    You pretty much blindly use that exact code without really knowing what's it's doing.  There's several ways to go about this, but for starters, the foreach should be more like:

    $folders = Get-Content "C:\pizza.txt" foreach ($folder in $folders)

    ...

    Tuesday, May 14, 2019 7:16 PM
  • With technical things it is allays necessary to understand the technology before trying to work with it.  This is fundamental to all things in this world.  First take time to read and study the technology.  If you cannot understand the material then ask simple questions about what you don't understand.

    With computer technology it is critical to have basic technical training in computers, operating systems and software.  Without that as a start everything else will be impossible to understand.

    Asking others to understand this for you is unfair and unprofessional.

    Here is how we would approach this solution in PowerShell.

    First write code that gets the file per folder.

    Get-Content C:\pizza.txt |
        Get-ChildItem -Recurse -File |
        Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } 

    This gets you most of the way to a solution.  Once you can understand how this works you are set to learn how to add the code that moves the files.


    \_(ツ)_/


    Tuesday, May 14, 2019 8:30 PM
  • Lesson #2 - What folder is the file in:

    Get-Content C:\pizza.txt -PipelineVariable fldr |
        Get-ChildItem -Recurse -File |
        Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
        ForEach-Object{
            Write-Host ('File {0} is in folder {1}.' -f $_.Name,$fldr) -fore green
        }
    

    Note the changes.  Try to figure out why they have been made and how they work.  Use PowerShell help and review the following tutorial.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/



    Tuesday, May 14, 2019 8:34 PM
  • Well you can tell I'm new to this but my background is more support. But I am pushing myself to learn more cause in a sick way I kind of enjoy doing this powershell scripting. Thanks for you patience for this newbie but it is working now!!
    Tuesday, May 14, 2019 8:42 PM
  • Lesson #3 - get the subfolder for the target dir creation.

    Get-Content content.txt -PipelineVariable fldr |
        Get-ChildItem -Recurse -File |
        Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
        ForEach-Object{
            # remove the root folder from the path
            $root = [regex]::Escape($fldr)
            $subfolder = $_.DirectoryName -replace $root
            Write-Host ('File {0} is from subfolder {1}.' -f $_.Name, $subfolder) -fore green
        }
    

    Now you can see how to build a script in layers.  Start from the beginning and add each component as you have solved each previous issue.

    Of course all of this requires thinking and understanding of basic technology and how to logically and technically analyze a problem.  To  make the above do what you want it requires only three more lines of code.


    \_(ツ)_/

    Tuesday, May 14, 2019 9:14 PM
  • Cool I will try to figure it out! My head is about to explode but I will learn this!!!
    Tuesday, May 14, 2019 9:57 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, June 3, 2019 7:36 AM