tags:

views:

69

answers:

1

I am contemplating writing a program that will move some newly created dirs to another that puts them into date-stamped folders; either by week-month-year or month-year. I can probably write/debug/test this in java in 1.5 hours, BUT

I would like to know if anyone else has had to deal with this, and perhaps has done it with a windows batch script, or something else i can just use.

edit: it turns out i didnt state my requirements well enough. Better requirements:

  • i need newly created folders of current month to be copied to a current-month named folder elsewhere. This is important because new folders are being created every day, and if i always copy them all from source dir to month stamped dirs, i will eventually copy last month's file to this months folder.

Moreover, deleting files in source dir after copy is also a bad option, because the files are being streamed in, so there is a chance of some file in source dir being written to at the time of deletion.

+2  A: 

From the command prompt:

for /f "tokens=3 delims=/ " %f in ('date /t') do md %f & xcopy source\xyz.* %f

this parses the command date /t

C:\>Date /t
Mon 19/10/2009

using the delimters / and space

and which decomposes to

Mon, 19, 10, 2009

Then you ask for it to get the third token (10) which on my system is the month

After parsing the command md %f (which becomes make directory 10), and using the & we add another command to be executed (ie the xcopy to the new directory).

Another example to make it a bit simpler (here I'm breaking up the date /t into its parts and printing them on the screen)

C:\>for /f "tokens=2,3,4 delims=/ " %f in ('date /t') do @echo year = %h, month = %g, day = %f
year = 2009, month = 10, day = 19

Edit: To reflect comments. This is not the ideal solution as date formatting is very much based on user regional setting preferences. However it does illustrate the point and you should check before using this exact code.

Preet Sangha
welcome - in batch for is very very powerful!
Preet Sangha
actually. this is exactly what i asked for, which turned out to be not exactly what i needed =)
mkoryak
Remember, that date manipulation done this way is prone to breakage. Actually, not everyone uses `/` as the date format separator. The order of Y, M and D might vary, etc.
Joey
@johannes. yes you are right.
Preet Sangha