Jump to content



Photo

How to Rename a File with AutoIt?


  • Please log in to reply
4 replies to this topic

#1 charvi

charvi

    Prodigy

  • Active Members
  • PipPipPip
  • 150 posts

Posted 05 January 2009 - 03:18 PM

Hi everybody,

How can I rename a file with AutoIt? I have not found a Rename or FileRename function???

Cheers, Charvi







#2 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 23,003 posts

Posted 05 January 2009 - 03:23 PM

@charvi,

From the helpfile:

"Because AutoIt lacks a "FileRename" function, use FileMove to rename a file!"

M23
My UDFs:
Spoiler

#3 charvi

charvi

    Prodigy

  • Active Members
  • PipPipPip
  • 150 posts

Posted 05 January 2009 - 04:24 PM

@charvi,

From the helpfile:

"Because AutoIt lacks a "FileRename" function, use FileMove to rename a file!"

M23

Thank you for your quick reply, Melba23.

Well, I then have written _FileRename_ and _DirRename_ functions so it no longer "lacks" in AutoIt :)

I hope it might be useful. Please note that the identation is correct in the SciTe editor.

Cheers, Charvi

Plain Text         
; ## FUNCTION ##================================================================================================== ================= ; Name ..............:  _FileRename_ ; Description .......:  Rename a File ; Syntax ............:  _FileRename_($s_Source, $s_Destination, $i_Flag = 0) ; Parameters ........:  $s_Source               Source File ;                       $s_Destination          Destination File ;                       $i_Flag                 Flag    0 = (default) do not overwrite existing files ;                                                       1 = overwrite existing files ;                                                       8 = Create destination directory structure if it doesn't exist ; Return values .....:  Success:    Returns 1 ;                       Failure:    Returns 0 ; Author ............:  'charvi' <charvista at gmail dot com> ; Creation Date .....:  2009-01-05 ; Version ...........:  0.0.0.1 ; Version Date ......:  2009-01-05 ; Remarks ...........:  If the source and destination paths are on different volumes a copy and delete operation is performed rather than a move. ;                       The destination directory must already exist, except using with flag value '8'. ;                       For instance the combined flag '9' (1 + 8) overwrites the target file and prechecks for the destination directory structure and if it doesn't exist creates it automatically. ;                       Some file attributes can make the overwriting impossible. ;=================================================================================================== =============================== Func _FileRename_($s_Source, $s_Destination, $i_Flag = 0)     Local $i     $i = FileMove($s_Source, $s_Destination, $i_Flag)     Return $i EndFunc


Plain Text         
; ## FUNCTION ##================================================================================================== ================= ; Name ..............:  _DirRename_ ; Description .......:  Rename a Directory (or "Folder" if you prefer) ; Syntax ............:  _DirRename_($s_Source, $s_Destination, $i_Flag = 0) ; Parameters ........:  $s_Source               Source Directory ;                       $s_Destination          Destination Directory ;                       $i_Flag                 Flag    0 = (default) do not overwrite existing directory ;                                                       1 = overwrite existing directory ; Return values .....:  Success:    Returns 1 ;                       Failure:    Returns 0 ; Author ............:  'charvi' <charvista at gmail dot com> ; Creation Date .....:  2009-01-05 ; Version ...........:  0.0.0.1 ; Version Date ......:  2009-01-05 ; Remarks ...........:  If the source and destination are on different volumes or UNC paths are used then a copy/delete operation will be performed rather than a move. ;                       If the destination already exists and the overwrite flag is specified then the source directory will be moved INSIDE the destination. ;=================================================================================================== =============================== Func _DirRename_($s_Source, $s_Destination, $i_Flag = 0)     Local $i     $i = DirMove($s_Source, $s_Destination, $i_Flag)     Return $i EndFunc


#4 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,266 posts

Posted 05 January 2009 - 07:18 PM

There is no point to make such UDF, its' can be built like that:

Func _FileRename_($s_Source, $s_Destination, $i_Flag = 0)     Return FileMove($s_Source, $s_Destination, $i_Flag) EndFunc


Now, what is the different between the original usage (FileMove()), just a function name? :)

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1



AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler


Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *


==================================================
My_Userbar.gif
==================================================

 

AutoIt is simple, subtle, elegant. © AutoIt Team


#5 GEOSoft

GEOSoft

    Sure I'm senile. What's your excuse?

  • MVPs
  • 10,573 posts

Posted 05 January 2009 - 07:30 PM

The help file gives a good example of handling this situation and there is no need for a function at all.
$sFileOld = "MyFile.au3" $sFileRenamed = "MyNewFile.au3" FileMove($sFileOld, $sFileRenamed)

or
$sFileOld = "C:\MyFiles" $sFileRenamed = "C:\MyNewFiles" DirMove($sFileOld, $sFileRenamed)


At one time we had a FileRename() function in AutoIt but it was deemed to be redundant and obsolete. So it's not considered to be "missing" functionality. I have a feeling that you are comparing it to antiquated syntax such as that used in VBScript.
GeorgeQuestion about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else."Old age and treachery will always overcome youth and skill!"




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users