Solved

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

Posted on 2016-11-01
3
34 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

751 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