none
Read text file line by line RRS feed

  • Question

  • Hi,

    I trayng to make a script that read the content from a text file, read from that text file line by line, search for that content in 10 directories, and copy the files to a new directory.

    I am new to powershell, please do you have some ideas.

    Thank you

    Wednesday, January 6, 2016 11:03 AM

Answers

  • Start by learning powershell ;) there are tons of material available, e. g.: http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/04/weekend-scripter-the-best-ways-to-learn-powershell.aspx

    To get you started have a look at:

    • get-content to read a text file
    • get-childitem to look in your directorys
    • copy-item to copy files
    • get-help to understand what each command does usage: get-help command e. g. get-help get-content

    Regards, Olaf

    Wednesday, January 6, 2016 11:50 AM
  • Something like this?

    $SearchString = Get-Content "C:\Look_for_this.txt"
    $TargetDir = "C:\Found_Files"

    $SearchDir = "C:\Search_Files"

    Foreach ($File in (Get-ChildItem $SearchDir -Recurse -File))
        {Foreach ($line in (Get-Content $File.FullName))
            {If ($line -like "*$SearchString*")
                {Copy-Item $File.FullName $TargetDir}
            }
        }

    • Proposed as answer by GL004 Thursday, January 7, 2016 8:09 PM
    • Marked as answer by Elaine JingModerator Thursday, January 14, 2016 2:33 AM
    Thursday, January 7, 2016 8:04 PM

All replies

  • Start by learning powershell ;) there are tons of material available, e. g.: http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/04/weekend-scripter-the-best-ways-to-learn-powershell.aspx

    To get you started have a look at:

    • get-content to read a text file
    • get-childitem to look in your directorys
    • copy-item to copy files
    • get-help to understand what each command does usage: get-help command e. g. get-help get-content

    Regards, Olaf

    Wednesday, January 6, 2016 11:50 AM
  • Hello avi9992

    Welcome for PowerShell Scripts.. :) 

    Please be more specific regarding your request, The files that you want to copy to "new directory" are the files that you found the content inside them? do you want to search a recursive inside those 10 directories? What kind of content you tried to search?

    Thanks

    Aviv


    Wednesday, January 6, 2016 5:00 PM
  • Hello Aviv,

    Thank you for the replay

    ok, i want to read the content of a text file with "get-content" the results of that command i want to search in many directories recursive, find them and copy the to a new directory.

    Hope this will help

     

    Thank you

    avi

    <o:p></o:p>


    • Edited by avi9992 Thursday, January 7, 2016 12:47 PM
    • Proposed as answer by GL004 Thursday, January 7, 2016 8:01 PM
    • Unproposed as answer by GL004 Thursday, January 7, 2016 8:02 PM
    Thursday, January 7, 2016 12:38 PM
  • Something like this?

    $SearchString = Get-Content "C:\Look_for_this.txt"
    $TargetDir = "C:\Found_Files"

    $SearchDir = "C:\Search_Files"

    Foreach ($File in (Get-ChildItem $SearchDir -Recurse -File))
        {Foreach ($line in (Get-Content $File.FullName))
            {If ($line -like "*$SearchString*")
                {Copy-Item $File.FullName $TargetDir}
            }
        }

    • Proposed as answer by GL004 Thursday, January 7, 2016 8:09 PM
    • Marked as answer by Elaine JingModerator Thursday, January 14, 2016 2:33 AM
    Thursday, January 7, 2016 8:04 PM