Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Renaming and file extensions

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.

Member function str.ChangeFilename
Copy      Help
function $newFilename

;Replaces filename. Does not change extension.

;newFilename - new filename. Must be without extension.

;This variable initially can contain filename with extension, or full file path. Can be enclosed in ".

;str f="c:\f\A.gif"
;out f

_s.from(newFilename "$1")
this.replacerx("(?<=\\|^)[^\\/|:''\r\n]+(\.[^\.\\/|:\r\n]+)$" _s 4)

more examples
Macro Macro1545
Copy      Help
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''"


out file1
out file2
out file3
out file4
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 ; )
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!
Function FileRenameNoExt
Copy      Help
function $oldFilePathName $newNameWithoutExt

;Renames file, leaving same extension.

;oldFilePathName - full path of existing file.
;newNameWithoutExt - new file name, without path and without extension.

;FileRenameNoExt "$desktop$\test.txt" "test2"

ren* oldFilePathName _s.from(newNameWithoutExt PathFindExtension(oldFilePathName))
err end _error
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!


Set Details view in file Open/Save dialogs

Forum Jump:

Users browsing this thread: 1 Guest(s)