Need help with bulk renaming, based on a CSV file Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Seabottom
Posts: 25
Joined: 30 Nov 2013, 18:59

Need help with bulk renaming, based on a CSV file

Post by Seabottom » 01 Dec 2023, 14:50

Hello!
I'm having some issues getting parsing to work first and foremost. The CSV file I'm reading from has 6 columns in it.
This is the code, and it gives me an error on line 3 "This variable appears to never be assigned a value", which I'm confused about

Code: Select all

AudioClips := FileRead("O:\Audio.csv")
AudioClips := StrSplit(string,";",,)
AudioClips.Get(1 , Default)
msgbox AudioClips
I'm just trying to make sure that AHK is parsing and indexing the CSV file properly before continuing.
I don't mind to fiddle a bit myself, but this is proving rather tricky. I've looked in the help file documentation and found no answer.

Ultimately, I want a script that renames .wav files in a folder according to these requirements:
-Only rename if column 2 != ""
-Capital first letter of the word in column 6
-The entire string of column 3
-The entire string in column 2

Example - The following is from the CSV file -

Code: Select all

ispyseb;Mission sir?;Spy;Select;;Allied;
After renaming, the file "ispyseb.wav" will be "ASpy_Mission sir?.wav"

User avatar
mikeyww
Posts: 27213
Joined: 09 Sep 2014, 18:38

Re: Need help with bulk renaming, based on a CSV file

Post by mikeyww » 01 Dec 2023, 15:10

Default is meant to be replaced by a default value. It is otherwise interpreted to be a variable name. All variables must be defined before they are accessed. If you do not need a default, then you can access the array element more directly.

Code: Select all

#Requires AutoHotkey v2.0
AudioClips := [1,, 3]
MsgBox AudioClips.Get(1, 4) '`n'
     . AudioClips.Get(2, 4) '`n'
     . AudioClips[3]
"String" is the name of an AHK function, so I recommend avoiding it unless you intend to call that function.

User avatar
Seabottom
Posts: 25
Joined: 30 Nov 2013, 18:59

Re: Need help with bulk renaming, based on a CSV file

Post by Seabottom » 03 Dec 2023, 11:15

Thanks, I've found other ways to do the same thing to make it all easier, I guess.
I need to combine these 3 separate pieces of code, but the first two contains A_Index and I don't know how to call to that?

Code: Select all

#Requires AutoHotkey v2.0

Loop read, "O:\Audio.csv"
{
    LineNumber := A_Index
    Loop parse, A_LoopReadLine, "CSV"
    {
        Result := MsgBox("Row " LineNumber ", Column " A_Index "`n" A_LoopField "`n`nContinue?",, "y/n")
        if Result = "No"
            break
    }
}

Loop Files, "O:\RA2\*.wav"
FileList .= A_LoopFileName "`n"
Loop Parse, FileList, "`n"
{
    if A_LoopField = ""
        continue
    Result := MsgBox("File number " A_Index " is " A_LoopField ".  Continue?",, "y/n")
    if Result = "No"
        break
}

Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "Fox"
If InStr(Haystack, Needle)
    MsgBox "The string was found."
Else
    MsgBox "The string was not found."
I need to use the needle/haystack code to find the row in the CSV file of the filename. It's basically a 1:1, but for coding purposes I think a for each loop is preferred? I don't know how to do that though.

User avatar
mikeyww
Posts: 27213
Joined: 09 Sep 2014, 18:38

Re: Need help with bulk renaming, based on a CSV file  Topic is solved

Post by mikeyww » 03 Dec 2023, 11:18

For the original request:

Code: Select all

#Requires AutoHotkey v2.0
csv      := A_ScriptDir '\test.csv'
audioDir := A_ScriptDir '\audio'
ext      := 'wav'
If !FileExist(csv) {
 MsgBox 'File not found.`n`n' csv, 'Error', 'Icon!'
 Return
}
Loop Read csv {
 col := StrSplit(RegExReplace(A_LoopReadLine, '[*"/<>|?:\\]'), ';')
 If FileExist(source := audioDir '\' col[1] '.' ext) {
  If col[2] != ''
   If FileExist(dest := Format('{}\{:U}{}_{}.' ext, audioDir, SubStr(col[6], 1, 1), col[3], col[2]))
    MsgBox 'Destination file already exists.`n`n' dest, 'Error', 'Icon!'
   Else FileMove source, dest
 } Else MsgBox 'Source file not found.`n`n' source, 'Error', 'Icon!'
}
MsgBox 'Done!', 'Status', 'Iconi'

User avatar
Seabottom
Posts: 25
Joined: 30 Nov 2013, 18:59

Re: Need help with bulk renaming, based on a CSV file

Post by Seabottom » 06 Dec 2023, 16:01

mikeyww wrote:
03 Dec 2023, 11:18
For the original request:
-code
This is amazing, it does exactly everything, and more than I could think of - Error messages that actually were helpful for me, and removing characters that can't be put into a windows file name. I never thought of that and it has proved super useful.
There is nothing I had to adjust with the code, it just works. The only issues I had was with my CSV file, but that was my problem to fix.
Thank you heaps

Post Reply

Return to “Ask for Help (v2)”