4

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.

2
  • 2
    Batch scripting always seems simple...until it's not. PowerShell is the way to go here. Jun 26, 2017 at 15:04
  • 1
    The move command should be: FOR %%i in ("c:\test\source\*.csv") DO (MOVE %%i "c:\test\destination\%%~ni_%stamp%.csv"). Also, creation date may not be the same as modification date. If a file previously existed with the same name and had been deleted/moved, Windows may use the creation date of the previous file that had been deleted/moved. So you may want to use modification date.
    – Greg Askew
    Jun 26, 2017 at 15:43

1 Answer 1

4

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! :)

8
  • 1
    Ignore this, missed your bit about created/mod time, I'll ammend in just a sec... Jun 26, 2017 at 15:03
  • 1
    Updated to fix the above. Jun 26, 2017 at 15:07
  • 1
    Thank you, I think I'll take your advice and work on my powershell skills. As for your code, it mostly works great, there is some weird issue with the time stamp, it seems to get stuck and stamp the same time on a bunch of the files. If I manually create 10 files, waiting a few seconds between each, it time stamps the first few correctly then gives the rest the same time stamp. (I edited the timestamp to say yyMMdd and dropped the M from the hhmmMss) as it was showing the minutes instead of the month, I think I did it right, feel free to shout at me if not)
    – SysAdminUK
    Jun 27, 2017 at 7:36
  • I'll also need to figure out how to edit the code to replace the filename with a new name + timestamp, example instead of just adding the time stamp at the end I need to replace it with a new name + time stamp such as Finished_YYMMDD_hhmmss etc.
    – SysAdminUK
    Jun 27, 2017 at 7:39
  • 1
    Thank you, you've been incredibly helpful. I'll take it from here and learn through tweaking it.
    – SysAdminUK
    Jun 27, 2017 at 7:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .