none
[PowerShell] Search specify file, then copy it to folder using Get-Content, Foreach-Object, get-childitem.

    Question

  • Hi everyone,  

           I would like to run a powershell script that will search all *.jpg files in a specified directory by a specified list. The script will then copy the files to specified folder. 

    ########################################################################################

          My test code as below

          $filelist = 'J:\test\target_list.txt'

          $source='J:\test\source'

          $destination = 'J:\test\find'
         

          Get-Content $filelist | Foreach-Object {
              Get-ChildItem -Path $source -Recurse -Include *.jpg -Filter $_ | Copy-Item -Destination $destination
          }

    #########################################################################################

           I exepect Get-Content pipe the item of filelist one by one to Foreach-Object $_ variable, and then search $source using Get-ChildItem

           if match, copy it to $destination, if not, do nothing.

           In fact, when I running script, all *.jpg files under  $source copied to $destination. It seem no filter with specified $_ variable.

           I am pretty sure source folder include target file.

           Can someone help me figure out what I miss or what I wrong.

           I spent over one week to study and research PowerShell tutorial, also here script forums.

           I know here must be a answer which alread returned. But I didn't find out.

           HELP.... really!!  Or someone can pass me duplicate problem link to me.

           Thank you very much.

         

    Wednesday, December 5, 2018 4:46 AM

All replies

  • Just do this:

    Copy-Item (Get-Content $filelist) -Destination $destination


    \_(ツ)_/

    Wednesday, December 5, 2018 4:53 AM
  • Hi Jrv, 

         Thanks for your reply.

         Sorry for my poor English. I didn't make clear for my $filelist content.

         My $filelist content like below

         #########################################

         top440137488x379324712_3606800x3606800_0_0.jpg      
         bottom445357183x378290124_8712200x3632200_0_0.jpg   
         bottom445357183x378290124_8712200x3632200_0_0.jpg   
         bottom404552073x372898510_4419600x4419600_0_0.jpg   
         bottom87179075x375489320_4419600x4419600_0_0.jpg    
         bottom452572713x358376842_1447800x1447800_0_0.jpg   
         top457018229x356198025_1447800x1447800_0_0.jpg      
         top457018229x355359825_1447800x1447800_0_0.jpg      
         top456027616x356198025_1447800x1447800_0_0.jpg      
         top456027616x355359825_1447800x1447800_0_0.jpg      
         bottom198888278x355372522_5283200x5283200_0_0.jpg   
         bottom203079278x355372520_5283200x5283200_0_0.jpg

    ###########################################

        Only know filename, no filepath info.

        Even that!! I still can't get your point in use  

         Copy-Item (Get-Content $filelist) -Destination $destination


    • Edited by Tzu-Chiang Wednesday, December 5, 2018 5:49 AM not finished
    Wednesday, December 5, 2018 5:44 AM
  • Hi Jrv, 

           Follow your suggestion I research official Powershell doc again.

          I assume you want me do this like below

        
    Get-Content $filelist | Foreach-Object {  Copy-Item -Filter $_ -Path $source -Recurse -Destination $destination     }

          The result is the same. Script copy all jpg files to destination. 

           -Filter no functional.

    Wednesday, December 5, 2018 7:19 AM
  • Simple:

    Push-Location J:\test\source
    Copy-Item (Get-Content $filelist) -Destination $destination
    Pop-Location
    


    \_(ツ)_/

    Wednesday, December 5, 2018 7:44 AM