Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding the current quarter year in Powershell to rename a file

I’m trying to get the current quarter placed in a file name.
I’ve assigned the year and current quarter number to two different variables so I can use them as I please anywhere as well (such as renaming a folder, to reference a file/folder, etc.)

Current File is:

FileName.csv

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code:

$QYear = $(Get-date -f yyyy)
$QNum = [int][math]::Ceiling($today.Month/3)
$file1 = “file1.csv”
$file2 = “$QYear-Q$QNum-FileName(distrubution).csv”
Import-CSV $lineCount | Select-Object 'LineCount', 'Name' | Export-Csv $file2 #I was reordering columns

Current Output:

2023-Q3-FileName.csv

Desired output:

2023Q3FileName.csv

>Solution :

Change this string expression:

$file2 = "$QYear-Q$QNum-FileName(distrubution).csv"

to:

$file2 = "${QYear}Q${QNum}FileName(distrubution).csv"

By using { } as qualifiers around the variable name, you prevent PowerShell from trying to resolve $QYearQ or $QNumFileName

1 comments

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading