Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


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
  • 20,962 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
StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#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,251 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

Posted Image AutoIt Russian CommunityPosted Image Projects: 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 ProgramPosted Image UDFs: 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 UDFPosted Image Examples: 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 DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

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