Format result in Script

Advanced Renamer forum
#1 : 06/08-24 10:25
GSComputer
Posts: 36
The filename Is taz20240806
I would like to rename it to TAZ 06.08.2024 Di
wherby Di =dayoFWeek

DF friendly showed and explained me in another thread how to script, This script I changed for my new purpose. Here it is

var fname, index, nameHead, day, month, year, D, M, Y, dayOfWeek;
fname = item.newBasename ;

index = fname.search(/(TAZ|late ed|saturday|samstag)/i ) ;

if ( index > -1 ) {
match = fname.match(/^(.*)(\d{4})(\d{2})(\d{2})$/) ;
if (match) {
nameHead = match[1];
D = (1*match[4]) ;
M = 1*match[3] ;
Y = 1*match[2] ;

const dayOfWeek = new Date(M, D, Y).getDay();

year = Y.toString() ;
month = ("0" + M.toString()).slice( -2 ) ;
day = ("0" + D.toString()).slice( -2 ) ;
fname = nameHead + " "+ day + "." + month + "." + year + " " + dayOfWeek ;
}
}
return fname ;

This gives me the result taz 06.08.2024 3
I need a hint to capialize taz in TAZ and dayOfWeek 3 in Di (short form of german Dienstag)


edited: 06/08-24 10:30
#2 : 06/08-24 11:48
Delta Foxtrot
Posts: 339
Reply to #1:

Yo G,

Easy. After the line

const dayOfWeek = new Date(M, D, Y).getDay();

add this:

switch ( dayOfWeek ) {
case 1:
dow = "So" ;
break;
case 2:
dow = "Mo" ;
break;
case 3:
dow = "Di" ;
break;
case 4:
dow = "Mi" ;
break;
case 5:
dow = "Do" ;
break;
case 6:
dow = "Fr" ;
break;
case 7:
dow = "Sa" ;
break;
}

and change this line

fname = nameHead + " "+ day + "." + month + "." + year + " " + dayOfWeek ;

to:

fname = ( nameHead = "taz" ? "TAZ" : nameHead ) + " "+ day + "." + month + "." + year + " " + dow ;

Boom!

That last bit won't change anything except "taz" to upper case, so if you want to adjust other keywords we'll have to play around some more. And you may have to adjust some of the "switch/case" statements if that's not the way you want your day abbreviations.

Bis später, mein Freund,
DF
#3 : 06/08-24 11:55
Styb
Posts: 122
Reply to #1: for an alternative way

After the script, you can add 3 methods to achieve the desired outcome.

1) New Case:
-Set upper case

2) Remove:
-Remove count: 1
-Starting at: 1
-Backwards: mark

3)Add:
-Add: <Date:ddd>
-At index: 0
-Backwards: mark




edited: 06/08-24 11:56
#4 : 06/08-24 12:08
GSComputer
Posts: 36
Reply to #3:
Thanks Styb,

This methods I know, but I use this Script/Batch in a bunch of other files, Therefor I want to do these "Formats" in the Script, as a "All In One Solution"


#5 : 06/08-24 12:17
Delta Foxtrot
Posts: 339
Reply to #4:

So G, did that work for you?

DF
#6 : 06/08-24 12:39
GSComputer
Posts: 36
Reply to #5:
Hi DF,

naturally, runs perfect.
I hope you noticed that I try to understand scripts. I read a lot about (w3school)
Now you gave me new examples I will research. (switch and formatting)

I have to learn a lot of "VOCABULARY"

Vielen Dank
Gerhard

edited: 06/08-24 12:42
#7 : 06/08-24 14:56
Delta Foxtrot
Posts: 339
Reply to #6:

G,

Glad it worked for you my friend. I DID notice that you are progressing nicely in the javascript department, in fact I'm kind of surprised that you are grasping the basics this quickly. When I started using ARen I knew very little about js (although I'd tried to learn it a long time ago and sort of hated it. even though I had a LOT of programming background). But ARen gave me an easy on-ramp, and David Lee's and Kim Jensen's help got me convinced that I could do it. So keep up the good work, OG, you're doing great.

"switch" and "if/else/else if" are kind of my go-to, even if they are fairly rudimentary in the jscript data structure department. And that "( nameHead = "taz" ? "TAZ" : nameHead )" is just an inline if/else, but it's fun to use. Go with what you know, I say. :)

Best,
DF

edited: 06/08-24 14:59
#8 : 06/08-24 20:05
GSComputer
Posts: 36
Reply to #7:
deleted by GSComputer

edited: 06/08-24 20:55
#9 : 06/08-24 20:05
GSComputer
Posts: 36
Reply to #7:
sorry DF

still with failures The first time it runs. Next time wrong number
filname taz20240806
yyyymmdd

I only change the last to digits (day) and get wrong value


getday()
0 = "So"
1 = "Mo"
2 = "Di"
3 = "Mi"
4 = "Do"
5 = "Fr"
6 = "Sa"

There must be something wrong with getDay()
I give up for today. Maybe sleep helps

edited: 07/08-24 08:16
#10 : 07/08-24 05:02
Delta Foxtrot
Posts: 339
Reply to #9:

Aw, G,

:) Oh well, fail 7 times get up 8...

Here's a slightly different way to do it. I added some spaces in the return just for me.

(no pre-batch)

const days = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"] ;
x = item.name
n = x.match( /^([^\d]+)(\d{4})(\d{2})(\d{2})(.*)$/ );
txt = n[1];
if ( txt != "taz" ) {
ntxt = txt.substring(0,1).toUpperCase() + txt.substring(1,txt.length) ;
} else {
ntxt = txt.toUpperCase() ;
}
Y = 1*n[2];
Ys = n[2];
M = 1*n[3];
Ms = ("0"+n[3].slice(-1)) ;
D = 1*n[4];
Ds = ("0"+n[4].slice(-1)) ;
xtr = n[5];
d = new Date(M,D,Y)
dow = days[d.getDay()] ;
return ntxt + " " + Ds + "." + Ms + "." + Ys + " " + dow + xtr ;
//--------------------------------------------------------------
https://drive.google.com/file/d/1xXHyK_RlWurPCqB 1lVkfrWVcYUHAvYGC/view?usp=sharing
(As a thought experiment I am converting a ~30-method batch to all javascript. You think I haven't hit a few head-scratchers? My head is bloody! :)

Toodles,
DF
#11 : 07/08-24 08:54
GSComputer
Posts: 36
Reply to #10:
(As a thought experiment I am converting a ~30-method batch to all javascript. You think I haven't hit a few head-scratchers? My head is bloody! :)

Hey Sorry my friend, keep cool, all is more important than my little AR/Java problem. Don'offer too much time for that

Your proposal lookes inthe way I wanted to have it schould be renamed. The only thing the last to digits, The dayOfWeek :
The picture you send me shows a file with TAZ 06.08.2024 Mi but it should be Di(Tu)
Mi (We) should have been made 3
The last ed is also wrong It should have Sa ore without your interesting "if else Switch Funktion"

I tried the getDay() in several other Internet JS windows (consols) for test. with the right resunlts.
Is it possible, that is an internal AR $.02 problem?
Also I can image the Time zone can cuse trouble.

The next 10 Hours I keep my hands off this stuff, and try it starting at Zero again.

I must say scripting can make addicting. "But it is a long way to Tipperary"

DF Thanks for your patience.
#12 : 07/08-24 09:06
Delta Foxtrot
Posts: 339
Reply to #11:

(scratching my bloody head)

I didn't even think to check the day...

:)
#13 : 07/08-24 10:15
GSComputer
Posts: 36
Reply to #12:
Sorry my friend. I hope I did nothing to make you angry-
If you want, really forget this thing.

Best Regards
#14 : 07/08-24 12:15
Delta Foxtrot
Posts: 339
Reply to #13:

Are you kidding? This is my idea of fun! :)
#15 : 08/08-24 07:08
Delta Foxtrot
Posts: 339
Reply to #14:

Not fun anymore... Nothing I know how to do has worked to convert a DATE to a DAY of the week. Spent several hours playing with different ways of converting, but I just can't make it work. Totally bombed out with getDay() and also toLocaleDateString(). Either I just don't get it or there's an internal js engine problem; but I tried it on both 3.95 and 4.0b and according to Kim it's two different js engines, so now I'm flamboozled. What really gets me is that in earlier posts by David Lee and others they seemed to be able to get the scripted day no problem.

Oh well, I can live with my failure. :) Don't worry G, despite what I said above I'm still having fun!

Let's see, if I use Unix timestamp, there are 36,527 days in a century... Or is it 36,525... Divide by seven... <head explodes>...

I may go try my efforts out on an online javascript interpreter to see what happens there.

Still best regards,
DF
#16 : 08/08-24 08:09
GSComputer
Posts: 36
Reply to #15:
Good morning DF,

I'm happy you're are the old one again. :)
I do this for years in a workaround, never using a script. So no big deal.
Seeing what can be done with scripting with your excamples for me and all over the forum, I thought this task is a good one to do my first script. All was clear how to and getDay should work. And it does in several internet descriptions like
https://www.w3schools.com/js/js_date_methods.asp
and it works.
I spent a minimum of 12 to 15 hours to that task. Which forced me to read and read, try to understand and therafore the time was not wasted but useful for me in contrast to you.
Your descripions to it gave me two new "versions/various" as else and goto, I haven' seen before somewhwere. I keep them in my mind as "DF-functions" .:)

My oppinion to the problem is, it it the js engine in AR.

By the way, I go to my already made methods to optimize them. The understanding what regex really is able to and the nose in scripting methods, I have a lot of fun working with a for me new AR.
Keep On Rockin' ( To come down I just listened Roy Buchanan's "When A Guitar plays the Blues")
Best Regards
GS

edited: 09/08-24 21:12