Link to home
Start Free TrialLog in
Avatar of namerg
namergFlag for United States of America

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 ?

$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

    }

Open in new window


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,
Avatar of yo_bee
yo_bee
Flag of United States of America image

Are you saying 1-9 you want 01 - 09?

you can use

$CDRFileCreationTimeStamp = $CDRFileCreationTime.CreationTime.tostring("yyyyMMddhhmmss")

Open in new window



PS C:\Users\Mike>
$file =Get-Item C:\Temp\1GB.txt
$file.CreationTime
$file.CreationTime.ToString("yyyyMMddhhmmss")

Wednesday, January 24, 2018 7:15:08 PM
20180124071508
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

Thanks footech