Rename files - # of week

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Rename files - # of week

#1 Post by Zick » 05 Jul 2013 08:03

I need to rename files like this "date ABC #ofWeek.week".

One of the dozens answers I got (none of them working) is here:

Code: Select all

@echo off
for /F "tokens=1-5 delims=/" %%d in ("%date%") do (
set ddmmyy=%%e.%%f.%%g
set /A dd=1%%e-100, mm=1%%f-100, yy=%%g, yyM1=yy-1
)
:: Get Julian Day Number of today's date
if %mm% lss 3 set /A mm+=12, yy-=1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10, jdn=c+dd+e+f-1524
:: Subtract Julian Day Number of January/1st (get number of days in year)
set /A a=yyM1/100, b=a/4, c=2-a+b, e=36525*(yyM1+4716)/100, f=306*14/10, days=jdn-(c+1+e+f-1524)+1
:: Get number of week
set /A week=(days+3)/7+1
rename "H:\BatchStuff\1.txt" "%ddmmyy% - %week%.week !random!!random!.txt"

and another roughly same here:

Code: Select all

@echo off
for /F "tokens=1-3 delims=/" %%d in ("%date%") do (
set ddmmyy=%%d.%%e.%%f
set /A dd=1%%d-100, mm=1%%e-100, yy=%%f, yyM1=yy-1
)
if %mm% lss 3 set /A mm+=12, yy-=1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10,
jdn=c+dd+e+f-1524
set /A a=yyM1/100, b=a/4, c=2-a+b, e=36525*(yyM1+4716)/100, f=306*14/10,
days=jdn-(c+1+e+f-1524)+1
set /A week=(days+3)/7+1
rename "H:\BatchStuff\1.txt" "%ddmmyy% - %week%.week %random%%random%.txt"
pause


I really don't know how to fix all of that, could you please re-write / correct it for me?

Thank you.

penpen
Expert
Posts: 1872
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename files - # of week

#2 Post by penpen » 05 Jul 2013 14:22

I assume that the problem is the date format of your computer, although i haven't checked the below computation.

The upper code works on a date format like this: Monday/31/12/2013
and the lower code works on such a date format: 31/12/2013

To make the code work on your computer you have to modify the upper for loop:
the char after "delim=" part is the character between the numbers (or words), you may use two chars if needed.
And the variables in the upper block are (%%d(Monday), %%e(31), %%f(12), %%g(2013)), lower block is analog.

To specify your date format, just type at the console window:

Code: Select all

echo %date%


penpen

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Rename files - # of week

#3 Post by Endoro » 05 Jul 2013 14:37

Why does Aacini's answer not work?
Did you get error messages or unexpected results?


This is not helpful, for any reason:
sadly it seems it's not working, for some reason

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#4 Post by Zick » 05 Jul 2013 15:18

Endoro wrote:Why does Aacini's answer not work?
Did you get error messages or unexpected results?


This is not helpful, for any reason:
sadly it seems it's not working, for some reason

Oh, sorry about that. I thought it'd do the same as it does to me... I didn't know about the PC differences, either way it renames the file to ".. - -502.week !random!!random!"

I'll try to change the line as suggested above.

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#5 Post by Zick » 05 Jul 2013 15:21

penpen wrote:To make the code work on your computer you have to modify the upper for loop:
the char after "delim=" part is the character between the numbers (or words), you may use two chars if needed.
And the variables in the upper block are (%%d(Monday), %%e(31), %%f(12), %%g(2013)), lower block is analog.

To specify your date format, just type at the console window:

Code: Select all

echo %date%


penpen


Code: Select all

for /F "tokens=1-5 delims=." %%d in ("%date%") do (
   set ddmmyy=%%d.%%e.%%f.%%g
how I changed it

and the outcome is "fri 05.07.2013. - 52022.week"

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Rename files - # of week

#6 Post by Endoro » 05 Jul 2013 15:35

But you have only 3 token: "05", "07", and "2013".

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#7 Post by Zick » 05 Jul 2013 15:39

Endoro wrote:But you have only 3 token: "05", "07", and "2013".

I've tried 1-3, but there was no change, so I tried random stuff, no change either. Is it my PC settings then?

Anything from 1-3 to 1-31 and the outcome is still the same

%%d - for me is "Mon 31", not only "Monday", so %%e (31) and %%f (12), %%g does nothing and comes out as "%g" in the file name.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Rename files - # of week

#8 Post by Endoro » 05 Jul 2013 15:45

What ist the output of

Code: Select all

echo %date%

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#9 Post by Zick » 05 Jul 2013 15:46

fri 05.07.2013

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Rename files - # of week

#10 Post by Endoro » 05 Jul 2013 16:01

OK, try this (you can replace "fri 05.07.2013" with "%date%"):

Code: Select all

@echo off &SETLOCAL
for /F "tokens=1-4 delims=. " %%d in ("fri 05.07.2013") do (
set ddmmyy=%%d %%e.%%f.%%g
set /A dd=1%%e-100, mm=1%%f-100, yy=%%g, yyM1=yy-1
)
if %mm% lss 3 set /A mm+=12, yy-=1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10, jdn=c+dd+e+f-1524
set /A a=yyM1/100, b=a/4, c=2-a+b, e=36525*(yyM1+4716)/100, f=306*14/10, days=jdn-(c+1+e+f-1524)+1
set /A week=(days+3)/7+1
ECHO rename "H:\BatchStuff\1.txt" "%ddmmyy% - %week%.week %random%%random%.txt"
pause

Output (may vary):

Code: Select all

rename "H:\BatchStuff\1.txt" "fri 05.07.2013 - 28.week 11194570.txt"

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#11 Post by Zick » 05 Jul 2013 16:03

28. week, as you said, but it's 27th. I think - not sure whether to trust that website, lol.
http://www.epochconverter.com/epoch/weeknumbers.php
Last edited by Zick on 05 Jul 2013 16:17, edited 1 time in total.

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#12 Post by Zick » 05 Jul 2013 16:16

So yea, 28th week, it goes to 29th on 12-07 and 28th on 05-07, as if start of the week was Friday.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Rename files - # of week

#13 Post by Endoro » 05 Jul 2013 16:20

Code: Select all

@echo off &SETLOCAL
for /F "tokens=1-4 delims=. " %%d in ("fri 05.07.2013") do (
set ddmmyy=%%d %%e.%%f.%%g
set /A dd=1%%e-100, mm=1%%f-100, yy=%%g, yyM1=yy-1
)
if %mm% lss 3 set /A mm+=12, yy-=1
set /A a=yy/100, b=a/4, c=2-a+b, e=36525*(yy+4716)/100, f=306*(mm+1)/10, jdn=c+dd+e+f-1524
set /A a=yyM1/100, b=a/4, c=2-a+b, e=36525*(yyM1+4716)/100, f=306*14/10, days=jdn-(c+1+e+f-1524)+1
set /A week=(days+1)/7+1
ECHO rename "H:\BatchStuff\1.txt" "%ddmmyy% - %week%.week %random%%random%.txt"
pause

Code: Select all

rename "H:\BatchStuff\1.txt" "fri 05.07.2013 - 27.week 1378232273.txt"


Here you can count, if you want:

Code: Select all

                               2013

       January               February                 March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2                   1  2
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    3  4  5  6  7  8  9
13 14 15 16 17 18 19   10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 18 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31         24 25 26 27 28         24 25 26 27 28 29 30
                                              31
        April                   May                   June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6             1  2  3  4                      1
 7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
        July                  August                September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31            25 26 27 28 29 30 31   29 30

       October               November               December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
27 28 29 30 31         24 25 26 27 28 29 30   29 30 31
:)

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#14 Post by Zick » 05 Jul 2013 16:21

Code: Select all

set /A week=(days+1)/7+1
Would this fix it? :D
----------
Oh, thx a lot!!!
Next time I'll just move to another country! Less work than with this.

Now how do I thumbs you up or reputation you or something.
Thanks so much again, I'll go to bed after like 4 days of googling and randoming with a code... Hopefully first and last batch I'll ever need.

Zick
Posts: 9
Joined: 05 Jul 2013 07:57

Re: Rename files - # of week

#15 Post by Zick » 05 Jul 2013 16:39

BTW does something in that script change my PC settings somehow? :D Cause I had a simple batch

Code: Select all

@echo off
start firefox https://docs.google.com/spreadsheet/ccc?key=0ArP_code

and it worked well, now when I start it, it keeps opening the cmd till PC freezes...

And my iexplorer is odd as well, it has some certificate problems lol...

Post Reply