Posts: 121
Threads: 33
Joined: Jun 2007
Hi,
Is there a simple way to rename a file with a string, without messing with the file's extension?
For example, the macro generates a Name String like "2011-02-08_press_conference" based on user input
The above Name String will later be used to rename several different files of various types (e.g., transcript.doc becomes 2011-02-08_press_conference.doc, file00001.wav becomes 2011-02-08_press_conference.wav, dsc4006.jpg becomes 2011-02-08_press_conference.jpg) at different points throughout the macro.
.
I can write code to do this, but was wondering if I was missing a simpler way than PathFindExtension etc.
Thanks!
Posts: 12,140
Threads: 142
Joined: Dec 2002
Member function
str.ChangeFilename
function $newFilename
;Replaces filename. Does not change extension.
;newFilename - new filename. Must be without extension.
;REMARKS
;This variable initially can contain filename with extension, or full file path. Can be enclosed in ".
;EXAMPLE
;str f="c:\f\A.gif"
;f.ChangeFilename("B")
;out f
_s.from(newFilename "$1")
this.replacerx("(?<=\\|^)[^\\/|:''\r\n]+(\.[^\.\\/|:\r\n]+)$" _s 4)
more examples
Macro
Macro1545
str newName="three"
str file1="one.txt"
str file2="c:\f\two.gif"
str file3="one ~1.~xt"
str file4="''c:\f f\two.gif''"
file1.ChangeFilename(newName)
file2.ChangeFilename(newName)
file3.ChangeFilename(newName)
file4.ChangeFilename(newName)
out file1
out file2
out file3
out file4
Posts: 121
Threads: 33
Joined: Jun 2007
Great, was just learning/writing function using PathFindExtension and thought, "bet he already beat me to the punch." I will continue to write it so I can learn and stop bothering you ; )
Posts: 121
Threads: 33
Joined: Jun 2007
Hi Gintaras,
As an alternate version, can you show me how to add the ren* operation be added to the member function above so it goes ahead and renames the original file with the new name+orig extension?
Thanks much!
Posts: 12,140
Threads: 142
Joined: Dec 2002
Function
FileRenameNoExt
;/
function $oldFilePathName $newNameWithoutExt
;Renames file, leaving same extension.
;oldFilePathName - full path of existing file.
;newNameWithoutExt - new file name, without path and without extension.
;EXAMPLE
;FileRenameNoExt "$desktop$\test.txt" "test2"
ren* oldFilePathName _s.from(newNameWithoutExt PathFindExtension(oldFilePathName))
err end _error
Posts: 121
Threads: 33
Joined: Jun 2007
Small problem - Seems to be missing the "." between new filename and extension.
--
Off topic: do you know a way to force the OpenSave dialog to "details" view throughout XP? From what I've read, it's not possible, but thought if anyone knew...
If there is no global XP solution, is it at least possible that when OpenSave is called within a qm macro, you could program the view change to details?
Thanks, as always!
Posts: 12,140
Threads: 142
Joined: Dec 2002