Copying Files to directory which name contains expression

Welcome Forums General PowerShell Q&A Copying Files to directory which name contains expression

Viewing 8 reply threads
  • Author
    Posts
    • #259465
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Hello Guys,

      I’m new in forum and also new in powershell community. I got a task to write a script which copies file to folders, which matches expression in name. For example, I got these filenames:

      file1: C:\Documents\M_20190103_0609_GDA48H2 _YS2R4X20002122620.ddd

      file2: C:\Documents\M_20190812_1459_WGM 68110_XLRTEH4300G277302.ddd

      file3:C:\Documents\M_20191216_1705_WGM_14315_XLRTEH4300G155004.ddd

      These files must be copied to these folders:

      C:\Documents\M_20191216_1705_WGM_14315_XLRTEH4300G155004 -> D:\Cars\WGM14315

      C:\Documents\M_20190812_1459_WGM 68110_XLRTEH4300G277302.ddd -> D:\Cars\WGM68110

      C:\Documents\M_20191216_1705_WGM_14315_XLRTEH4300G155004.ddd ->D:\Cars\WGM14315

      I wrote some code but it does not work that i won’t. It creates the directory that matches regex, but dont copy to folder. Here is my code:

      These code creates a directory matches regex and copies files matches regex, but it doesn’t copy to destination folders. Is this possible to copy files to destination directories without creating unnecessary directories? Maybe hash table will be useful? I tried:

      The hash map is correct, but it doesn’t copy files from hash key to directories that are hash values.

      Any suggestion?I will be very grateful for your help in solving the problem.

      • This topic was modified 2 weeks, 6 days ago by kraci3j89.
      • This topic was modified 2 weeks, 6 days ago by kvprasoon. Reason: code formatting
      • This topic was modified 2 weeks, 6 days ago by kraci3j89. Reason: Editing variables. New to forum and learning formatting
      • This topic was modified 2 weeks, 6 days ago by kvprasoon.
    • #259480
      Senior Moderator
      Topics: 9
      Replies: 1365
      Points: 5,024
      Helping Hand
      Rank: Community MVP

      Are you sure on the above cod ? $new_reg and $tacho_dirs are not a defined variable and $documents is just a string and not a collection here to iterate. you would wanna use $files as it has the proper file collection.

    • #259489
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Are you sure on the above cod ? $new_reg and $tacho_dirs are not a defined variable and $documents is just a string and not a collection here to iterate. you would wanna use $files as it has the proper file collection.

      Sorry, i Wrote wrong variables – $new_reg is $regex and $tacho_dir is $dest_dir.

    • #259492
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Kvprasoon any suggestions to work it out? Beacuse I’m stuck with this too much time unfortunately.

    • #259501
      Senior Moderator
      Topics: 9
      Replies: 1365
      Points: 5,024
      Helping Hand
      Rank: Community MVP

      lemme not write code.

      you are almost there. Iterating through file collection and matching is an okay approach. You can better do it with a Where-Object cmdlet connecting with Get-ChildItem so that $files will have more filtered output.

      Then its a matter of iterating foreach $file in $files and as you already did, joining path and creating the folder if not available(you regex match will give the match with _ like WGM_14315. Finally you don’t need to join the path again as you have the joined path in $new_path variable. You can use $new_path.FullName as -Destination.
      Copy using -Path as $File.FullName and -Destination as $new_Path.FullName

    • #259507
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Kvprasoon, thank you for quickly reply. Yes, this code works for creating directories like WGM_14315 and copy files to this directories, but not copy to correct directories like WGM14315. I will implement your advices to code and reply if works or not. Thank you 🙂

    • #259516
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Kvparsoon, i changed line responsible for copy and for iteration, but it still doesn’t work correctly. Can you help me with some code? Or maybe is a possibility to rename created directories (WGM_14315) to WGM14315 when the right directory(WGM14315) exists? I tried with move-item and -force parameter but it doesn’t work too.

    • #259522
      Senior Moderator
      Topics: 9
      Replies: 1365
      Points: 5,024
      Helping Hand
      Rank: Community MVP

      you just need to replace _ from $Matches[0]. Use ($Matches[0] -replace ‘_’,”) with Join-Path cmdlet.

    • #259534
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Kvparsoon, yes! I Just changed it before you post and it works. Thank you so much!! 🙂

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