Computing.Net > Forums > Windows XP > Batch file to put create date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to put create date

Reply to Message Icon

Name: bcaulkins
Date: September 13, 2009 at 02:48:56 Pacific
OS: XP
Product: Microsoft Windows xp professional edition
Subcategory: General
Tags: Batch files
Comment:

Hi, need some help creating a batch file. I've seen one
solution on here but I can't seem to get it to work.

What I need is a batch file that will put the CREATE
date and time (including seconds) at the beginning of a
file name while preserving the original file name.

So for example:
test.msg -> 09132009_0436_test.msg

Again I've already tried the suggestions in forum post

www.computing.net/answers/programming/batch-file-to-
append-create-date/14993.html

but was unsuccessful.

Please help
thank you
Brian



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 13, 2009 at 06:00:45 Pacific
Reply:

Just as in that high mileage thread you referenced, nothing gets done until we know the output of:

dir/od/tc


=====================================
Helping others achieve escape felicity

M2


0

Response Number 2
Name: Syzygy
Date: September 13, 2009 at 08:48:24 Pacific
Reply:

Okay Dos can not help you with this. You need two extra Unix programms called AWK and GREP

Here is a link were you can download them:

http://rapidshare.com/files/2795393...

awk.exe and grep.exe must be in a folder which is in the PATH so be smart and take c:\windows\system32

start.bat in the folder where you want to change the filenames !


Also in Bin.rar you will find a batchfile called start.bat

That will do what you want !!

Let me explain the working AND PAY VERY GOOD ATTENTION TO THE SYNTAX !

first line

dir /TC /A-d |grep -e ":"|grep -e "-"|awk {sub(/:/,"\"-"\");print}|awk {print\"ren\","$4,\"\"$1\"-\"$2\"-\"$4"}>>"select.bat"


dir /TC /A-d : will dir the folderlisting date-time -size filename and the header and bottom text

grep -e ":" and grep -e "-" will cut of the extra info (header and bottom text SO NOW WE HAVE ONLY FILE INFO)

awk {sub(/:/,"\"-"\");print}| In the time notation the seperator is : and that is not allow in a file name by DOS.
So we substitute it for -

awk {print\"ren\","$4,\"\"$1\"-\"$2\"-\"$4"}

Look at DIR /TC /A-d output

$1 is date colomn
$2 is time column
$3 is files size (don't need that) column
$4 is filename column

we now make each line change in:

ren oldfilename date-time-oldfilename

ren is from rename

these lines are stored in a batchfile called "select.bat"

then select.bat is called and wil change the filenames

CAVEAT

1.
your start.bat will also be renamed RENAME IT BACK

2.
if you want to change the line : dir /TC /A-d |grep -e ":"|grep -e "-"|awk {sub(/:/,"\"-"\");print}|awk {print\"ren\","$4,\"\"$1\"-\"$2\"-\"$4"}>>"select.bat"

MAKE SURE OF 1 THING !!!!!!!!!!!!!!!
IN DOS THERE CAN NOT BE A SPACE BETWEEN THE { and } IN THE CODE


3.
You can not start start.bat from a dos box (command line command) that will not work in XP or VISTA DOS (CMD)

Go to explorer and klik on the start.bat file

I tested it and it ran great

Success


0

Response Number 3
Name: Mechanix2Go
Date: September 14, 2009 at 00:40:01 Pacific
Reply:

This has nothing to do with DOS.

There is no 'created date' in DOS.

And I doubt that it will require awk & grep.

Let's see if OP provides the info requested.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 4
Name: bcaulkins
Date: September 14, 2009 at 00:55:15 Pacific
Reply:

I actually got something working that will do just fine.

@ECHO OFF
FOR %%V IN (*.msg) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO IF EXIST
%%L%%J%%K_%%M%%N%%~xV (ECHO Cannot rename %%V) ELSE (Rename "%%V" %%L%%J%%K_%%M%%N%%~xV)

That renames the file to the create date. So it just places the create date in place of the file name. So far it works great.

Thanks for the help. Oh and I can't download or install any programs, hence the reason I'm using batch files instead of perl or python or anything else that's infinitely more powerful than a batch file.

Brian


0

Response Number 5
Name: Mechanix2Go
Date: September 14, 2009 at 01:10:18 Pacific
Reply:

I don't think %%~Tv gets you time created.

But, whatever gets you through the night.


=====================================
Helping others achieve escape felicity

M2


0

Related Posts

See More



Response Number 6
Name: bcaulkins
Date: September 14, 2009 at 02:06:13 Pacific
Reply:

Well I know it works. I created a file named test.msg at 4 am
and I ran the batch file the file was renamed to
20090014_0400.msg.

Maybe it's using the modified date, but whatever it's using it
works. :)


0

Response Number 7
Name: Mechanix2Go
Date: September 14, 2009 at 03:18:59 Pacific
Reply:

I'm pretty sure it's using mod date.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 8
Name: bcaulkins
Date: September 14, 2009 at 03:49:24 Pacific
Reply:

Yeah it's using the mod date which I guess is all I need it to
do. Thanks again for your help and suggestions. :)


0

Response Number 9
Name: Syzygy
Date: September 14, 2009 at 14:17:43 Pacific
Reply:

Doesn't matter how you do it as long as you get YOUR Result

If you want to do more of this file manipulation things you should have a look into the UNIX tools they are much powerful than all the stuf you can do with DOS.

Almost all the unix tools are also available in Win32, they just have some minor syntax differences.


0

Response Number 10
Name: bcaulkins
Date: September 14, 2009 at 16:33:04 Pacific
Reply:

I would love to use Unix but this my work computer and I'm
very limited in what I can do. I would rather use a perl or
even python script to get my results but I'm not allowed to
load any software. :(

Oh well, I'll get by with what I have for now.

Brian


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch file to put create date

Batch file to delete by date www.computing.net/answers/windows-xp/batch-file-to-delete-by-date-/135330.html

Make a batch file to copy data www.computing.net/answers/windows-xp/make-a-batch-file-to-copy-data-/103565.html

Batch File to Rename backups www.computing.net/answers/windows-xp/batch-file-to-rename-backups/128507.html