Jump to content

Rename all files in a folder based on the contents of a text file


Go to solution Solved by ioa747,

Recommended Posts

Posted

In both cases we have two arrays
one from FileReadToArray (from list.txt) based on 0
and one from _FileListToArray (from audio files) based on 1 (0 has the count)
and we create a loop so that
index 1 from table _FileListToArray is renamed with the name from index 0 of table filereadtoarray
index 2 from table _FileListToArray is renamed with the name from index 1 of table filereadtoarray
index 3 from table _FileListToArray is renamed with the name from index 2 of table filereadtoarray
...
...

I know that I know nothing

Posted

Good day,

Well, it would appear that my MAJOR ISSUE was WAS NOT including the folders that the data was located in!!!

Sure, I was testing the filenames...but NOT the paths where those filenames were located!!! "Duh!!!" and "Duh!, Duh!"

So, without...

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sTextFolder = "I:\Live_Rig\Scripts\Development\_FileMove\HelpFile\Data\FileListing.txt"

    ; Read the text file
    Local $sTextFileToRead = FileReadToArray($sTextFolder)
    For $i = 0 To UBound($sTextFileToRead) - 1
        ConsoleWrite($sTextFileToRead[$i] & @CRLF)
    Next

    Local $sDataFolder = "I:\Live_Rig\Scripts\Development\_FileMove\HelpFile\Data\"
    ; Read the folder contents
    Local $aFileList = _FileListToArray($sDataFolder, "*.wma")
    For $i = 1 To $aFileList[0]
        ConsoleWrite($aFileList[$i] & @CRLF)
    Next
EndFunc   ;==>Example

...and, with...

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sTextFolder = "I:\Live_Rig\Scripts\Development\_FileMove\HelpFile\Data\FileListing.txt"

    ; Read the text file
    Local $sTextFileToRead = FileReadToArray($sTextFolder)
    For $i = 0 To UBound($sTextFileToRead) - 1
        ConsoleWrite($sTextFileToRead[$i] & @CRLF)
    Next

    Local $sDataFolder = "I:\Live_Rig\Scripts\Development\_FileMove\HelpFile\Data\"
    ; Read the folder contents
    Local $aFileList = _FileListToArray($sDataFolder, "*.wma")
    For $i = 1 To $aFileList[0]
        ConsoleWrite($sDataFolder & $aFileList[$i] & @CRLF)
    Next
EndFunc   ;==>Example

Thus, I do still have more testing to do...and will keep ya'all updated as to my progress [...or it is REGRESS?!?]

No. No. I’m listening. It just takes me a minute to process that much stupid all at once.” Sheldon (Big Bang Theory)

 

Posted

Good day,

Again, my many thanks to ioa747...whom has managed to "save my bacon" on numerous occasions!

As noted, here is my "code":

; Update the folder data
    For $i = 1 To $sNewName[0+1]
        ConsoleWrite($sNewName[$i] & @CRLF)
        ConsoleWrite($sOldName[$i] & @CRLF)
        FileMove($sOldName[$i], $sNewName[$i], $FC_OVERWRITE)
    Next

...and here is ioa747's offering:

; Update the folder data
    For $i = 0 to UBound($sNewName) - 1
      FileMove($sOldName[$i], $sNewName[$i], $FC_OVERWRITE)
      ConsoleWrite("FileMove:" & $sDataFolder & $sOldName[$i + 1] & ", " & $sDataFolder & $sNewName[$i] & ".wma" & @CRLF)
    Next

So:

; FileMove($sOldName[$i], $sNewName[$i], $FC_OVERWRITE)

; VERSUS

; FileMove($sDataFolder & $sOldName[$i + 1], $sDataFolder & $sNewName[$i] & ".wma", $FC_OVERWRITE)

...an OBVIOUS difference!!

Thanks again...ioa747! Much appreciated!

Posted (edited)

don't forget the
For $i = 1 To $sNewName[0+1]
because $sNewName[0+1] = Celeste

Edited by ioa747

I know that I know nothing

Posted (edited)

ioa747,

My brain...whatever is left of it, is a bit OVERWHELMED at the moment. I really would prefer NOT having to learn anything else NEW!

What is wrong\not wrong with this?

; -----------------------------------------------
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
; -----------------------------------------------
_Example()
; -----------------------------------------------
Func _Example()
    Local $file_to_read = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\FileListing.txt"
    Local $aArray1 = FileReadToArray($file_to_read)
    ; -----------------------------------------------
    $file_to_write = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\Audio\"
    Local $iLineCount = @extended
    Local $aArray2 = _FileListToArray($file_to_write, "*.*", $FLTA_FILES)
    ; -----------------------------------------------
    For $i = 0 To $iLineCount - 1
        FileMove($file_to_write & $aArray2[$i + 1], $file_to_write & $aArray1[$i] & ".wma", $FC_OVERWRITE)
    Next
EndFunc   ;==>_Example
; -----------------------------------------------

Have a peep...

_UpdateSrcAudio_a.au3 _UpdateSrcAudio_b.au3

Edited by mr-es335
Posted (edited)

a good tool when working with arrays is _ArrayDisplay($aArray1)
to visualize things,  believe me , it helps

; -----------------------------------------------
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
; -----------------------------------------------
_Example()
; -----------------------------------------------
Func _Example()
    Local $file_to_read = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\FileListing.txt"
    Local $aArray1 = FileReadToArray($file_to_read)
    Local $iLineCount = @extended
;~  _ArrayDisplay($aArray1)
    ; -----------------------------------------------
    $file_to_write = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\Audio\"
    Local $aArray2 = _FileListToArray($file_to_write, "*.*", $FLTA_FILES)
;~  _ArrayDisplay($aArray2)
    ; -----------------------------------------------
    For $i = 0 To $iLineCount - 1
;~      FileMove($file_to_write & $aArray2[$i + 1], $file_to_write & $aArray1[$i] & ".wma", $FC_OVERWRITE)
        ConsoleWrite("FileMove:" & $file_to_write & $aArray2[$i + 1] & ", " & $file_to_write & $aArray1[$i] & ".wma" & @CRLF)
    Next
EndFunc   ;==>_Example
; -----------------------------------------------

 

as well as the correct nomenclature

; -----------------------------------------------
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
; -----------------------------------------------
_Example()
; -----------------------------------------------
Func _Example()
    Local $file_to_read = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\FileListing.txt"
    Local $aList = FileReadToArray($file_to_read)
    Local $iLineCount = @extended
;~  _ArrayDisplay($aList)
    ; -----------------------------------------------
    $Audio_folder = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\Audio\"
    Local $aFile = _FileListToArray($Audio_folder, "*.*", $FLTA_FILES)
;~  _ArrayDisplay($aFile)
    ; -----------------------------------------------
    For $i = 0 To $iLineCount - 1
;~      FileMove($Audio_folder & $aFile[$i + 1], $Audio_folder & $aList[$i] & ".wma", $FC_OVERWRITE)
        ConsoleWrite("FileMove:" & $Audio_folder & $aFile[$i + 1] & ", " & $Audio_folder & $aList[$i] & ".wma" & @CRLF)
    Next
EndFunc   ;==>_Example
; -----------------------------------------------

 

Edited by ioa747

I know that I know nothing

Posted

Hello,

The method for accessing array data  - is one more preferable over another?

For example, in the two script examples  noted above, is "For $i = 1 To $sNewName[0+1]" preferable over "For $i = 0 to UBound($sNewName) - 1"?

Any clarification in this matter would be greatly appreciated!

Posted (edited)

You will tell me.

; -----------------------------------------------
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
; -----------------------------------------------
_Example()
; -----------------------------------------------
Func _Example()
    Local $file_to_read = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\FileListing.txt"
    Local $aList = FileReadToArray($file_to_read)
;~  Local $iLineCount = @extended
;~  _ArrayDisplay($aList)
    ; -----------------------------------------------
    $Audio_folder = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\Audio\"
    Local $aFile = _FileListToArray($Audio_folder, "*.*", $FLTA_FILES)
;~  _ArrayDisplay($aFile)
    ; -----------------------------------------------
    For $i = 1 To $aFile[0]
;~      FileMove($Audio_folder & $aFile[$i], $Audio_folder & $aList[$i - 1] & ".wma", $FC_OVERWRITE)
        ConsoleWrite("FileMove:" & $Audio_folder & $aFile[$i] & ", " & $Audio_folder & $aList[$i - 1] & ".wma" & @CRLF)
    Next
EndFunc   ;==>_Example
; -----------------------------------------------

 

Edited by ioa747

I know that I know nothing

Posted

ioa747,

As you noted: "You will tell me."

I see no difference between the two script examples  namely, "For $i = 0 To $sNewName[0+1]" preferable over "For $i = 0 to UBound($sNewName) - 1"?

Thanks for the clarification! Appreciated!

Posted (edited)

here is an alternative

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
; -----------------------------------------------
_Example()
; -----------------------------------------------
Func _Example()
    Local $file_to_read = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\FileListing.txt"
    Local $aList
    _FileReadToArray($file_to_read, $aList)
;~  _ArrayDisplay($aList)
    ; -----------------------------------------------
    Local $Audio_folder = "I:\Live_Rig\Scripts\Development\_RenameAudioData\ioa747\Audio\"
    Local $aFile = _FileListToArray($Audio_folder, "*.*", $FLTA_FILES)
;~  _ArrayDisplay($aFile)
    ; -----------------------------------------------
    For $i = 1 To $aFile[0]
;~      FileMove($Audio_folder & $aFile[$i], $Audio_folder & $aList[$i] & ".wma", $FC_OVERWRITE)
        ConsoleWrite("FileMove:" & $Audio_folder & $aFile[$i] & ", " & $Audio_folder & $aList[$i] & ".wma" & @CRLF)
    Next
EndFunc   ;==>_Example
; -----------------------------------------------

 

Edited by ioa747

I know that I know nothing

Posted
6 hours ago, mr-es335 said:

Any clarification in this matter would be greatly appreciated!

You are probably confused by the difference between 0-based and 1-based arrays. @TheDcoder has created a helpful tutorial on arrays. This contains, for instance, an explanation of this particular topic.

Excerpt :

UBound returns the no. of elements in an array with the index starting from 1! That's right, you need to remove 1 from the total no. of elements in order to process the array because the index of an array starts with 0! So append a simple - 1 to the statment:

 

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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
  • Recently Browsing   0 members

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