Replacing multiple characters, words etc in a variable

I have a script where I am using the file name as the name of the output file. It has everything I need. Currently I am doing 3 -replace operations on the name to get it to where I need it.

I found a way I can do it with one -replace operation but I am not sure I understand how it’s setup. They recommended using a character group as shown in the example below. In this example they are replacing : and / with _

So if I read this correctly I need the . as separators between the characters being replaced.

[pre]

Get-Date -Format G | foreach {$_ -replace “[:./]”, “_”}
6_5_2013 3_50_44 PM

[/pre]

 

I think I can use that for what I am doing but not sure. Here is what I am doing:

The filenames look like this: 2019AustraliaHolidays.csv The only thing that will change in the filename from year to year is the actual year. For this year it’s 2019.

Here is the script snippet I want to do in one operation rather than 3.

[pre]

$name = $item.name
$year = $name.Substring(0,4)
$CalendarName = $Name -replace “.csv$”, “”
$CalendarName = $Name -replace “Holidays$”, “”
$CalendarName = $name -replace “$year”, “”

[/pre]

 

Any assistance would be greatly appreciated.

Not exactly sure if I understand what you mean but if all you want to do is updating the year you could do something like.

[pre]
$oldName = “2018AustraliaHolidays.csv”
$year = “2019”

$newName = $year + $oldName.Substring(4)
[/pre]

Or if you provide an example of what is in the variable $name, that would help.

It might help to see the entire piece involved. I have included it below. The script creates .ics calendar files from input csv files. This particular section of the script uses the file name of the csv file to create the .ics filename.

[pre]

Foreach ($item in $HolidayFiles)
{
$EmployeeType

$name = $item.name
$year = $name.Substring(0, 4)
$CalendarName = $name -replace “$year”, “”
$CalendarName = $Name -replace “.csv$”, “”
$CalendarName = $Name -replace “Holidays$”, “”

Switch ($EmployeeType)
{
employee { $FinalPath = “c:\scripts\Holidayfiles$Year” + “$Year” + " $CalendarName" + " Holiday Calendar.ics" }
manager { $FinalPath = “c:\scripts\Holidayfiles\Manager$Year” + “$Year” + " $CalendarName" + " Holiday Calendar.ics" }
}

[/pre]

 

Your example

Get-Date -Format G | foreach {$_ -replace "[:\./]", "_"}

uses a Regular Expression. Unless you have linux/regex experience, I would use something simple like:

(Get-Date -Format G).Replace(':','_')

which uses the .Replace method instead of the -replace operator

Better yet, you can format the date to start with using the -Format parameter as in:

Get-Date -Format 'MM/dd/yyyy hh_mm_ss tt'
$EmployeeType = 'employee' # $EmployeeType = 'manager'

$SampleFileNameList = @(
    '2019AustraliaHolidays.csv'
    '2019ChinaHolidays.csv'
    '2019USHolidays.csv'
) 

$SampleFileNameList | foreach {
    $EmployeeFolder = if ($EmployeeType -eq 'manager') { 'Manager\' } else { '' }
    $Year = $_.Substring(0, 4)
    $CalendarName = $_.Replace($Year,'').Replace('Holidays.csv','')
    "c:\scripts\Holidayfiles\$EmployeeFolder$Year $CalendarName  Holiday Calendar.ics"
}

Ok, I will try that.

It looks interesting.