Script for renaming files and adding a timestamp (server 2012 r2)

batchbatch-file

I'm struggling to create a (fairly simple) batch file script that meets these requirements:

  • Looks in the folder called Source for a file (with any name.csv)
  • Renames the file to File_YYYYMMDD_HHMinMinSS.csv (timestamp should be modified/creation date if possible (modified/creation should be the same thing so either works)
  • Moves it to a folder called Destination where further work can be done on it.

1 file is created per day, so should only be 1 in there, but if there is more than 1 (due to the script not running for whatever reason) it should be able to do multiple files or just pick up the first and handle it correctly. (I can schedule it to run multiple times a day to pick up any that weren't processed before.

So here's my code so far. I've pieced it together from google and it works in a fashion but doesn't do quite what I want.

echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%%MM%%DD%_%HH%%Min%%Sec%

move "c:\test\source\*.csv" "c:\test\destination\File_%stamp%.csv"

It finds the file ok. It renames it in the format I want (but uses current date and time, not sure how to use modified or created date instead) and moves it correctly. But it only works for a single file. If there are multiple files in the directory it errors out that it can't merge multiple files into a single file (which makes sense, it can't rename all files to 1 file.

But I'm stuck. I don't know how to take it further. Can someone help me out? I vaguely understand what the code is doing but not enough to manipulate it into doing what I want.

Powershell would also be an option but I know even less about how to do it in there. If there's another option that can be easily scheduled through task scheduler that would work too.

Best Answer

This bit of powershell does what you want. Just put the full path to the source and destination directories at the top between the quotation marks.

Edit: added the bit about file creation time. Edit: added naming examples, the # is a comment, only leave one $name = uncommented at a time, if you uncomment multiple, it will be the last uncommented one that takes effect.

$source = "c:\te\s"
$destination = "c:\te\d"

Get-ChildItem $source -Recurse -Include *.csv | % {
    $name = $_.Name.Split(".")[0] + "_" + ($_.CreationTime | Get-Date -Format yyyymmdd) + "_" + ($_.CreationTime | Get-Date  -Format hhmmss) + ".csv"
    #$name = "Finished_" + ($_.CreationTime | Get-Date -Format yyyymmdd) + "_" + ($_.CreationTime | Get-Date  -Format hhmmss) + ".csv"
    #$name = "Finished_" + $_.Name.Split(".")[0] +  "_" + ($_.CreationTime | Get-Date -Format yyyymmdd) + "_" + ($_.CreationTime | Get-Date  -Format hhmmss) + ".csv"
    Rename-Item $_ -NewName $name
    Move-Item "$($_.Directory)\$name" -Destination $destination
}

As an aside, I highly suggest setting a bit of time each day aside to learning PowerShell, it's really a great tool, and much more user friendly than batch scripts, as scripting languages go it's easy to read, mostly because it's written in almost plain english. This is a good resource for resources! :)