Jump to content

File Rename


 Share

Recommended Posts

-----------------------------------------------------------------------------------------------------------------------------------------------

#include <Array.au3>

Global $locations = "C:\Documents and Settings\Administrator\My Documents\My Pictures\"

Local $i, $get = False

Dim $Found_Files[1]

$Total_files = DirGetSize($locations, 3)

$Fh = FileFindFirstFile($locations & "*.*")

$i = 0

While 1

$found = FileFindNextFile($Fh)

If @error Then ExitLoop

;~ ConsoleWrite($found&@CRLF)

$34 = StringReplace($locations&$found, $found, "PICTURE"&$i&".JPG",0)

ConsoleWrite($34&@CRLF)

If $Found_Files[0] = "" Then

_ArrayPush($Found_Files, $34, 1)

Else

_ArrayAdd($Found_Files, $34)

EndIf

$i += 1

WEnd

_ArrayDisplay($Found_Files, "Search Result")

-----------------------------------------------------------------------------------------------------------------------------------------------

I want to rename that files via Autoit i desing this script but it does not files rename

so help me in this script.

Files_Rename.au3

Link to comment
Share on other sites

filemove () and use a different name for the output will rename it

Link to comment
Share on other sites

  • Moderators

SAEED,

First, welcome to the AutoIt forums.

A good start - some code to work on and a clear question. I wish some others would do the same. :-)

From the Help file: "Because AutoIt lacks a "FileRename" function, use FileMove to rename a file!". So we need to get a list of all the files (your code basically does that already) and then loop through them with FileMove to change their names.

Have a look at this:

#include <Array.au3>
Global $locations = "C:\Documents and Settings\Administrator\My Documents\My Pictures\"
Local $i, $get = False
Global $Found_Files[1] ; Try not to use Dim!

$Total_files = DirGetSize($locations, 3)
$Fh = FileFindFirstFile($locations & "*.*")
While 1
    $found = FileFindNextFile($Fh)
    If @error Then ExitLoop
    ConsoleWrite($found & @CRLF)
    If $Found_Files[0] = "" Then
        _ArrayPush($Found_Files, $found, 1)
    Else
        _ArrayAdd($Found_Files, $found)
    EndIf
WEnd
FileClose($Fh)  ; You should close the Search file handle!

_ArrayDisplay($Found_Files, "Search Result")

For $i = 0 To UBound($Found_Files) - 1
    FileMove($locations & $Found_Files[$i], $locations & "PICTURE" & $i & ".JPG", 0)
Next

The array is now a list of all the files in your folder with their current names. The final loop then renames them using FileMove.

2 other points:

1 - You should close the search file handle when the search is over. Tha tHelpfile again: "When you have finished searching with the FileFind... functions you must call FileClose() to release the search handle."

2. Do not use Dim (unless you want to empty an existing array) - Global/Local are much better as they scope the array immediately.

Ask if you have any questions.

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

 

Link to comment
Share on other sites

Thanks alot Dear to solve my problam, its true i am a new user of Autoit.

i am designing an other program like a Windows "search" but i have some problam in my "search Program". If u dont mind can i send my script to you for better solution.

waiting ur Nice Reply..

M-SAEED

Link to comment
Share on other sites

  • Moderators

SAEED,

The forum is here so the whole community can benefit. If you have a problem with your "search" script, please open a new thread - that way many forum members can help and (perhaps) learn! Sending PMs asking for help is frowned upon here. ;-)

When you do post your script, please use Code tags - put [code ] before and [/code ] after your posted code (but omit the trailing space - it is only there so the tags display here).

Jos,

I had exactly the same reaction - quite made my day! :-)

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

 

Link to comment
Share on other sites

  • 1 month later...

SAEED,

First, welcome to the AutoIt forums.

A good start - some code to work on and a clear question. I wish some others would do the same. :-)

From the Help file: "Because AutoIt lacks a "FileRename" function, use FileMove to rename a file!". So we need to get a list of all the files (your code basically does that already) and then loop through them with FileMove to change their names.

Have a look at this:

#include <Array.au3>
Global $locations = "C:\Documents and Settings\Administrator\My Documents\My Pictures\"
Local $i, $get = False
Global $Found_Files[1]; Try not to use Dim!

$Total_files = DirGetSize($locations, 3)
$Fh = FileFindFirstFile($locations & "*.*")
While 1
    $found = FileFindNextFile($Fh)
    If @error Then ExitLoop
    ConsoleWrite($found & @CRLF)
    If $Found_Files[0] = "" Then
        _ArrayPush($Found_Files, $found, 1)
    Else
        _ArrayAdd($Found_Files, $found)
    EndIf
WEnd
FileClose($Fh) ; You should close the Search file handle!

_ArrayDisplay($Found_Files, "Search Result")

For $i = 0 To UBound($Found_Files) - 1
    FileMove($locations & $Found_Files[$i], $locations & "PICTURE" & $i & ".JPG", 0)
Next

The array is now a list of all the files in your folder with their current names. The final loop then renames them using FileMove.

2 other points:

1 - You should close the search file handle when the search is over. Tha tHelpfile again: "When you have finished searching with the FileFind... functions you must call FileClose() to release the search handle."

2. Do not use Dim (unless you want to empty an existing array) - Global/Local are much better as they scope the array immediately.

Ask if you have any questions.

M23

Instead of renaming the files with PICTURE & $i, would it be possible to rename the files with the same name but stripped of unwanted characters? Or perhaps strip the unwanted character before it's stored in the array?

Such as...

Old Names

12345_r.tif

123456_c.tif

New Names

12345r.tif

123456c.tif

Link to comment
Share on other sites

Instead of renaming the files with PICTURE & $i, would it be possible to rename the files with the same name but stripped of unwanted characters? Or perhaps strip the unwanted character before it's stored in the array?

Such as...

Old Names

12345_r.tif

123456_c.tif

New Names

12345r.tif

123456c.tif

Since the original names are needed for the rename, the array should contain unaltered information. (Unless you store this elsewhere)
FileMove($locations & $Found_Files[$i], $locations & StringReplace($Found_Files[$i],  "_", ""), 0)

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...