File Match Date and Rename

It takes me a bit to get used to a new language, but I can use some help.

I have some files in a directory.

I am looking to do 2 or 3 things.

  1. Find files with a .TXT extension.
  2. Get those .TXT file that match today’s date without the time.
  3. Rename that same file with a date extension and the word LINE(ie. Claim_20230210_LINE.TXT)

Yes, the below is not complete or working.

Thanks,
Vince

Find the current datetime and convert to a string that

$strtoday = (Get-Date).ToString(“yyyyMMdd”) # I presume this 20230210

Find all files with a .TXT extension in the directory below.

iterate over each one to only return the name. Store each file name in the $fileList variable

$filelist = Get-ChildItem "R:\SDM-IT Reports\RSDM_MDAudit_Files\Claim" -Filter *.TXT

Get the current date

$today = Get-Date -Format “MM/dd/yyyy” # I presume this 20230210

Process each file in a foreach loop

foreach ($file in $filelist) {
## Prepend the datetime string to the file name

    if ($file.LastWriteTime.ToString('MM/dd/yyyy') -eq $today) {

    $file = $file + $strtoday

    }
## Rename the old file to the new name with the datetime string
##Rename-Item "C:\Folder\$file" $newname -Verbose

}

Vince,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You should NOT convert dates to strings. PowerShell is able to compare or calculate with dates and times. :wink:

This should work …

$today = (Get-Date).Date
Get-ChildItem -Path 'R:\SDM-IT Reports\RSDM_MDAudit_Files\Claim' -Filter *.txt |
    Where-Object {
        ($_.LastWriteTime).Date -eq $today
    } |
        ForEach-Object {
            $NewName = $_.BaseName + $today.ToString('_yyyyMMdd_Line') + $_.Extension
            Rename-Item -Path $_.FullName -NewName $NewName
        }

Thanks Olaf ( I like that… that is cool).

Just a quick question. Would the script catch or grab all files on that day regardless of the time stamp? I am not sure if I worded that correctly, but let me know.

Thanks.

Why don’t you run the code just without the Foreach-Object part?

$today = (Get-Date).Date
Get-ChildItem -Path 'R:\SDM-IT Reports\RSDM_MDAudit_Files\Claim' -Filter *.txt |
    Where-Object {
        ($_.LastWriteTime).Date -eq $today
    }