Solved

How can I configure the script not to create a new file if the original file is not present?

Posted on 2014-09-16
5
369 Views
Last Modified: 2014-09-16
I need to modify the following script so that if it can't find the original.txt file it will not create a newdatafile.txt

$InputFile = "C:\Programs\Temp\original.txt"
$OutputFile = "C:\Programs\Temp\newdatafile.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
$poline,$totline,$total = $NULL,$NULL,$NULL
Set-Content $OutputFile $null
}
Process{
      Get-Content -Path $InputFile | % {

If ($_ -match "AZI\*1"){
      If ($poline -ne $null -and $total -ne $null){
      $Totline = "Total: `$$Total"
      write-host "This is Total `$$total"
            "`n$poline `r`n$totline" | out-file $OutputFile -Encoding UTF8 -Append
            $poline,$totline,$total = $NULL,$NULL,$NULL
      }
}
   If($_ -match "(MA\*)(\d{0,10})") {
      $poline = "PONumber: $($Matches[2])"
    }Elseif ($_ -match "PO1"){
        $col=$_.split("*") ; $total += [double]$col[2] * [double]$col[4]
  }
 }
}
}

ParseText $OutputFile $InputFile


Currently, this will produce the new file regardless if it finds the input file or not.

It must not create the new file if the input file does not exist.
0
Comment
Question by:100questions
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Randy Poole
Comment Utility
$InputFile = "C:\Programs\Temp\original.txt"
$OutputFile = "C:\Programs\Temp\newdatafile.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
$poline,$totline,$total = $NULL,$NULL,$NULL
Set-Content $OutputFile $null
}
Process{
      Get-Content -Path $InputFile | % {

If ($_ -match "AZI\*1"){
      If ($poline -ne $null -and $total -ne $null){
      $Totline = "Total: `$$Total"
      write-host "This is Total `$$total"
            "`n$poline `r`n$totline" | out-file $OutputFile -Encoding UTF8 -Append
            $poline,$totline,$total = $NULL,$NULL,$NULL
      }
}
   If($_ -match "(MA\*)(\d{0,10})") {
      $poline = "PONumber: $($Matches[2])"
    }Elseif ($_ -match "PO1"){
        $col=$_.split("*") ; $total += [double]$col[2] * [double]$col[4]
  }
 }
}
}

if exist $InputFile ParseText $OutputFile $InputFile

Open in new window

Only call your procedure if the file exists
0
 

Author Comment

by:100questions
Comment Utility
This does not seem to work.

Missing '(' after 'if' in if statement.
At C:\.....\NEW.ps1
:29 char:4
+ if  <<<< exist $InputFile ParseText $OutputFile $InputFile
    + CategoryInfo          : ParserError: (OpenParenToken:TokenId) [], ParseE
   xception
    + FullyQualifiedErrorId : MissingEndParenthesisInIfStatement
0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
Comment Utility
Sorry seems you are using powershell, not standard batch
$InputFile = "C:\Programs\Temp\original.txt"
$OutputFile = "C:\Programs\Temp\newdatafile.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
$poline,$totline,$total = $NULL,$NULL,$NULL
Set-Content $OutputFile $null
}
Process{
      Get-Content -Path $InputFile | % {

If ($_ -match "AZI\*1"){
      If ($poline -ne $null -and $total -ne $null){
      $Totline = "Total: `$$Total"
      write-host "This is Total `$$total"
            "`n$poline `r`n$totline" | out-file $OutputFile -Encoding UTF8 -Append
            $poline,$totline,$total = $NULL,$NULL,$NULL
      }
}
   If($_ -match "(MA\*)(\d{0,10})") {
      $poline = "PONumber: $($Matches[2])"
    }Elseif ($_ -match "PO1"){
        $col=$_.split("*") ; $total += [double]$col[2] * [double]$col[4]
  }
 }
}
}

if (Test-Path $InputFile){ 
     ParseText $OutputFile $InputFile
}

Open in new window

0
 

Author Comment

by:100questions
Comment Utility
Thanks, yes I am, my apology for not mentioning it.
I will test this and get back to you.
0
 

Author Comment

by:100questions
Comment Utility
This works very well, excellent.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now