Add date to filename after specific underscore/position

Moonlit3 21 Reputation points
2022-04-13T13:09:31.117+00:00

Hello

Im trying to append a timestamp to filenames.
The files have specific pattern with underscores between characters.
This way : XXXXX_XXXXXX_XXXX_XXX.txt

I want the files to be renamed this way:
Adding a timestamp after the first underscore.
XXXXX_TIMESTAMP_XXXXXX_XXXX_XXX.txt

I can scope files with the same pattern and exclude the others.
However I dont get to append the timestamp variable in the right place.
Im lost with the regex I should use in the -replace parameters

Here is my script:

>     $date = Get-Date -Format yyyyMMddHHmmssffffff

>     

>     $Path = "C:\TEST"

>     Get-ChildItem -Filter *.txt $Path | Foreach-Object{   

>        $NewName = $_.Name -replace "(.*?)_(.*?)_(.*?)_(.*?).txt", "\(_*\)+$date.txt+\(.*\).*\(.*\)"

>        Write-Host $NewName

>        Rename-Item -Path $_.FullName -NewName $NewName

>     }

Im able to rename files with $date only. but cant get it working with putting $date after the first underscore and keeping the other characters before and after.
I guess the issue is in the regex format:
-replace "(.?)_(.?)(.*?)(.?).txt", "(_)+$date.txt+(.).(.*)"

Can someone help me out ?
Thank you for your time !

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,434 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,416 Reputation points
    2022-04-14T14:54:17.693+00:00

    Whatever you're doing, you're doing it wrong. Your script, with my suggestion and a modification to the Get-ChildItem cmdlet, works.

    $date = Get-Date -Format yyyyMMddHHmmssffffff
    
    $Path = "C:\TEST"
    Get-ChildItem -Path "$Path\*" -Filter *.txt -File | 
        ForEach-Object {  
            $parts = $_.Name -split "_" 
            if ($parts.count -eq 4){
                $newname = "{0}_{1}_{2}_{3}_{4}" -f $parts[0], $date, $parts[1], $parts[2], $parts[3]
                Write-Host $NewName
                Rename-Item -Path $_.FullName -NewName $NewName
            }
            else{
                Write-Host "Skipping file " $_.Name -ForegroundColor Yellow
            }
    }
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2022-04-13T15:11:04.597+00:00

    Sometimes a regex just isn't the best (or most understandable) way to get things done.

    $x = "XXXXX_XXXXXX_XXXX_XXX.txt"
    $date = Get-Date -Format yyyyMMddHHmmssffffff
    
    $parts = $x -split "_"
    
    if ($parts.count -eq 4){
        $newname = "{0}_{1}_{2}_{3}_{4}" -f $parts[0], $date, $parts[1], $parts[2], $parts[3]
    }
    
    0 comments No comments

  2. Moonlit3 21 Reputation points
    2022-04-14T08:53:47.73+00:00

    Thank you for your reply Matheisen but it doesnt do anything.
    I've added the Get-ChildItem to catch my txt files but it didnt work either.


  3. Moonlit3 21 Reputation points
    2022-04-19T11:50:13.177+00:00

    Hello Mat,

    I was off to answer you earlier...
    Yes, indeed, I didnt explain the output and the script I used.
    Removing the variable " $x = "XXXXX_XXXXXX_XXXX_XXX.txt" " and using Get-ChildItem was the way to go.
    Now, with your recommandations, it works like a charm.
    I wanted to thank you for your help !
    I really appreciate
    Have a nice day :)

    0 comments No comments