namerg
asked on
How to add two digits for file creation time after renaming file
I have the following code but not sure how to add always two digits for the month, day, hour, minutes and seconds when it gets renamed...or a better way to do it ?
I get CDR_YYYYMDHms.txt and I want CDR_YYYYMMDDHHmmss.txt
YYYY = Year
MM = Month
DD = Day
mm = Minutes
ss = Seconds
Thanks for your help,
$SourcePath1 = "\\SERVER\llccucm-sa\192.168.26.11\"
$SubFoldersPath1 = "\cm\cdr\"
$CDRFile = "_cdrActiveFile.act"
$ListFolders = Get-ChildItem -Path "\\SERVER\llccucm-sa\192.168.26.11"
If ($ListFolders -eq $null)
{
Write-Host "I do not have folders"
}
Else
{
Write-Host "I have folders"
$CMParentFolder = Get-ChildItem $SourcePath1 | Select Name
$CMParentFolderName = $CMParentFolder.Name
$CDRFileCreationTime = Get-ChildItem ($SourcePath1 + $CMParentFolderName + $SubFoldersPath1 + $CDRFile) | Select CreationTime
$CDRFileCreationTimeStamp = $CDRFileCreationTime.CreationTime
$CDRFileYear = $CDRFileCreationTimeStamp.Year
$CDRFileMonth = $CDRFileCreationTimeStamp.Month
$CDRFileDay = $CDRFileCreationTimeStamp.Day
$CDRFileHour = $CDRFileCreationTimeStamp.Hour
$CDRFileMinutes = $CDRFileCreationTimeStamp.Minute
$CDRFileSeconds = $CDRFileCreationTimeStamp.Second
$OldCDRFileName = "$SourcePath1" + "$CMParentFolderName" + "$SubFoldersPath1" + "$CDRFile"
$NewCDRFileName = "CDR_" + "$CDRFileYear" + "$CDRFileMonth" + "$CDRFileDay" + "$CDRFileHour" + "$CDRFileMinutes" + "$CDRFileSeconds" + ".txt"
Rename-Item -Path $OldCDRFileName -NewName $NewCDRFileName
}
I get CDR_YYYYMDHms.txt and I want CDR_YYYYMMDDHHmmss.txt
YYYY = Year
MM = Month
DD = Day
mm = Minutes
ss = Seconds
Thanks for your help,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks footech
you can use
Open in new window
PS C:\Users\Mike>
$file =Get-Item C:\Temp\1GB.txt
$file.CreationTime
$file.CreationTime.ToStrin
Wednesday, January 24, 2018 7:15:08 PM
20180124071508