Move file listed on csv file

Welcome Forums General PowerShell Q&A Move file listed on csv file

Viewing 9 reply threads
  • Author
    Posts
    • #192622
      Participant
      Topics: 1
      Replies: 4
      Points: 20
      Rank: Member

      Hello,

      I have folder with 3 millions files.  Files names format is 123_456_Name_country.pdf

      First part = Candidate id
      Second Part = Job ID
      I have csv files which have candidate id in format
      candidateid,
      123,
      345,
      321

      total number of Candidate ID on csv file is around 12000.

      Source Folder = D:\Source where all 3 millions files are

      I need powershell scripts which will move file to D:\Dest folder with all the candidateid in CSV files.
      Can anyone help me .

       

       

    • #192652
      Moderator
      Topics: 3
      Replies: 169
      Points: 889
      Helping Hand
      Rank: Major Contributor

      Hi Kundan,

      Did you try anything? Please post your code, then we can suggest you as needed. Writing code from scratch is a bit difficult here.

      Thank you.

    • #192775
      Participant
      Topics: 5
      Replies: 105
      Points: 262
      Helping Hand
      Rank: Contributor

      Maybe something like import IDs from csv to variable. Use this variable in the get-childitem include parameter and pipe the outcome to move-item. Please try first, post your code and then we can help. It's quite a simple one in the end if you put any effort in trying.

    • #192871
      Participant
      Topics: 1
      Replies: 4
      Points: 20
      Rank: Member

      Hello Kiran,
      THis is what I have but it does not work

      $Files = Get-childitem -Path d:\source

      $CandidateIDs = Import-CSV d:\source\candidate.csv

      ForEach($File in $Files){

      ForEach($ID in $CadidateIDs){

      If(($File.Name.Substring(0,3)) -eq $ID.candidateid){

      $MovedFile = "d:\dest\" + $File.Name

      move-item $File.FullName $MovedFile

      }

      }

      }

       

    • #192874
      Participant
      Topics: 5
      Replies: 105
      Points: 262
      Helping Hand
      Rank: Contributor

      Would this match the need?

      
      $IDs = Import-Csv "d:\source\candidate.csv" | select -ExpandProperty candidateid
      $filter = $IDs | foreach {"$($_.substring(0,3))*"}
      
      Get-ChildItem "d:\source\*" -Include $filter | select -expand fullname | Move-Item -Destination "d:\dest\"
      

      edit from import-certificate to import-csv

    • #192877
      Participant
      Topics: 1
      Replies: 4
      Points: 20
      Rank: Member

      I receive the error message

      Import-Certificate : Cannot validate argument on parameter 'Context'. The argument "d:\source\candidate.csv" does not belong to the set "CurrentUser,LocalMachine" specified by the
      ValidateSet attribute. Supply an argument that is in the set and then try the command again.
      At line:1 char:27
      + $IDs = Import-Certificate d:\source\candidate.csv | select -ExpandPro ...
      + ~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Import-Certificate], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,Import-Certificate

      You cannot call a method on a null-valued expression.
      At line:2 char:30
      + $filter = $IDs | foreach {"$($_.substring(0,3))*"}
      + ~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull

    • #192898
      Participant
      Topics: 1
      Replies: 4
      Points: 20
      Rank: Member

      But it odes copy all files and not only files on the csv

       

    • #192958
      Participant
      Topics: 5
      Replies: 285
      Points: 213
      Helping Hand
      Rank: Participant
      
      # move pdf files that start with candidateid in filename
      $CandidateID = (Import-CSV d:\source\candidate.csv).candidateid
      
      foreach ($c in $CandidateID){
      Get-ChildItem -Path "d:\source" -Filter $c*.pdf |
      move-Item -Destination "d:\dest"}
      
      
    • #192976
      Participant
      Topics: 1
      Replies: 4
      Points: 20
      Rank: Member

      Thank you . This is exactly what i was looking for

       

    • #193075
      Participant
      Topics: 5
      Replies: 105
      Points: 262
      Helping Hand
      Rank: Contributor

      tab completion error 😀

      
      $IDs = Import-csv "d:\source\candidate.csv" | select -ExpandProperty candidateid
      $filter = $IDs | foreach {"$($_.substring(0,3))*"}
      
      Get-ChildItem "d:\source\*" -Include $filter | select -expand fullname | Move-Item -Destination "d:\dest\"
      
      
Viewing 9 reply threads
  • You must be logged in to reply to this topic.