I have a script that renames files to dates entered by the user at the keyboard- It’s very simple, just asks for the month, day, and year. As long as the user enters 2-digit values, it works well… the problem is that sometimes the user does not enter the leading 0, and then the whole thing falls apart. How do I make sure the leading 0 is included?
So far, here is what I have:
$BatchDay = $(read-host "Enter Day of Month")
if ($BatchDay -eq ''){$BatchDay = Get-Date -format dd}
$BatchMonth = $(read-host "Enter Month of Year")
if ($BatchMonth -eq ''){$BatchMonth = Get-Date -format MM}
$BatchYear = $(read-host "Enter Year")
if ($BatchYear -eq ''){$BatchYear = Get-Date -format yy}
Then it concatenates the numbers to build the filename:
$Filename = "Customer--$BatchMonth-$BatchDay-$BatchYear($BatchNum).txt"
How do I force the variables to be two-digit, even when days and months are only one digit?
Thanks!
You can just create your filename formating your vars :
gives
in your case :