Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

19 Experts available now in Live!

Get 1:1 Help Now