BAT file or script to add date of creation (YYYY MM DD) prefix to file name?

Martin Farez 21 Reputation points
2023-01-05T14:56:44.187+00:00

Hello,

I'm looking for a bat file (if possible) or script (or whatever is the fastest solution) that will rename every file in a specific folder by adding a date of creation prefix in YYYY MM DD format.

I need the date of creation (not last modified) and this specific date format because it's in accordance with how my company does things. I don't need the time and the rest of the name needs to remain the same.

Example:

File created on March 8 2022 named "IMG--ARGUMENT--TEST"
will become "2022 03 08 IMG--ARGUMENT--TEST"

(If possible only the last 2 digits of the year "22 03 08 IMG--ARGUMENT--TEST" but that is not a priority because I have other tools to do that)

A bat file would be preferable because I'm gonna have to give this tool to people who are not particularly tech-savvy or curious enough to go through the effort of going through various coding steps.

PowerRename was a good temporary solution but I think it's too many steps for the people I'm gonna give this too.

I hope this is clear enough

Many thanks for considering my request.

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,602 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 56,271 Reputation points
    2023-01-05T16:06:21.523+00:00

    In PS something simple like this works.

       param(  
           [string] $targetPath  
       )  
         
       # Assuming the script is run in the current directory  
       if (-Not $targetPath) {  
           $targetPath = Get-Location  
       }  
         
       # Get the files  
       Get-ChildItem $targetPath -File | Rename-Item -NewName { "$($_.CreationTime.ToString('yy MM dd')) $($_.Name)" }  
    

    If you put the script in the same directory as the files to be renamed then you can just run the script directly. Otherwise specify -targetPath ... to specify the target directory to rename. If you want to recursively rename subfolder files then add -recurse to the Get-ChildItem call.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,501 Reputation points
    2023-01-05T15:51:52.79+00:00

    Something like this? Remove the "-whatif" from the Rename-Item when you're satisfied it works the way you want it to:

    $d = Read-Host -Prompt "Enter the directory name. E.g., C:\MyDirectoryName"  
    $Proceed = ""  
    While ("Y","N" -notcontains $Proceed){  
        $Proceed = Read-Host "Is this the correct directory? $d`n Enter 'Y' if it is, or 'N' if it isn't"  
    }  
    If ($Proceed -eq "N"){  
        Write-Host "Exiting. Rerun this script using the correct directory name."  
        Return  
    }  
    Get-ChildItem $d |  
        ForEach-Object{  
            $n = "{0} {1}" -f (get-date $_.CreationTime -Format "yy MM dd"), $_.Name  
            Rename-Item -Path $_.FullName -NewName $n -whatif  
        }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.