Jump to content

Plz Help For renaming Files


Recommended Posts

How can I rename the name of first image (Only first one file) of every folder ?

Sample:

folder
             folder 2
                                   asssszz.gif   <=====
                                   asssszz2.gif
                                   asssszz3.gif
             folder 3
                                   asssssaszz.gif        <=====
                                   asssxsxszz2.gif
                                   asssxaxszz3.gif


             folder 4
                                   asssssadasdszz.gif        <=====
                                   asssxsxszz2.gif
                                   asssxaxszz3.gif

this files can be renamed to 1.gif

Link to comment
Share on other sites

some ideas

If _FileListToArray() return that file on first position use FileMove to rename it.

If it dont then you can try to split all array result on '.' with

$split = StringSplit($array[$x], '.')

and test right most with

StringRight($split[1],1)

to cee if it have number with

If StringIsDigit(StringRight($split[1],1)) Then

so that you can locate correct file for renameing.

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

  • Moderators

Are the folders named in some similar fashion (Folder1, Folder2, etc.)? If so, you could always loop through the folders and use FileFindFirstFile to grab the first one in each folder.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Are the folders named in some similar fashion (Folder1, Folder2, etc.)? If so, you could always loop through the folders and use FileFindFirstFile to grab the first one in each folder.

thanks plz give me a sample code for this

my folders have some different names I try to count them and loop then try to find first file of each folder but it shows me just first file of first folder and does n't come out from loop.

#include <Array.au3>
Global $locations = "C:Documents and SettingsAll UsersDocumentsMy PicturesSample 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 & StringReplace($Found_Files[$i],  "_", ""), 0)
Next
Edited by AHRIMANSEFID
Link to comment
Share on other sites

try it like this

(test it on small amount of folders first)

to se do it return array correctly

#include <Array.au3>

$files = _FileGetFirstFiles(@DesktopDir&'script',"*.txt")
_ArrayDisplay($files,'before')
For $x = 1 To $files[0]
$split = StringSplit($files[$x], '.')
If Not StringIsDigit(StringRight($split[$split[0]-1],1)) Then
     $split[$split[0]] = '0.'&$split[$split[0]]
        $files[$x] = _ArrayToString($split)
    EndIf
Next
_ArrayDisplay($files,'after')


Func _FileGetFirstFiles($sDir,$searchString)
Local $return[1],$search,$file
Local $arr = _GetFilesFolder_Rekursiv($sDir,'*',1)
;~   _ArrayDisplay($arr,'tree_array')
For $x = 1 To $arr[0]
     $search = FileFindFirstFile($arr[$x]&$searchString)
     If @error Then
         FileClose($search)
     Else
         While 1
             $file = FileFindNextFile($search)
             If @error Then ExitLoop
             _ArrayAdd($return,$arr[$x]&$file)
             ExitLoop
         WEnd
         FileClose($search)
     EndIf
Next
$return[0] = UBound($return)-1
If $return[0] Then
     Return $return
Else
     SetError(1)
EndIf
EndFunc
;==================================================================================================
; Function Name: _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:   Recursive listing of files and/or folders
; Parameter(s): $sPath   Basicpath of listing ('.' -current path, '..' -parent path)
;                $sExt   Extension for file selection '*' or -1 for all (Default)
;                $iDir   -1 Files+Folder(Default), 0 only Files, 1 only Folder
;    optional: $iRetType 0 for Array, 1 for String as Return
;    optional: $sDelim Delimiter for string return
;                            0 -@CRLF (Default) 1 -@CR 2 -@LF 3 -';' 4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                Array[0] includes count of found files/folder
; Author(s):     BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
Global $oFSO = ObjCreate('Scripting.FileSystemObject')
Global $strFiles = ''
Switch $sDelim
     Case '1'
         $sDelim = @CR
     Case '2'
         $sDelim = @LF
     Case '3'
         $sDelim = ';'
     Case '4'
         $sDelim = '|'
     Case Else
         $sDelim = @CRLF
EndSwitch
If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
If $sExt = -1 Then $sExt = '*'
If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
_ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
If $iRetType = 0 Then
     Local $aOut
     $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
     If $aOut[1] = '' Then
         ReDim $aOut[1]
         $aOut[0] = 0
     EndIf
     Return $aOut
Else
     Return StringTrimRight($strFiles, StringLen($sDelim))
EndIf
EndFunc
Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
If Not IsDeclared("strFiles") Then Global $strFiles = ''
If ($Dir = -1) Or ($Dir = 0) Then
     For $file In $Folder.Files
         If $Ext <> '*' Then
             If StringRight($file.Name, StringLen($Ext)) = $Ext Then $strFiles &= $file.Path & $Delim
         Else
             $strFiles &= $file.Path & $Delim
         EndIf
     Next
EndIf
For $Subfolder In $Folder.SubFolders
     $Folder.SubFolders
     If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '' & $Delim
     _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
Next
EndFunc
;==================================================================================================

basically i think that BugFix func can be edited so that it return needed data with no FileFindFirstFile but i don't think that this nice func shud be edited at all for something as this that can change its use drasticly, but if you have time you can always try it yourself.

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

  • Moderators

AHRIMANSEFID,

Is the file you want to rename the only one in that folder without an additional digit at the end? Or is there anything else which makes this filename unique with the folder? :huh:

If you can define this difference precisely then you could use my RecFileListToArray UDF to list all the files in the folder tree and then loop through them renaming those that meet this criterion. :)

M23

Edited by Melba23
Typo

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

Me Test Code but Not Working Run Send Me Error Line 727

.

try it like this(test it on small amount of folders first)to se do it return array correctly

#include $files = _FileGetFirstFiles(@DesktopDir&'\script',"*.txt")_ArrayDisplay($files,'before')For $x = 1 To $files[0]$split = StringSplit($files[$x], '.')If Not StringIsDigit(StringRight($split[$split[0]-1],1)) Then $split[$split[0]] = '0.'&$split[$split[0]] $files[$x] = _ArrayToString($split) EndIfNext_ArrayDisplay($files,'after')Func _FileGetFirstFiles($sDir,$searchString)Local $return[1],$search,$fileLocal $arr = _GetFilesFolder_Rekursiv($sDir,'*',1);~ _ArrayDisplay($arr,'tree_array')For $x = 1 To $arr[0] $search = FileFindFirstFile($arr[$x]&$searchString) If @error Then FileClose($search) Else While 1 $file = FileFindNextFile($search) If @error Then ExitLoop _ArrayAdd($return,$arr[$x]&$file) ExitLoop WEnd FileClose($search) EndIfNext$return[0] = UBound($return)-1If $return[0] Then Return $returnElse SetError(1)EndIfEndFunc;==================================================================================================; Function Name: _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]]); Description: Recursive listing of files and/or folders; Parameter(s): $sPath Basicpath of listing ('.' -current path, '..' -parent path); $sExt Extension for file selection '*' or -1 for all (Default); $iDir -1 Files+Folder(Default), 0 only Files, 1 only Folder; optional: $iRetType 0 for Array, 1 for String as Return; optional: $sDelim Delimiter for string return; 0 -@CRLF (Default) 1 -@CR 2 -@LF 3 -';' 4 -'|'; Return Value(s): Array (Default) or string with found pathes of files and/or folder; Array[0] includes count of found files/folder; Author(s): BugFix (bugfix@autoit.de);==================================================================================================Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')Global $oFSO = ObjCreate('Scripting.FileSystemObject')Global $strFiles = ''Switch $sDelim Case '1' $sDelim = @CR Case '2' $sDelim = @LF Case '3' $sDelim = ';' Case '4' $sDelim = '|' Case Else $sDelim = @CRLFEndSwitchIf ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0If $sExt = -1 Then $sExt = '*'If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1_ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)If $iRetType = 0 Then Local $aOut $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1) If $aOut[1] = '' Then ReDim $aOut[1] $aOut[0] = 0 EndIf Return $aOutElse Return StringTrimRight($strFiles, StringLen($sDelim))EndIfEndFuncFunc _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)If Not IsDeclared("strFiles") Then Global $strFiles = ''If ($Dir = -1) Or ($Dir = 0) Then For $file In $Folder.Files If $Ext <> '*' Then If StringRight($file.Name, StringLen($Ext)) = $Ext Then $strFiles &= $file.Path & $Delim Else $strFiles &= $file.Path & $Delim EndIf NextEndIfFor $Subfolder In $Folder.SubFolders $Folder.SubFolders If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)NextEndFunc;==================================================================================================
basically i think that BugFix func can be edited so that it return needed data with no FileFindFirstFile but i don't think that this nice func shud be edited at all for something as this that can change its use drasticly, but if you have time you can always try it yourself.

post-8907-0-50021800-1343476984_thumb.jp

Edited by AHRIMANSEFID
Link to comment
Share on other sites

I need an application source code to search folder by folder and find any picture with gif format, and after finding the files in this format rename the first one no matter what is the name of the first file rename it to 1.gif. any help would be great.

Link to comment
Share on other sites

Me Test Code but Not Working Run Send Me Error Line 727

cant remamber that i have 727 lines of code :P

newer the less i dont know how will 'Scripting.FileSystemObject' react on win7 especialy on x64, xp 32 bits is running smoothly.

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Let's put this in perspective. Do you know how to rename a file? In the help file it says you need to use FileMove because:

AutoIt lacks a "FileRename" function.

Acheiving this is your fist objective. Write that code and then proceed to the next stage, which is locating specific files you want to move (rename). Please understand that saying 'rename the first file in each directory' has no meaning. Do you mean the first file added to the directiory, the first alphabetical file name or the first file encountered?

I haven't tried bogQ's code BTW.

Link to comment
Share on other sites

  • Moderators

AHRIMANSEFID,

As I posted somewhere earlier, and czardas has also mentioned, please tell us how we can distinguish this "first file" from all the others in the folder. In your example it is the only one with digits at the end of the filename - is that always the case? Or do you just want the first file in an alphabetically sorted list? :huh:

Once we know how to do that the rest is easy - but until we know we cannot really help you. :)

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

  • Moderators

AHRIMANSEFID,

This script works on the folder/file structure you posted and lists the files you wanted to rename: :)

#include <RecFileListToArray.au3>
#include <Array.au3>

; List all files and folders - add a trailing  to get a  added to folders
$aList = _RecFileListToArray("N:Folder", "*.gif", 0, 1, 1, 2)

; Just for display
_ArrayDisplay($aList)

For $i = 1 To $aList[0]

    ; Ignore folders by checking for the trailing 
    If StringRight($aList[$i], 1) <> "" Then
        ; Now see if there is no digit before the .gif extension
        If Not StringRegExp($aList[$i], ".*d.gif") Then
            ; We found a file that matches
            ConsoleWrite($aList[$i] & @CRLF)
        EndIf
    EndIf
Next

All clear? :)

M23

Edit: You can find the RecFileListToArray UDF in my sig - just download the include file and put it in the same folder as your script. ;)

Edited by Melba23

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

  • Moderators

AHRIMANSEFID,

not work

That is not a lot of use. What does not work? What output do you get? Did you change the path to that you use? :huh:

Help us to help you. :)

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

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