Jump to content



Photo

Plz Help For renaming Files


  • Please log in to reply
22 replies to this topic

#1 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 25 July 2012 - 11:27 AM

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





#2 bogQ

bogQ

    Nusquam est verus, panton est licitus.

  • Active Members
  • PipPipPipPipPipPip
  • 1,648 posts

Posted 25 July 2012 - 11:44 AM

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 connectionAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)let me google that 4 y or y can try Forum Search Button and this part of code i love, try it, its easy and it can help you alot :)
ShellExecute("AutoIt.chm", "", StringReplace(@AutoItExe,"autoit3.exe",""))
If i ever did help you or anyone brake TOS TOU or EULA or anything like that in any way in any time i promise that il try that i never ever do it again in the future :)

#3 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 26 July 2012 - 05:31 AM

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.

#4 JLogan3o13

JLogan3o13

    Down to 98

  • MVPs
  • 2,103 posts

Posted 26 July 2012 - 12:42 PM

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, 26 July 2012 - 12:42 PM.

J.I spent 10 minutes reviewing code and thinking "What kind of drugs is this guy on?" before realizing it was something I wrote.My Scripts:Include Source with Compiled Script, Disk Maintenance for Windows XP, "Deal-A-Day" Sites, SCCM 2007 Front End, Windows Firewall UDF

#5 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 28 July 2012 - 05:47 AM

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.

Plain Text         
#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, 28 July 2012 - 07:56 AM.


#6 bogQ

bogQ

    Nusquam est verus, panton est licitus.

  • Active Members
  • PipPipPipPipPipPip
  • 1,648 posts

Posted 28 July 2012 - 08:48 AM

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


Plain Text         
#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, 28 July 2012 - 09:09 AM.

TCP server and client - Learning about TCP servers and clients connectionAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)let me google that 4 y or y can try Forum Search Button and this part of code i love, try it, its easy and it can help you alot :)
ShellExecute("AutoIt.chm", "", StringReplace(@AutoItExe,"autoit3.exe",""))
If i ever did help you or anyone brake TOS TOU or EULA or anything like that in any way in any time i promise that il try that i never ever do it again in the future :)

#7 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,374 posts

Posted 28 July 2012 - 09:42 AM

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, 28 July 2012 - 10:04 AM.
Typo

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

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#8 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 28 July 2012 - 12:01 PM

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.

Attached Thumbnails

  • a.JPG

Edited by AHRIMANSEFID, 28 July 2012 - 12:03 PM.


#9 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 28 July 2012 - 12:41 PM

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.

#10 czardas

czardas

  • Active Members
  • PipPipPipPipPipPip
  • 5,074 posts

Posted 28 July 2012 - 01:04 PM

Have you looked at _FileListToArray, or looked at Melba's UDF?

Edited by czardas, 28 July 2012 - 01:05 PM.


#11 bogQ

bogQ

    Nusquam est verus, panton est licitus.

  • Active Members
  • PipPipPipPipPipPip
  • 1,648 posts

Posted 28 July 2012 - 05:03 PM

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 connectionAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)let me google that 4 y or y can try Forum Search Button and this part of code i love, try it, its easy and it can help you alot :)
ShellExecute("AutoIt.chm", "", StringReplace(@AutoItExe,"autoit3.exe",""))
If i ever did help you or anyone brake TOS TOU or EULA or anything like that in any way in any time i promise that il try that i never ever do it again in the future :)

#12 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 29 July 2012 - 06:37 AM

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.

#13 czardas

czardas

  • Active Members
  • PipPipPipPipPipPip
  • 5,074 posts

Posted 29 July 2012 - 07:10 AM

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.

#14 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 29 July 2012 - 09:44 AM

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.

#15 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,374 posts

Posted 29 July 2012 - 09:58 AM

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

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#16 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 29 July 2012 - 12:32 PM

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.

#17 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,374 posts

Posted 29 July 2012 - 12:49 PM

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, 29 July 2012 - 05:50 PM.

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

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#18 AHRIMANSEFID

AHRIMANSEFID

    Wayfarer

  • Active Members
  • Pip
  • 63 posts

Posted 30 July 2012 - 10:28 AM

not work test.

#19 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,374 posts

Posted 30 July 2012 - 10:32 AM

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

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#20 czardas

czardas

  • Active Members
  • PipPipPipPipPipPip
  • 5,074 posts

Posted 30 July 2012 - 10:51 AM

not work test.


You know you also need code from here. Look at Melba's signature. Place the file RecFileListToArray.au3 in the same folder as the script and try again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users