Copying of last modified files to another folder and following structure.

Welcome Forums General PowerShell Q&A Copying of last modified files to another folder and following structure.

Viewing 5 reply threads
  • Author
    Posts
    • #195653
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      Hi, I am a newbie to powershell.My requirement is copying last modified file to destination folder.The files and subfolder name also need to copied.

      Let me explain clearly by structure.

      Source

      Subfolder:1

      Text1.

      Text2(last modified)

      Subfolder 2

      Text3

      Text4(last modified)

      Subfolder

      Text5

      Text6

      Output:This is what I need

      Destination:

      Subfolder1

      Text2

      Subfolder2

      Text4

    • #195674
      Participant
      Topics: 2
      Replies: 1693
      Points: 3,368
      Helping Hand
      Rank: Community Hero

      Did you consider using robocopy? It has plenty of options to specify the files to copy.

      Did you try to find something fitting in the PowershellGallery?

    • #195677
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      I am using script like this..But this copies only files but not sub folder name

      $Targetfolder= "C:\Users\Vamsy\desktop\Continuous Integration\Target"
      
      $Sourcefolder= "C:\Users\Vamsy\desktop\Continuous Integration\Source"
      
      $files = get-childitem $Sourcefolder -file | 
                where-object { $_.LastWriteTime -gt [datetime]::Now.AddMinutes(-5) }|
                Copy-Item -Path $files -Destination $Targetfolder -recurse -Force
    • #195683
      Participant
      Topics: 2
      Replies: 1693
      Points: 3,368
      Helping Hand
      Rank: Community Hero

      Please take a look at robocopy. I linked the documentation in my first answer. There's actually no need for a script.

    • #195701
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      But robocopy operation is used only for date..but not in minutes..

       

    • #195752
      Participant
      Topics: 2
      Replies: 1693
      Points: 3,368
      Helping Hand
      Rank: Community Hero

      OK, you did not specify the desired age in your first post.

      With this requirement you will have to extract the part of the source path you want to mirror in the target, create the target folder first before you're able to copy the file to the newly created desitnation folder.

      There are several examples throughout the internet for Powershell scripts with this kind of requirements. You should search for it in the PowershellGallery or StackOverflow or in the Microsoft Technet Forums. You don't have to re-invent the wheel.

      If you like to do it by yourself anyway ... the following example illustrates how to extract the target folder structure:

      $StartFolder = 'D:\sample\test'
      $FileFullName = 'D:\Sample\Test\SubFolder1\Test2\File.txt'
      $SourceFolder = Split-Path $FileFullName -Parent
      $TargetFolderStructure = $SourceFolder -replace [regex]::Escape($StartFolder)
      $TargetFolderStructure

      BTW: You could leverage the ability of robocopy to filter on the archive attribute with the option /M

Viewing 5 reply threads
  • You must be logged in to reply to this topic.