[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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
0
luketr
Asked:
luketr
  • 3
  • 3
1 Solution
 
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now