I think I've bitten off a lot more than I can chew.
I'm a court reporter, working in the Minnesota court system. We've developed a new policy for filing our electronic notes, as so many reporters are no longer printing to paper notes. Different brands of reporting machines create files with different naming conventions, and we want to standardize the names so someone in district administration can find the right notes easily if we are not around.
So we want to rename FILE.001, FILE.002, and 20151018-082815.sgstn, for example, to the date the file was created. The sgstn file, of course, has a unique name, but the ubiquitous writers that create FILE.* files really need renaming, and standardization will help all. I'm creating a CMD file that will also poll for the judge's name and the writer brand, so anyone pulling the notes out of storage will find exactly what they're looking for: "2015-10-18 Judge Jones, Stentura writer.001"
I've found plenty of solutions for renaming files with TODAY's date and time, but we want to rename them with THEIR OWN date and time.
I'm working as a standard user in Windows 7 Pro and do not have the option of installing third-party software. Windows allows individual users to set their preferred date style, and dbenham and others here have shown me how to get that style from the registry:
:: For REG.EXE 3.0 (Windows XP) and later versions
FOR /F "tokens=3" %%A IN (
'REG QUERY "HKCU\Control Panel\International" /v sShortDate'
) DO (SET sShortDate=%%A)
Echo %sShortDate%
That will return yyyy-MM-dd or d-M-yy or whatever the user has chosen.
And then I can test to see what the contents are of the different pieces of the string:
for /f "tokens=1,2,3 delims=/-" %%A in ("%sShortDate%") do (
for /f "tokens=1" %%D in ("%%A") do (
set Part1=%%D
)
for /f "tokens=1" %%D in ("%%B") do (
set Part2=%%D
)
for /f "tokens=1" %%D in ("%%C") do (
set Part3=%%D
)
)
echo Part1 = %Part1%
echo Part2 = %Part2%
echo Part3 = %Part3%
But this is all getting really unwieldy, and I'm only halfway there after a solid 24 hours of reading here and elsewhere. Is there really no way in Windows, at the CMD prompt or in any command-line-available tool, to get the date of a file in a predictable format and to use that date?
Can anyone take pity on me and give us a less cumbersome means of finding out what the user's particular system thinks the date should be formatted as and thus consistently rename all files that get dropped on the CMD shortcut?
Finally, this is my first post here. All constructive criticism gladly accepted. Seriously.
-- Timothy J. McGowan
Edited to add: Maybe I've been overthinking this. It's the user's hive in the registry I'm polling for the date format. We ought to be able to write to our own hive from the command line; right?
So I could read the user's preferred date format, save that to a variable, force the date format I want to use, process the files, and then reset the user's date format. Right? Worth a shot.
I'll report back if it works, but any more comments are quite welcome!
A first idea :
Use the delimiters to get the file date or name or extension
@echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "$FileName=%%~na"
set "$FileExtension=%%~xa"
set "$FileDate=%%~ta"
set "$FileDate=!$FileDate:/=-!"
echo !$FileDate!-!$FileName!!$FileExtension!)
EDIT I
To get the date in YYYY-MM-DD :
@echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "$FileName=%%~na"
set "$FileExtension=%%~xa"
set "$FileDate=%%~ta"
set "$FileDate=!$FileDate:/=-!"
set "$FileDate=!$FileDate:~6,4!-!$FileDate:~3,2!-!$FileDate:~0,2!"
echo !$FileDate!-!$FileName!!$FileExtension!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With