Script to fins text in a file and then move the file and rename it

Hi,

I have a interface file that generated from a database and what I would like is a script that will do the following please.

I need the script to look from line 2 in the file and always in column position 22 there will be either 1,2,3,4,5,6. If 2 appears then I need to move this file to another location. The file could contain 100's on lines.

I have attached an example file.

When the file is moved I would like to rename it to Completions_rejected_ followed by the date and time stamp of when the file was created.

Many thanks

Luke
im---Job-Card-Completions---bjccmim4.txt
luketrAsked:
Who is Participating?
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.

luketrAuthor Commented:
Could the script be in power shell if possible please.
0
oBdACommented:
Could you please elaborate what you need? How do you want to specify the file (or file(s)) to be scanned?
Do you have a folder full of these files and want to process all of them, or is that a script that needs to process a single file when it's called?
And did I understand you correctly that if there is a 2 in column 22 in any line of the file, it should be moved?
0
luketrAuthor Commented:
If we use d:\temp as the example and there could be multiple files in here at any given time.

So can we scan the d:\temp directory for files contained within the directory that have the number 2 in column position 22 in any line apart from line 1.

If the scan finds 2 in column 22 in any line I want to leave the contents of the file alone and just to move the file and rename it .

Thanks
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

oBdACommented:
Try this then; it's in test mode and will only display what it would do; uncomment line 17 to run it for real:
$SourceFolder = "C:\Temp"
$FileMask = "*.txt"
$TargetFolder = "D:\Temp"
$Column = 22
$Char = "2"
$Column -= 1
ForEach ($FileItem In (Get-ChildItem -Path $SourceFolder -Filter $FileMask)) {
	"Processing '$($FileItem.Name)' ... " | Write-Host -NoNewLine
	$LineCount = 0
	$Moved = $False
	ForEach ($Line In (Get-Content -Path $FileItem)) {
		$LineCount += 1
		If (($LineCount -gt 1) -And ($Line.Length -gt $Column)) {
			If ($Line.SubString($Column, 1) -eq $Char) {
				$TimeStamp = Get-Date $FileItem.LastWriteTime -Format "yyyy-MM-dd_HH-mm-ss"
				$DestinationName = "Completions_rejected_" + $TimeStamp + $FileItem.Extension
				# $FileItem | Move-Item -Destination (Join-Path $TargetFolder $DestinationName)
				$Moved = $True
				Break
			}
		}
	}
	If ($Moved) {
		"'$($Char)' found in line $($LineCount), moved to '$DestinationName'." | Write-Host -ForegroundColor Red
	} Else {
		"'$($Char)' not found." | Write-Host -ForegroundColor Green
	}
}

Open in new window

0
luketrAuthor Commented:
I've placed the test file in C:\Temp but I am getting the following error.

rocessing 'TEst.txt' ... Get-Content : Cannot find path 'C:\Users\luketr\TEst.txt' because it does not exist.
At line:11 char:32
+     ForEach ($Line In (Get-Content <<<<  -Path $FileItem)) {
    + CategoryInfo          : ObjectNotFound: (C:\Users\luketr\TEst.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

As shown below in screen shot.

Thanks

Luke
error.jpg
0
oBdACommented:
Sorry:
$SourceFolder = "C:\Temp"
$FileMask = "*.txt"
$TargetFolder = "D:\Temp"
$Column = 22
$Char = "2"
$Column -= 1
ForEach ($FileItem In (Get-ChildItem -Path $SourceFolder -Filter $FileMask)) {
	"Processing '$($FileItem.Name)' ... " | Write-Host -NoNewLine
	$LineCount = 0
	$Moved = $False
	ForEach ($Line In (Get-Content -Path $FileItem.FullName)) {
		$LineCount += 1
		If (($LineCount -gt 1) -And ($Line.Length -gt $Column)) {
			If ($Line.SubString($Column, 1) -eq $Char) {
				$TimeStamp = Get-Date $FileItem.LastWriteTime -Format "yyyy-MM-dd_HH-mm-ss"
				$DestinationName = "Completions_rejected_" + $TimeStamp + $FileItem.Extension
				Move-Item -Path $FileItem.FullName -Destination (Join-Path $TargetFolder $DestinationName) -Force
				$Moved = $True
				Break
			}
		}
	}
	If ($Moved) {
		"'$($Char)' found in line $($LineCount), moved to '$DestinationName'." | Write-Host -ForegroundColor Red
	} Else {
		"'$($Char)' not found." | Write-Host -ForegroundColor Green
	}
}

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.