powershell script to search contents of file and move it to a different folder

I need a script to search the contents of a file and based on the title move to to a different folder - about 10 different possible values/folders. I think it can be done with the switch command but not much of a coder.
Thanks!
saladoilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

soostibiCommented:
Could you attach some samples of the text files and specify what kind of values and titles are there!
0
saladoilAuthor Commented:
They are reports so on one of the top several lines have a title such as Analysis, Discounts or Notes so based on what the title is want to move the files to a Analysis folder, etc.  Can't send samples as this is private data.
I got as far as using select-string -pattern "Notes" | copy-item etc but need to go through multiple files and send to different destinations.
Thanks
0
soostibiCommented:
It would be enough to copy the title area, retype any confidential data with dummy text.

But as a first try here is my code assuming that the first line of the text files look like:

Title: Analysis
Title: Discounts
Title: Notes

Please update:
- the first path in line 1 after dir for the source of the reports
- the root of the target folders in line 4 at the end of the move-item line

Of course the pattern should be updated based on the actual title sections.


dir c:\ee\*.txt | Select-String -Pattern "Title:\s(\w+)" -List| %{
    $file = $_.path
    $target = $_.matches[0].groups[1].value
    move-Item -Path $file -Destination "c:\ee\$target"
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saladoilAuthor Commented:
That doesn't seem to work - there are multiple reports and they aren't consistantly named Title: xxxx -
more like this:
REPORT    - ARB015B   AS OF 02/28/2011                                                                              RUN  03/05/2008
 COMPANY   -     3 .                                    ANALYSIS OF ACCOUNTS RECEIVABLE                               PAGE          1
 MARKET    -        NOT ON FILE
 TERRITORY - 73999  NOT ON FILE

                       TCO/SUSP  TOTAL     --------------- CURRENT --------  ------------ PAST DUE ----------  ---  NOTES --UA-CASH-
          CUSTOMER       CODE  RECEIVABLES  DISCOUNT  NET/NET-30   DATINGS     1 MONTH   2 MONTHS 3 MTH/OVER
                                             -ABLE
--
so I would want to use something like the switch command and select the string pattern and specify where to move the file to.
0
saladoilAuthor Commented:
I got the script to work for me so here are your points - thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.