Solved

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

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete email from user mailbox 23 76
Microsoft Azure Powershell Book Recommendations 5 24
Filter IP range with PowerShell 1 27
Add ESXi to LDAP/AD = Scripted? 2 4
Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
A brief introduction to what I consider to be the best editor for PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

25 Experts available now in Live!

Get 1:1 Help Now