AHRIMANSEFID Posted July 25, 2012 Share Posted July 25, 2012 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 More sharing options...
bogQ Posted July 25, 2012 Share Posted July 25, 2012 some ideasIf _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 withStringRight($split[1],1)to cee if it have number withIf StringIsDigit(StringRight($split[1],1)) Thenso that you can locate correct file for renameing. TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) 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 More sharing options...
AHRIMANSEFID Posted July 26, 2012 Author Share Posted July 26, 2012 I have about 1000 foldrs that there are some pictures in this folders and iwant to rename first picture of every folder to this name 1.gif,I dont understand your opinion. can you explain more about it? thanks. Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted July 26, 2012 Moderators Share Posted July 26, 2012 (edited) 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 July 26, 2012 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 More sharing options...
AHRIMANSEFID Posted July 28, 2012 Author Share Posted July 28, 2012 (edited) 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 July 28, 2012 by AHRIMANSEFID Link to comment Share on other sites More sharing options...
bogQ Posted July 28, 2012 Share Posted July 28, 2012 (edited) try it like this (test it on small amount of folders first) to se do it return array correctly expandcollapse popup#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 July 28, 2012 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) 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 More sharing options...
Moderators Melba23 Posted July 28, 2012 Moderators Share Posted July 28, 2012 (edited) 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? 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 July 28, 2012 by Melba23 Typo 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AHRIMANSEFID Posted July 28, 2012 Author Share Posted July 28, 2012 (edited) 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. Edited July 28, 2012 by AHRIMANSEFID Link to comment Share on other sites More sharing options...
AHRIMANSEFID Posted July 28, 2012 Author Share Posted July 28, 2012 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 More sharing options...
czardas Posted July 28, 2012 Share Posted July 28, 2012 (edited) Have you looked at _FileListToArray, or looked at Melba's UDF? Edited July 28, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
bogQ Posted July 28, 2012 Share Posted July 28, 2012 Me Test Code but Not Working Run Send Me Error Line 727cant remamber that i have 727 lines of code 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 connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) 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 More sharing options...
AHRIMANSEFID Posted July 29, 2012 Author Share Posted July 29, 2012 I read & test your code many times but it does n't work,can you write a code for me that i can use it and rename my file thanks a lot. Link to comment Share on other sites More sharing options...
czardas Posted July 29, 2012 Share Posted July 29, 2012 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. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
AHRIMANSEFID Posted July 29, 2012 Author Share Posted July 29, 2012 can you write me a sample code for me? I am really tired from trying and getting no answer. also all the files name started with the letter i and they sorted in alphabetic mode. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 29, 2012 Moderators Share Posted July 29, 2012 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? Once we know how to do that the rest is easy - but until we know we cannot really help you. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AHRIMANSEFID Posted July 29, 2012 Author Share Posted July 29, 2012 In your example it is the only one with digits at the end of the filename - is that always the case?Yes it's always same and it's in .gif format. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 29, 2012 Moderators Share Posted July 29, 2012 (edited) 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 NextAll clear? M23Edit: 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 July 29, 2012 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AHRIMANSEFID Posted July 30, 2012 Author Share Posted July 30, 2012 not work test. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 30, 2012 Moderators Share Posted July 30, 2012 AHRIMANSEFID,not workThat is not a lot of use. What does not work? What output do you get? Did you change the path to that you use? Help us to help you. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted July 30, 2012 Share Posted July 30, 2012 not work test.You know you also need code from Look at Melba's signature. Place the file RecFileListToArray.au3 in the same folder as the script and try again. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now