Mauro Posted October 27, 2016 Share Posted October 27, 2016 Hi everyone, I like to create a script that recursively goes through a chose folder and sub folders and renames there folders with the following rules: -all folder names need to be replaced with count files "Y" and "Z". I believe its possible with _FileListToArrayRec and _FileListToArray functions, but how? e.g. whit this directory tree: root ├── folder │ ├── folder │ │ ├─ file │ │ ├─ file │ │ └─ file │ ├── folder │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ └── folder ├── folder │ ├── folder │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ ├── folder │ │ ├─ file │ │ ├─ file │ └── folder │ ├── folder │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ ├── folder │ └── folder ├── folder │ └─ file └── folder ├─ file ├─ file └─ file would produce this output: root ├── folder(3) │ ├── folder(3) │ │ ├─ file │ │ ├─ file │ │ └─ file │ ├── folder(4) │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ └── folder(0) ├── folder(3) │ ├── folder(4) │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ ├─ file │ ├── folder(2) │ │ ├─ file │ │ ├─ file │ └── folder(3) │ ├── folder(4) │ │ ├─ file │ │ ├─ file │ │ ├─ file │ │ └─ file │ ├── folder(0) │ └── folder(0) ├── folder(1) │ └─ file └── folder(3) ├─ file ├─ file └─ file Sorry for my bad english. Thanks Mauro Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 Sorry, I wrote for error "Y" and "Z" Link to comment Share on other sites More sharing options...
232showtime Posted October 27, 2016 Share Posted October 27, 2016 its possible but first show us what you did... ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon. Link to comment Share on other sites More sharing options...
l3ill Posted October 27, 2016 Share Posted October 27, 2016 Yeah lets see some code... Stick to _FileListToArrayRec and pay close attention to the parameter settings (folders only for instance) I don't understand your folder hierarchy, you have 2 folders named "folder(3)" in the root folder for instance, I don't see any system. And what is the y and z? was that the mistake you mentioned? Should be numbers not letters? My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 23 minutes ago, Mauro said: Here is my code. The action of the script stops at the top level folder #include <File.au3> #include <Array.au3> $sPercorso = @ScriptDir & "\UNEPtest\" $Cartella = _FileListToArrayRec($sPercorso, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_RELPATH) ; 1° parametro = Il percorso della directory iniziale. In questo caso: @ScriptDir & "\UNEPtest\" ; 2° parametro = Il filtro per i tipi di file da cercare. In questo caso: "*" (tutto) ; 3° parametro = Specifica se voglio: 0 = files e cartelle (default); 1 = files; 2 = cartelle. In questo caso: 2 = cartelle ; 4° parametro = Specifica se cercare ricorsivamente nelle subcartelle e a quale livello. ; 0 = Non cerca nelle subcartelle (default); 1 = Cerca in tutte le subcar- ; telle (ricorsione illimitata). In questo caso: 1 = ricorsione illimitata ; 5° parametro = Ordina i risultati alfabeticamente e per la profondità. 0 = Non ordinare ; (default); 1 = Ordinare; 2 = Ordinare velocemente. In questo caso: 0 = non ordinare ; 6° parametro = Specifica se visualizzare il percorso dei risultati. 0 = Solo nome; 1 = ; Percorso relativo al percorso iniziare; 2 = Percorso completo. In questo caso: 0 = visualizza solo nome If @error = 1 Then MsgBox(0, "Errore $Cartella", "Path not found or invalid") Exit ElseIf @error = 9 Then MsgBox(0, "Errore $Cartella", "No files/folders found") Exit EndIf ;~ _ArrayDisplay($Cartella, "Cartelle"); <===== DEBUG For $i = 1 To $Cartella[0] $Files = _FileListToArray($sPercorso & "\" & $Cartella[$i], "*.*", $FLTA_FILES, TRUE) ;~ _ArrayDisplay($Files, "Files in " & $Cartella[$i]); <===== DEBUG ;~ $Cartella[1] = _RimuoviStringaDa($Cartella[1], "(") ConsoleWrite( "TEST: " & $Cartella[1] & " contiene " & $Files[0] & " files e avrà come NUOVO NOME " & $Cartella[1] &"(" & $Files[0] & ")" & @CRLF) ;~ DirMove($Cartella[1], $Cartella[1] &"(" & $Files[0] & ")") Next ; #FUNCTION# ================================================================== ; Nome: _RimuoviStringaDa ; Sintassi: _RimuoviStringaDa($sStringa, $sDa, $iCaseSense, $iOccorrenza) ; Parametri: ; $sStringa = Stringa che contiene il testo da rimuovere ; $sDa = Il carattere dal quale inizia il testo da rimuovere ; $iCaseSense = [opzionale] Indica se deve rispettare maiuscole ; e minuscole ; 0 = Non case sensitive ; 1 = Case sensitive ; $iOccorrenza = [opzionale] Indica quale occorrenza deve trovare ; nella stringa del testo da rimuovere ; Intero negativo = cerca dal lato destro ; 1 = trova la prima occorrenza (default) ; Valore Rit. Ritorna la stringa originaria senza la substringa rimossa ; Vers. AutoIt: 3.3.14.2 ; Autore: Mauro Schirru ; Data: 03.10.2016 ; Descrizione: Rimuove substringa da stringa dopo un dato carattere ; Commenti: ; ============================================================================= Func _RimuoviStringaDa($sStringa, $sDa, $iCaseSense=0, $iOccorrenza=1) Local $iPos = StringInStr($sStringa, $sDa, $iCaseSense, $iOccorrenza) If $iPos = 0 Then Return $sStringa Return StringLeft($sStringa, $iPos - 1) EndFunc ;==>_RimuoviStringaDa Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 8 minutes ago, Mauro said: ; #FUNCTION# ================================================================== ; Nome: _RimuoviStringaDa ; Sintassi: _RimuoviStringaDa($sStringa, $sDa, $iCaseSense, $iOccorrenza) ; Parametri: ; $sStringa = Stringa che contiene il testo da rimuovere ; $sDa = Il carattere dal quale inizia il testo da rimuovere ; $iCaseSense = [opzionale] Indica se deve rispettare maiuscole ; e minuscole ; 0 = Non case sensitive ; 1 = Case sensitive ; $iOccorrenza = [opzionale] Indica quale occorrenza deve trovare ; nella stringa del testo da rimuovere ; Intero negativo = cerca dal lato destro ; 1 = trova la prima occorrenza (default) ; Valore Rit. Ritorna la stringa originaria senza la substringa rimossa ; Vers. AutoIt: 3.3.14.2 ; Autore: Mauro Schirru ; Data: 03.10.2016 ; Descrizione: Rimuove substringa da stringa dopo un dato carattere ; Commenti: ; ============================================================================= Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 I3ll, In the name of the folder, the number in parentheses indicates the total content files Link to comment Share on other sites More sharing options...
l3ill Posted October 27, 2016 Share Posted October 27, 2016 Ahh that makes sense now... Please put your code in a code box when posting (<> button) next to the smiley. Try this DirMove($Cartella[$i], $Cartella[$i] &"(" & $Files[0] & ")") My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
l3ill Posted October 27, 2016 Share Posted October 27, 2016 BTW - I cant test this and it may still need some work but it looks like that is where the problem is. good luck! Bill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted October 27, 2016 Share Posted October 27, 2016 I have renamed folders before, the trick is to ensure your setup your array to read from the deepest directories first. Because if your have say 3 sets of folders: C:\Documents\May\Drafts The first action is to rename Documents, the moment you get to rename May it is no longer a valid path. Also another hint, Autoit does not have a true "rename" feature for files/folders. I felt more comfortable using the REN Command rather than FileMove() it felt like it was faster to me. Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 Bill, I apologize if I did not use the code box. The script does not work even with your edit. Thanks anyway. ViciousXUSSMC, what you suggest is very correct, but I can not put it into practice. Thank you Link to comment Share on other sites More sharing options...
Mauro Posted October 27, 2016 Author Share Posted October 27, 2016 12 hours ago, Mauro said: Spoiler #include <File.au3> #include <Array.au3> $sPercorso = @ScriptDir & "\UNEPtest\" $Cartella = _FileListToArrayRec($sPercorso, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_RELPATH) ; 1° parametro = Il percorso della directory iniziale. In questo caso: @ScriptDir & "\UNEPtest\" ; 2° parametro = Il filtro per i tipi di file da cercare. In questo caso: "*" (tutto) ; 3° parametro = Specifica se voglio: 0 = files e cartelle (default); 1 = files; 2 = cartelle. In questo caso: 2 = cartelle ; 4° parametro = Specifica se cercare ricorsivamente nelle subcartelle e a quale livello. ; 0 = Non cerca nelle subcartelle (default); 1 = Cerca in tutte le subcar- ; telle (ricorsione illimitata). In questo caso: 1 = ricorsione illimitata ; 5° parametro = Ordina i risultati alfabeticamente e per la profondità. 0 = Non ordinare ; (default); 1 = Ordinare; 2 = Ordinare velocemente. In questo caso: 0 = non ordinare ; 6° parametro = Specifica se visualizzare il percorso dei risultati. 0 = Solo nome; 1 = ; Percorso relativo al percorso iniziare; 2 = Percorso completo. In questo caso: 0 = visualizza solo nome If @error = 1 Then MsgBox(0, "Errore $Cartella", "Path not found or invalid") Exit ElseIf @error = 9 Then MsgBox(0, "Errore $Cartella", "No files/folders found") Exit EndIf ;~ _ArrayDisplay($Cartella, "Cartelle"); <===== DEBUG For $i = 1 To $Cartella[0] $Files = _FileListToArray($sPercorso & "\" & $Cartella[$i], "*.*", $FLTA_FILES, TRUE) ;~ _ArrayDisplay($Files, "Files in " & $Cartella[$i]); <===== DEBUG ;~ $Cartella[1] = _RimuoviStringaDa($Cartella[1], "(") ConsoleWrite( "TEST: " & $Cartella[1] & " contiene " & $Files[0] & " files e avrà come NUOVO NOME " & $Cartella[1] &"(" & $Files[0] & ")" & @CRLF) ;~ DirMove($Cartella[1], $Cartella[1] &"(" & $Files[0] & ")") Next Func _RimuoviStringaDa($sStringa, $sDa, $iCaseSense=0, $iOccorrenza=1) Local $iPos = StringInStr($sStringa, $sDa, $iCaseSense, $iOccorrenza) If $iPos = 0 Then Return $sStringa Return StringLeft($sStringa, $iPos - 1) EndFunc ;==>_RimuoviStringaDa Link to comment Share on other sites More sharing options...
Mauro Posted October 30, 2016 Author Share Posted October 30, 2016 I found the solution with Array Reverse function. I thank you all. 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