Sign in to follow this  
Followers 0

How to Rename a File with AutoIt?

5 posts in this topic

Posted

Hi everybody,

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

Cheers, Charvi

Share this post


Link to post
Share on other sites



Posted

@charvi,

From the helpfile:

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

M23

Share this post


Link to post
Share on other sites

Posted

@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

; ## 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

; ## 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

Share this post


Link to post
Share on other sites

Posted

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? :)

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.