Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-01
3
Medium Priority
?
48 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 1000 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 14

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 1000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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…
Loops Section Overview

971 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