1
我有一個腳本,將文件重命名爲用戶在鍵盤上輸入的日期 - 這很簡單,只是要求月,日和年。只要用戶輸入2位數值,它就可以很好地工作......問題在於有時用戶不會輸入前導0,然後整個事情就會崩潰。我如何確保包含領先的0? 到目前爲止,這裏是我有:從鍵盤的PowerShell日期
$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}
然後它加到數字建立文件名:
$Filename = "Customer--$BatchMonth-$BatchDay-$BatchYear($BatchNum).txt"
如何強制變量是兩位數,即使天月份只有一位數字?
謝謝!
我正在重溫這個問題 - 現在我需要的不僅僅是一個文件名變量,因爲日期在另一個函數中使用。會發生什麼情況是日期成爲查找替換的一部分,因爲用戶將日期輸入爲單位數字日,因此該月的前9天失敗...我感謝您的幫助! – user1467163
所以使用相同的格式化技巧。例如:$ dateEntered ='{0:00}'-f(Read-Host'輸入日期') – EBGreen