?
Solved

Powershell Script to move log file based on text within file.

Posted on 2011-10-13
9
Medium Priority
?
2,007 Views
Last Modified: 2012-05-12
All,

I need a powershell script that will move files from a local server to a UNC based on text within the file.

Example:
I have failed request trace log files being written to the servers local hard drive. I need only files that contain a certain URL path listed in the file moved to a UNC. All other files that do not contain this specific text(path) to remain on the servers local hard drive. So let's say I want to move files that contain "*/website_folder/*" in the file where "*" are wildcard characters.

The script should be written in powershell not vbscript. The script should be configurable so that the script can be pointed to a specific folder to scan and a specific folder (UNC) to move files too.

TIA...
0
Comment
Question by:mobot
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Assisted Solution

by:Dale Harris
Dale Harris earned 1000 total points
ID: 36963484
This looks to be pretty straightforward:

$UNC = "\\192.168.0.1\ArchivedTraceLogs"

$PathToLogFiles = "C:\TraceLogsFolder"

$SearchString = "Website_folder"
#Assuming all the files in there are .txt files and no subsequent folders

$Files = gci $PathToLogFiles -filter *.txt

Foreach ($File in $Files){
gc $File | ?{$_.contains("$SearchString")} | Copy-Item $File.fullname $UNC | Remove-Item $File.fullname -whatif
}


I have not tested this script in terms of actually copying and removing

I've also added the -whatif parameter to prevent you from actually deleting any files.  I would verify first without the | Remove_item.... portion.  Make sure it's copying the files that applies to your search filter.  Remember when you're doing your search filter, you don't have to put the * on each side.  You're looking for "/Website_folder/" or a key word.  Don't use the asterisks for the search string.

If you have multiple folders with .txt files in them under your main path to the log files, then you're going to want to put the -recurse parameter after GCI so it looks like this:


$Files = gci $PathToLogFiles -filter *.txt -recurse

HTH,

Dale Harris
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 37048040
I would use this, which is shorter:
$DestDir = "\\192.168.0.1\ArchivedTraceLogs"
$SrcDir = "C:\TraceLogsFolder"
$SearchString = "Website_folder"

gci $SrcDir -filter *.txt | select-string $SearchString | select path | move-item -dest $DestDir -whatif

Open in new window

The same as said above applies in regard of adding -recurse to gci, and remove -whatif to perform the move.
0
 

Author Comment

by:mobot
ID: 37298797
Will try
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Dale Harris
ID: 37298842
Qlemo, "move-item" doesn't seem to work between different drives, so I would caution not using that, but instead using Copy-Item then Remove-Item.

Yes, you posted something more succinct, but I'm trying to go for two things as well: ability to easily add/edit the code without too much worry (foreach blocks instead of using the $files | feature), and readability for future scripters/beginners.

DH
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 37298950
First, move-item works between different drives.

Second, I do not see any advantage in not using the advanced features PS provides, and hence need to code more. "More lines" is not the same as "better readability". In particular if the more compact code does not obscur anything. As-is, the scripts sounds logical, at least to me.
In addition, foreach () often requires you to do extra work on the processed objects (here getting the full name from $File). I do not think that improves readability at all, and introduces another pifall to avoid.

We can discuss whether the pipelined foreach (foreach-object) or the looping one (foreach) is to be preferred at all times, but never have both any advantage over directly passing objects thru the pipeline, and let the cmdlets do their magic automatically and more efficient.
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37299079
Qlemo,

I suppose I'm simply defending the fact that you came by 15 days after I offered a "correct" answer with a more succinct one and doesn't add a ton of new functionality.  If I offered 15 lines of code and you offered 1 then that's a great improvement.  But what are we really gaining from your second answer which simply shortens it by essentially 1 line, but possibly obscures the behavior from the beginner?  If he asked for the fewest lines possible, I would have coded it with fewer lines in mind.  I decided to make it more "readable" by others as well.  We live in a world where most people haven't fully grasped the idea of PowerShell, so I'm always looking to teach others through our re-usable answers.  Which is why we comment our PowerShell code.

Mine: 5 Lines
$UNC = "\\192.168.0.1\ArchivedTraceLogs"
$PathToLogFiles = "C:\TraceLogsFolder"
$SearchString = "Website_folder"
$Files = gci $PathToLogFiles -filter *.txt
Foreach ($File in $Files){gc $File | ?{$_.contains("$SearchString")} | Copy-Item $File.fullname $UNC | Remove-Item $File.fullname -whatif}

Yours: 4 Lines
$DestDir = "\\192.168.0.1\ArchivedTraceLogs"
$SrcDir = "C:\TraceLogsFolder"
$SearchString = "Website_folder"
gci $SrcDir -filter *.txt | select-string $SearchString | select path | move-item -dest $DestDir -whatif

DH

P.S. if you want to continue this debate off this thread, I'm on Google+.  I don't think we should be using this thread any further to discuss these minor viewpoint differences.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 37299372
That is is a point of view only has been expressed by my introductionary "I would use this".

Of course you can defend your code, but the fact is 15 days passed between your and my post is not relevant.
Further, you are wrong. My code is different, and shows a different approach. And you started the discussion, by stating something wrong in http:#a37298842, so I had to post.

Can you tell me why you use a pipeline for | copy-item ... | remove-item ? There should be nothing passing here (since the pipeline content is not used).

Looking more into the code, we both failed to see that the move is executed too often (for each matching line instead of for each file with a matching line). That could result into superfluous error messages, since the file is no longer there. Hence correction:
$DestDir = "\\192.168.0.1\ArchivedTraceLogs"
$SrcDir = "C:\TraceLogsFolder"
$SearchString = "Website_folder"

gci $SrcDir -filter *.txt | select-string $SearchString -list | select path | move-item -dest $DestDir -whatif

Open in new window

0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37300116
I see what you mean about doing the move if the file contains more than one line where the line matches.  I think we both assumed it would only exist once in the entire file, but it's more efficient the way you put it.

I did make the mistake of saying Move-Item didn't work between drives as I'd had issues with that in the past, and didn't know when they fixed that.

When you are asking about the pipeline of using copy-item and remove-item, that was an oversight of mine fixed with your move-item change.

Overall, good catch and I hope that mobot gets a chance to try this out and see if it works for his requirements.

DH
0
 

Author Closing Comment

by:mobot
ID: 37601653
Hey guys a colleague of mine opened this and must have forgot about it.  Sorry about that, I'm going to split the points so we can close this question.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Loops Section Overview
Screencast - Getting to Know the Pipeline

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question