############################################################ # Script for automatically changing names for all sound files in a folder # New names are taken from a text file # In the text file, each line is the name of one sound # The sound files will be named in the order in which they are loaded # into praat. # The script can for example be used if the names of your sound files are # just running numbers and you want to add additional information, # e.g. coding of experimental variables, to the names. ## # Written for the experiment on Finnish syllable structure & prosody # Anja Arnhold, November 2010 ## # version 2: # - The old file name (contains speaker and running number) is appended # as a suffix (optional) form Input information comment In which directory are the sound files? sentence Sounds_directory C:\Dokumente und Einstellungen\Anja\Eigene Dateien\Promotion\SecondExperiment-syllStruc\recordings\s01\ comment Do you want to rename all or only some? sentence File_name_or_initial_substring sentence File_name_includes sentence File_extension .wav comment Give location and name of the text file listing the new sound file names comment (remember the file extension, normally ".txt") sentence Textfilepath C:\Dokumente und Einstellungen\Anja\Eigene Dateien\Promotion\SecondExperiment-syllStruc\recordings\s01\ sentence Textfilename codes-s01.txt comment Where should the renamed the sound files be saved? sentence New_directory C:\Dokumente und Einstellungen\Anja\Eigene Dateien\Promotion\SecondExperiment-syllStruc\recordings\renamedSoundfiles\ comment Append old file name as suffix? boolean Suffix 1 endform ### # Read sound files as file list, get number of sound files Create Strings as file list... list 'sounds_directory$'/'file_name_or_initial_substring$'*'file_name_includes$'*'file_extension$' fileslist = selected("Strings") file_count = Get number of strings # Read file containing new names for sound files textfilename$ = "'textfilepath$'/'textfilename$'" textfile$ < 'textfilename$' if textfile$ = "" exit The text file is empty. endif # Check compatibility of the text file with the number of sound files textfilelength = length (textfile$) #if textfilelength <> file_count # exit The text file does not contain the right number of lines ('textfilelength' lines for 'file_count' sound files). #endif # Initially define variable remainingText$ as the whole text file remainingText$ = textfile$ ### # Loop through files in the folder, open and save them with a different name # Note remainingText was a variable starting with capital letter before, for current_file from 1 to file_count select Strings list filename$ = Get string... current_file Read from file... 'sounds_directory$'/'filename$' name_only$ = filename$ - ".wav" # Get new name of the sound file # (=first new line in the text file with sound file names) firstnewline = index (remainingText$, newline$) newsoundname$ = left$ (remainingText$, (firstnewline - 1)) pause old 'name_only$', new 'newsoundname$' # Save sound under the new name and remove it from the list afterwards select Sound 'name_only$' if suffix = 0 Write to WAV file... 'new_directory$'/'newsoundname$'.wav else Write to WAV file... 'new_directory$'/'newsoundname$'-'name_only$'.wav endif select Sound 'name_only$' Remove # Get new remaining text from text file containing new sound file names # (minus the line that was just used) remainingTextlength = length (remainingText$) remainingText$ = right$ (remainingText$, (remainingTextlength - firstnewline)) endfor select 'fileslist' Remove