Jump to content
Sign in to follow this  
charvi

How to Rename a File with AutoIt?

Recommended Posts

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

@charvi,

From the helpfile:

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

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
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, edit and colour 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

@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

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


 

Spoiler

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

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

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.


George

Question 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

I still think the function is reasonable to be improved, if you have noticed that the FileMove function does not have a case sensitive flag,

Try this: FileMove("c:\myfile\Files001.txt", "c:\myfile\FILES001.txt")

This might does not matter in windows, however, I would like to see the file name as we expected. Therefore, a wrapper to move it to a temp name and then to the final name might be useful.

 

Share this post


Link to post
Share on other sites

Not to encourage resurrecting a nearly 10 year old thread for something like this, but there is a way to have it with the case that you want without going through the process of creating a temp file first. Use it like this. 

FileMove("c:\myfile\Files001.txt", "c:\myfile\FILES001.txt", $FC_OVERWRITE) ; $FC_OVERWRITE (1) = overwrite existing files.

Use the overwrite flag to have it overwrite the file if it exists already to have it with the changed case.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

×
×
  • Create New...