Rename script, a little tricky, giving me trouble adjusting

Option Explicit

Dim fileNum
fileNum = 0

Dim numPrefix
numPrefix = Array( 111, 190, 251, 341, 442, 405, 512,590, 644, 715, 797, 810, 803, 910, 985 )

Dim newDate
Set newDate = DOpus.Create.Date()
newDate.Add 7, "d"

Function OnGetNewName(ByRef getNewNameData)
	If fileNum <= UBound(numPrefix) Then
		OnGetNewName = numPrefix(fileNum) & " " & getNewNameData.newname_stem & " " & newDate.Format("D#yyyy-MM-dd") & getNewNameData.newname_ext
		fileNum = fileNum + 1
	Else
		OnGetNewName = True
	End If
End Function

It takes a file(s) and prefixes it with a given set of numbers and the adds todays date plus 6 days in the YYYY-MM-DD format.

I'm TRYING to change the date insertion a bit by having TODAYS date prefix the date+6 but doesn't use "-" to separate numbers. This is for an existing filter system we use.

so the file chosen: FILENAME ONE would become 111 FILENAME ONE 20240719 2024-07-25 - using today's date July 19 2024. I originally wanted to have the date always be the upcoming Sunday, but gave up even though it seemed should be easy. Anyway, thanks if you can help out.

Based on the code you have, you already know how to get the current date:
Set newDate = DOpus.Create.Date()

And how to format the date into a string:
newDate.Format("D#yyyy-MM-dd")

All you need to do is store the current date separately (since you're adding 7 days to newDate) and change the format string to remove the unwanted - characters.

You can use the date's wday property to find out which day of the week it is, and from that know how many days to add.

Dim todayDate
Set todayDate = DOpus.Create.Date()

Dim newDate, wday
Set newDate = DOpus.Create.Date()
newDate.Add 7 - newDate.wday, "d" ' Bump to coming Sunday

DOpus.Output todayDate.Format("D#yyyyMMdd")
DOpus.Output newDate.Format("D#yyyy-MM-dd")

Thank you, that Sunday thing was bothering me for a long time, should have know Opus would have anticipated a solution. :slight_smile:

1 Like

Getting no output while trying to get upcoming sunday to work. I'm not a programmer so this might be a little beyond the time I have to learn it. Just testing some things, I'll keep at it knowing it's possible.

I was able to get the dates working for setting 1st date to tomorrow and the 2nd date to a week from it. can't quite figure out the always this coming sunday bit, but I can live without it i gues.

Option Explicit

Dim fileNum
fileNum = 0

Dim numPrefix
numPrefix = Array( 111, 190, 251, 341, 442, 405, 512, 590, 644, 715, 797, 810, 803, 910, 985 )

Dim newDate
Set newDate = DOpus.Create.Date()
newDate.Add 1, "d"

Dim futureDate
Set futureDate = DOpus.Create.Date()
futureDate.Add 14, "d"

Function OnGetNewName(ByRef getNewNameData)
    If fileNum <= UBound(numPrefix) Then
        Dim formattedTomorrowDate
        formattedTomorrowDate = newDate.Format("D#yyyy-MM-dd")
        
        Dim formattedFutureDate
        formattedFutureDate = futureDate.Format("D#yyyyMMdd")
        
        OnGetNewName = numPrefix(fileNum) & " " & getNewNameData.newname_stem & " " & formattedTomorrowDate & " " & formattedFutureDate & getNewNameData.newname_ext
        fileNum = fileNum + 1
    Else
        OnGetNewName = True
    End If
End Function

That seems to work as-is?

Not sure if you're screwing with me or not with that answer lol. I happened to make the post on the one day I don't need the code. If I could run the rename any day of the week and have the first date always be the nearest sunday and the 2nd date a week later, I'm there. I'll figure this thing out eventually.

You don't seem to have incorporated the code Leo gave you for next Sunday into your script?

Correct. I know the long standing rule is never to provide actual answers as far as finished code, I get it, but there are those like me that have spent hours getting this far and get stuck. What's the alternative to getting a working simple script? I have to pay some guy on fiver that knows what he's doing and he's done in five minutes or hope someone here sees the effort and helps out. Foolish optimism on my part, sorry for the bother.

The forum is full of finished scripts we've written for people's various esoteric requirements :slight_smile:

The thing is, you acted like you knew what you were doing, you posted a script that you said worked, and so Leo just showed you the single change you'd need to make to get the desired behavior.

If you post a script saying "this works, but" then we're going to assume we can just tell you where to change it to fix the "but".

Dim futureDate
Set futureDate = DOpus.Create.Date()
futureDate.Add 14, "d"

That will add 14 days to the current date. If you want next Sunday instead, change the Add line from:

futureDate.Add 14, "d"

to:

futureDate.Add 7 - futureDate.wday, "d" ' Bump to coming Sunday

Adding to what Jon said, when you said "getting no output" I assumed you meant the script you were using wasn't doing or outputting anything, at which point I tried the code you pasted and saw it was generating names for me.

I didn't notice you hadn't added my earlier suggested changes to that script, because I was focusing on the problem you said it had -- not outputing anything -- and couldn't work out how that was possible.

I'm a terrible communicator at times and was a little frustrated after trying so many different things. Sorry for assuming those things. I tried different AI to have them insert or change things and while they get some things to work, fail on many as well, but that's how I'm dealing with code that isn't dead simple to figure out. Again thanks for the guidance and help, I'll take another look and see if I can get it working. Love this program.

Dim fileNum
fileNum = 0

Dim numPrefix
numPrefix = Array( 111, 190, 251, 341, 442, 405, 512, 590, 644, 715, 797, 810, 803, 910, 985 )

Dim newDate
Set newDate = DOpus.Create.Date()
newDate.Add 1, "d"

Dim futureDate
Set futureDate = DOpus.Create.Date()
futureDate.Add 7 - futureDate.wday, "d"

Function OnGetNewName(ByRef getNewNameData)
    If fileNum <= UBound(numPrefix) Then
        Dim formattedTomorrowDate
        formattedTomorrowDate = newDate.Format("D#yyyyMMdd")
        
        Dim formattedFutureDate
        formattedFutureDate = futureDate.Format("D#yyyy-MM-dd")
        
        OnGetNewName = numPrefix(fileNum) & " " & getNewNameData.newname_stem & " " & formattedTomorrowDate & " " & formattedFutureDate & getNewNameData.newname_ext
        fileNum = fileNum + 1
    Else
        OnGetNewName = True
    End If
End Function

Running this today with the "upcoming Sunday" code gave me "111 New Text Document 20240722 2024-07-28" which is correct. If it works tomorrow I'm there. Thank you.

This script gave me the proper dates for upcoming Sundays as required.
...
Dim fileNum
fileNum = 0

Dim numPrefix
numPrefix = Array( 111, 190, 251, 341, 442, 405, 512, 590, 644, 715, 797, 810, 803, 910, 985 )

Dim newDate
Set newDate = DOpus.Create.Date()
newDate.Add 7 - newDate.wday, "d"

Dim futureDate
Set futureDate = DOpus.Create.Date()
futureDate.Add 7 - newDate.wday + 6, "d"

Function OnGetNewName(ByRef getNewNameData)
If fileNum <= UBound(numPrefix) Then
Dim formattedTomorrowDate
formattedTomorrowDate = newDate.Format("D#yyyyMMdd")

    Dim formattedFutureDate
    formattedFutureDate = futureDate.Format("D#yyyy-MM-dd")
    
    OnGetNewName = numPrefix(fileNum) & " " & getNewNameData.newname_stem & " " & formattedTomorrowDate & " " & formattedFutureDate & "." & getNewNameData.newname_ext
    fileNum = fileNum + 1
Else
    OnGetNewName = True
End If

End Function
...

Please see Formatting tips specific to the Opus forum (also linked at the top of the post editor if you ever need to refer back to it) for how to post code blocks, so we don't have to keep fixing your posts.