Solved

Powershell question. I need the script to skip over certain file names.

Posted on 2016-11-01
3
40 Views
Last Modified: 2016-11-02
I have this script that I need it to skip over files that already have a _cnc3 ending in them and continue on to the next file.

param( [string] $fileName)

# Check the file exists
if (-not(Test-Path $fileName)) {break}

# Display the original name
"Original filename: $fileName"

$fileObj = get-item $fileName

# Get the Add
$DateStamp = "_cnc3"
$extOnly = $fileObj.extension

if ($extOnly.length -eq 0) {
   $nameOnly = $fileObj.Name
$content = Get-Content $fileObj.Name
$content = $content -replace "T101", "T102";
$content = $content -replace "T103", "T102" ;

$content | Set-Content "$nameOnly-$DateStamp"
}
else {
   $nameOnly = $fileObj.Name.Replace( $fileObj.Extension,'')
$content = Get-Content $fileObj.Name  
$content = $content -replace "T101", "T102";
$content = $content -replace "T103", "T102" ;
$content | Set-Content "$nameOnly$DateStamp$extOnly"

}


I call this script to run from another file. I drop both files in the folder that I want this to run in.

foreach ($file in get-ChildItem *.txt) { ./CRMe.ps1 $file.name }

write-host "Press any key to continue..."
[void][System.Console]::ReadKey($true)
0
Comment
Question by:bobgraf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 41869109
at the top of your function/script,
if ($filename -like '*_cnc3*') {break}

or,
in your foreach,

foreach ($file in (get-ChildItem *.txt | where {$_ -notlike '*_cnc3*'} ) { ./CRMe.ps1 $file.name }
0
 
LVL 13

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 250 total points
ID: 41869121
Wrap your action in an if statement that checks if the end of the filename is _cnc3.  See this example:

$fn = "filenameonetwothree_cnc3"
if ($fn.Substring($fn.Length - 5) -eq "_cnc3")
{
    Write-Host "Test"  #your code would all go inside this wrapper.
}

Open in new window

0
 

Author Closing Comment

by:bobgraf
ID: 41870282
Thanks. Did exactly what I wanted it to do.
0

Featured Post

IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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