GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Copy File into Today's Version suffix

Clipboard Image


In many companies or driven purely by personal wills, you would tend to name your files with #Date_Version# suffix to make things neat. -- In my case, I will use #yymmdd# or #yyyymmdd#, to make sure files are automatically sorted in chronological order.
However this causes pain while you copy file into todays version -- you have to copy yesterday's file and rename it carefully.
This Script Button is to cover that little pain to do it automatically.

  • Execute on file that not fitting the naming pattern, nothing happens it'll ask you to create into naming-complaint file from today or not
  • Execute on naming-complaint file, copy and rename into today's version #1 => if target filename exists then prompt into #2,3,4... or replace it

Only works on files yet, and I'll think on folders further.
I am not in coding profession, so in case you see smarter way of the codes, I would love to be pointed out :slight_smile:
Hope it helps with you.
Copy2Today.dcf (8.7 KB)

updated 2020-03-18T16:00:00Z

Option Explicit
Function OnClick(ByRef clickData)
	DOpus.ClearOutput
	Dim cmd, lister, tab, selItem, folderEnum, folderItem
	' ---------------------------------------------------------
	Set cmd = clickData.func.command
	' cmd.deselect = false ' Prevent automatic deselection

	' DOpus.Output "Selected items in " & clickData.func.sourcetab.path & ":"
	If clickData.func.sourcetab.selected.count = 0 OR clickData.func.sourcetab.selected(0).is_dir Then
		' DOpus.Output "  (none)"
		Exit function
	Else
		' DOpus.Output "  (f) " & clickData.func.sourcetab.selected(0).RealPath
		Set selItem=clickData.func.sourcetab.selected(0)

		dim  sDirectorySlash, sNameExt, sName ,sExt
		sDirectorySlash=left(selItem.RealPath,(InStrRev(selItem.RealPath,"\")))
		' DOpus.Output sDirectorySlash
		sNameExt = Right(selItem.RealPath,Len(selItem.RealPath)-(InStrRev(selItem.RealPath,"\")))
		sExt = right(sNameExt,len(sNameExt)-InStrRev(sNameExt,".")+1)
		' DOpus.Output sNameExt & sExt
		sName = left(sNameExt,InStrRev(sNameExt,".")-1)

		' DOpus.Output sName
		dim test, pat8, pat6
		' test = "List of open points_20190209_v#"
		' pat1 = "........_v.$"
		pat8 = "[0-9]{8}_v.$"
		pat6 = "[0-9]{6}_v.$"
		' DOpus.Output(test & " RegEx """ & pat & """ = " & RegExTest(test, pat))
		' DOpus.Output(RegExTest(sName, pat1))
		dim sLeftPart, sOldDate, sNewDate, sNewVer, sOldVer, sNewName
		sOldVer = right(sName,1)
		if RegExTest(sName, pat8) Then
			sLeftPart=left(sName, len(sName)-11)
			sOldDate = right(left(sname,len(sname)-3),8)
			sNewDate=YYYYMMDD
			sNewVer = 1
			if sOldDate=YYYYMMDD then 
				sNewVer = cstr(sOldVer +1)
			end if
			sNewName=sLeftPart & sNewDate & "_v" & sNewVer
			' msgbox sNewName


			' msgbox "True1"
		' msgbox YYMMDD & YYYYMMDD
		elseif RegExTest(sName, pat6) Then 
			sLeftPart=left(sName, len(sName)-9)
			sOldDate = right(left(sname,len(sname)-3),6)
			sNewDate=YYMMDD
			sNewVer = 1
			if sOldDate=YYMMDD then 
				sNewVer = cstr(sOldVer +1)
			end if
			sNewName=sLeftPart & sNewDate & "_v" & sNewVer
			' msgbox sNewName
		else
			dim iCreateNow 
			iCreateNow=msgbox( "The file is not named properly." & chr(10) & chr(13) & "Would you like to create a proper version from today?", vbYesNoCancel, "Copy2Today")
			select case iCreateNow
				
			case vbYes
				sNewDate=YYYYMMDD
				sNewVer = 1
				sNewName=sName & "_" & sNewDate & "_v" & sNewVer
			case vbno
				exit function
		end select
			
		end if


		dim sNewFullName
		sNewFullName=sDirectorySlash & sNewName & sExt
		dim fso, oldfile
		set fso = CreateObject ("Scripting.FileSystemObject")

		if	fso.FileExists(sNewFullName) then 
			dim iRes
			iRes=msgbox( "The target today version is already existing." & chr(10) & chr(13) & "Do you want to replace it or create into newer version? if No you will replace v1.", vbYesNoCancel, "Copy2Today")
			select case iRes
			
				case vbYes
					do while  fso.FileExists(sNewFullName)
					sNewFullName=left(sNewFullName,len(sNewFullName)-len(sExt)-1) & cstr(right(left(sNewFullName,len(sNewFullName)-len(sExt)),1)+1) & sExt
					Loop
				case vbno
					fso.deletefile(sNewFullName)
				case else
					exit function
			end select
			
		end If
		set oldfile = fso.GetFile (selItem.RealPath)
		oldfile.Copy(sNewFullName)
		' clickData.func.sourcetab.selected.AddFile sNewFullName
		cmd.AddFile sNewFullName
	  	cmd.RunCommand "Select FROMSCRIPT"
	End If

End Function



Function RegExTest(str, pat)
	dim RE
	Set RE = New RegExp
	RE.IgnoreCase = True
	RE.Pattern = pat
	RegExTest = RE.Test(str)
End Function

Function YYMMDD()
	Dim t
	t=Now
	YYMMDD=Right(Year(t),2) & Right("0" & Month(t),2) & Right("0" & Day(t),2)
End Function

Function YYYYMMDD()
	Dim t
	t=Now
	YYYYMMDD=Year(t) & Right("0" & Month(t),2) & Right("0" & Day(t),2)
End Function
4 Likes

BTW Could anyone kindly show me what's the most proper way of Select given file/folder in DOpus VBScirpting please?
It's an elementary level question, however as I search for it in the forum I don't get quite clear clue.
Did I do it correctly in my script?

Thanks.

Hu? Can you try to ask in a different way? It's difficult to make sense of your sentence.

Sorry I'll rephrase.
given that:

I Have opened D:\ at my lister.
and below i have clear variant difined:

FilePath = "D:\Testing.txt"

how to write further in DOpus VBScript to select it programmatically?

Are you referring to this part of your script?

cmd.AddFile sNewFullName
cmd.RunCommand "Select FROMSCRIPT"

Looks alright to me. To make sure you always end up with just this one file selected you could add two lines:

cmd.ClearFiles
cmd.AddFile sNewFullName
cmd.RunCommand "Select NONE"
cmd.RunCommand "Select FROMSCRIPT"

Yes exactly this part.
Thanks Lxp!

Thanks for the nice script. Can any one Help me to change the date format in to dd-mm-yyyy-hh-mm-ss.
for instance my file name is: testfile
after I click this button the copied file name should be testfile 17-07-2020_05-58-15_v1
Here 17-07-2020 is todays date and the time is 05 is hh 58 is mm and 15 is second and v1 is version1.
How to do that?

Bear in mind, if someone provides you with a solution answer to this, your files will be sorted by day of month. You could end up with:

testfile 16-07-2020_05-58-15_v1
testfile 17-07-2017_05-58-15_v1
testfile 17-07-2017_05-58-15_v1
testfile 17-08-2018_05-58-15_v1
testfile 17-08-2019_05-58-15_v1
testfile 18-07-2016_05-58-15_v1
testfile 18-07-2019_05-58-15_v1
testfile 18-08-2008_05-58-15_v1
testfile 18-08-2018_05-58-15_v1
testfile 19-01-2004_05-58-15_v1

When sorting by filename, you'll be giving the highest precedence (after the text portion) to the day of the month, then the month, and then the year.

This is what I want Desertdwarf

Hi, Khalidhosain,
Replace my original Function YYYYMMDD with:

Function YYYYMMDD()
    Dim t
    t=Now
    YYYYMMDD== Right("0" & Day(t),2) & "-" & _
    Right("0" & Month(t),2)  & "-" & _
    Year(t) & "_" & _  
    Right("0" & Hour(t),2) & _
    Right("0" & Minute(t),2)    & _ 
    Right("0" & Second(t),2) 
End Function
1 Like

Hi Bryan.H
I have trying to replace that but it's create an error.

here is what i have done!

Option Explicit
Function OnClick(ByRef clickData)
	DOpus.ClearOutput
	Dim cmd, lister, tab, selItem, folderEnum, folderItem
	' ---------------------------------------------------------
	Set cmd = clickData.func.command
	' cmd.deselect = false ' Prevent automatic deselection

	' DOpus.Output "Selected items in " & clickData.func.sourcetab.path & ":"
	If clickData.func.sourcetab.selected.count = 0 OR clickData.func.sourcetab.selected(0).is_dir Then
		' DOpus.Output "  (none)"
		Exit function
	Else
		' DOpus.Output "  (f) " & clickData.func.sourcetab.selected(0).RealPath
		Set selItem=clickData.func.sourcetab.selected(0)

		dim  sDirectorySlash, sNameExt, sName ,sExt
		sDirectorySlash=left(selItem.RealPath,(InStrRev(selItem.RealPath,"\")))
		' DOpus.Output sDirectorySlash
		sNameExt = Right(selItem.RealPath,Len(selItem.RealPath)-(InStrRev(selItem.RealPath,"\")))
		sExt = right(sNameExt,len(sNameExt)-InStrRev(sNameExt,".")+1)
		' DOpus.Output sNameExt & sExt
		sName = left(sNameExt,InStrRev(sNameExt,".")-1)

		' DOpus.Output sName
		dim test, pat8, pat6
		' test = "List of open points_20190209_v#"
		' pat1 = "........_v.$"
		pat8 = "[0-9]{8}_v.$"
		pat6 = "[0-9]{6}_v.$"
		' DOpus.Output(test & " RegEx """ & pat & """ = " & RegExTest(test, pat))
		' DOpus.Output(RegExTest(sName, pat1))
		dim sLeftPart, sOldDate, sNewDate, sNewVer, sOldVer, sNewName
		sOldVer = right(sName,1)
		if RegExTest(sName, pat8) Then
			sLeftPart=left(sName, len(sName)-11)
			sOldDate = right(left(sname,len(sname)-3),8)
			sNewDate=YYYYMMDD
			sNewVer = 1
			if sOldDate=YYYYMMDD then 
				sNewVer = cstr(sOldVer +1)
			end if
			sNewName=sLeftPart & sNewDate & "_v" & sNewVer
			' msgbox sNewName


			' msgbox "True1"
		' msgbox YYMMDD & YYYYMMDD
		elseif RegExTest(sName, pat6) Then 
			sLeftPart=left(sName, len(sName)-9)
			sOldDate = right(left(sname,len(sname)-3),6)
			sNewDate=YYMMDD
			sNewVer = 1
			if sOldDate=YYMMDD then 
				sNewVer = cstr(sOldVer +1)
			end if
			sNewName=sLeftPart & sNewDate & "_v" & sNewVer
			' msgbox sNewName
		else
			dim iCreateNow 
			iCreateNow=msgbox( "The file is not named properly." & chr(10) & chr(13) & "Would you like to create a proper version from today?", vbYesNoCancel, "Copy2Today")
			select case iCreateNow
				
			case vbYes
				sNewDate=YYYYMMDD
				sNewVer = 1
				sNewName=sName & "_" & sNewDate & "_v" & sNewVer
			case vbno
				exit function
		end select
			
		end if


		dim sNewFullName
		sNewFullName=sDirectorySlash & sNewName & sExt
		dim fso, oldfile
		set fso = CreateObject ("Scripting.FileSystemObject")

		if	fso.FileExists(sNewFullName) then 
			dim iRes
			iRes=msgbox( "The target today version is already existing." & chr(10) & chr(13) & "Do you want to replace it or create into newer version? if No you will replace v1.", vbYesNoCancel, "Copy2Today")
			select case iRes
			
				case vbYes
					do while  fso.FileExists(sNewFullName)
					sNewFullName=left(sNewFullName,len(sNewFullName)-len(sExt)-1) & cstr(right(left(sNewFullName,len(sNewFullName)-len(sExt)),1)+1) & sExt
					Loop
				case vbno
					fso.deletefile(sNewFullName)
				case else
					exit function
			end select
			
		end If
		set oldfile = fso.GetFile (selItem.RealPath)
		oldfile.Copy(sNewFullName)
		' clickData.func.sourcetab.selected.AddFile sNewFullName
		cmd.AddFile sNewFullName
	  	cmd.RunCommand "Select FROMSCRIPT"
	End If

End Function


'  Use . as a wildcard character instead of #:


Function RegExTest(str, pat)
	dim RE
	Set RE = New RegExp
	RE.IgnoreCase = True
	RE.Pattern = pat
	RegExTest = RE.Test(str)
End Function

Function YYMMDD()
dim yyyy, m, d, yy, mm, dd
yyyy=cstr(year(now()))
m=cstr(month(now()))
d=cstr(day(now()))
yy=right(yyyy,2)
mm=m
dd=d

if len(m) =1 then mm = "0" & m
if len(d) =1 then dd = "0" & d
YYMMDD=yy & mm & dd
end function

Function YYYYMMDD()
    Dim t
    t=Now
    YYYYMMDD== Right("0" & Day(t),2) & "-" & _
    Right("0" & Month(t),2)  & "-" & _
    Year(t) & "_" & _  
    Right("0" & Hour(t),2) & _
    Right("0" & Minute(t),2)    & _ 
    Right("0" & Second(t),2) 
End Function

Change this part so it has only one equals sign:

YYYYMMDD== Right("0" & Day(t),2) & "-" & _

should be

YYYYMMDD = Right("0" & Day(t),2) & "-" & _

I've tested it and there are no errors and it's working correctly.

I've made a button for you to import in case you'd rather do that.

Copy2Today.dcf (8.7 KB)

1 Like

Thanks DesertDwarf
everything is ok but the time format is not OK. I want time in hh:mm:ss format.

Hi, Khalidhosain,
You should be able to do essential code reading and adaptation by your own, otherwise this discussion will never end.
Please change the

 YYYYMMDD== Right("0" & Day(t),2) & "-" & _
    Right("0" & Month(t),2)  & "-" & _
    Year(t) & "_" & _  
    Right("0" & Hour(t),2) & _
    Right("0" & Minute(t),2)    & _ 
    Right("0" & Second(t),2) 

into

 YYYYMMDD== Right("0" & Day(t),2) & "-" & _
    Right("0" & Month(t),2)  & "-" & _
    Year(t) & "_" & _  
    Right("0" & Hour(t),2) & ":" &_
    Right("0" & Minute(t),2)    &  ":" & _ 
    Right("0" & Second(t),2) 

I hope you can compare these two segments and understand how the trick works.
And then probably Windows doesn't allow you to use ":" so you can change into "-".

1 Like

Hi Bryan.H
Thank for your reply.

Yes, I replace the part of the code as you say. It's Works in the right format as I want. Thanks for that.
But now it stops create version 2. It just loops version1. If you Annoyed at me, then you can end this discussion here. Thank you so much for helping me.

It's OK, I foresee this would be potential risk due to my script will check if the same timestamp exists (duplicate) then creat v+1, but with your concept, it'll never be duplicated cause you have hhmmss.
Do u get me?

But further adaptation is still possible -- just check the part of "if"

Very minor thing I noticed in this code:

yyyy=cstr(year(now()))
m=cstr(month(now()))
d=cstr(day(now()))

If the script ran just before midnight, it might get Y on one day and M + D on the next, or Y + M on one day and D on the next. If the days were on different months then you could end up going back in time a year or a month. While it's unlikely, it's not impossible (I've seen it happen). I'd do this:

nowtime = now()
yyyy=cstr(year(nowtime))
m=cstr(month(nowtime))
d=cstr(day(nowtime))

Thanks Leo.
Yes it's potential risk.
However I've noticed that and published an update yesterday to avoid that.

1 Like

Hi, Khalidhosain,
I looked through the code and find that you need to do further adaptation:
my script will check if selected file is with naming pattern "8digit_v#", if yes then create newer version, else create this suffix.
in your case, you have to also adapt the pattern here, like into "##-##-####_##-##-##_v#", otherwise it'll loop with "adding suffix".
You need to change the variable "pat8", remove "pat6", with knowledge of RegEx.
And also remove the following segment:

elseif RegExTest(sName, pat6) Then 
			sLeftPart=left(sName, len(sName)-9)
			sOldDate = right(left(sname,len(sname)-3),6)
			sNewDate=YYMMDD
			sNewVer = 1
			if sOldDate=YYMMDD then 
				sNewVer = cstr(sOldVer +1)
			end if
			sNewName=sLeftPart & sNewDate & "_v" & sNewVer

Please try and you'll find it not that hard and enjoy the experience.