Sign in to follow this  
Followers 0

How to Rename a File with AutoIt?

5 posts in this topic

#1 ·  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



#2 ·  Posted

@charvi,

From the helpfile:

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert - Easily convert date/time formats, including the language used
ExtMsgBox ------------- A highly customisable replacement for MsgBox
GUIExtender ----------- Extend and retract multiple sections within a GUI
GUIFrame -------------- Subdivide GUIs into many adjustable frames
GUIListViewEx -------- Insert, delete, move, drag, sort and edit ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ---------------- Scrolling tickertape GUIs
NoFocusLines --------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify -------------------- Small notifications on the edge of the display
Scrollbars -------------- Automatically sized scrollbars with a single command
StringSize -------------- Automatically size controls to fit text
Toast -------------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#3 ·  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

#4 ·  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? :)


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...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

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

Share this post


Link to post
Share on other sites

#5 ·  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.


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!"

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