Modify this script to define folder path

Hi, I found this script, that works very well, from several years back on this forum to create a text file that lists the contents of a folder and sub folders. I would like to modify the first 2 lines so that I DON’T have a prompt to choose the folder. I want to set it to Volumes/files/movies. Also, I want to have the text file go to the same volume instead of the desktop, Volumes/files . I’ve made several searches and changes but only get errors. Thanks in advance for any help !!

property file_id : null

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to (path to desktop as string) & "Folder Inventory.txt"
set file_id to open for access file log_file_path with write permission
write ("Start " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "")
write (return & return) to file_id starting at eof
close access file_id
say "finished"
return

on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		log name_prefix & (name of this_folder) as string
		write (return & name_prefix & (name of this_folder) as string) to file_id starting at eof
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & "      " & (name of this_file) as string
			write (return & name_prefix & "      " & (name of this_file) as string) to file_id starting at eof
		end repeat
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, "  " & name_prefix)
		end repeat
	end tell
end do_subfolders

Hi,

since the recursion handler works with Finder specifiers anyway I’d recommend this

tell application "Finder" to set this_folder to folder "Movies" of disk "files"
set log_file_path to "files:Folder Inventory.txt"

And as always when using the File Read/Write commands, put everything that happens while the file’s open inside a ‘try’ statement to ensure that the file gets closed again even if something goes wrong:

property file_id : null

tell application "Finder" to set this_folder to folder "Movies" of disk "files"
set log_file_path to "files:Folder Inventory.txt"
set file_id to open for access file log_file_path with write permission
try
	write ("Start " & (current date) & return) to file_id starting at eof
	my do_subfolders(this_folder, "")
	write (return & return) to file_id starting at eof
	close access file_id
	say "finished"
on error errMsg
	close access file_id
	say errMsg
end try

Thanks for those answers. Below is the working combined code.

I realized from testing the code that I need to completely overwrite the contents of the file so that only the current contents of the folder is shown in the file. I have removed the EOF parts of the code but that does not remove the previous list of contents that are no longer in the folder. What would the code be to do this?

Thanks.

property file_id : null

tell application "Finder" to set this_folder to folder "movies" of disk "files"
set log_file_path to "files:Folder Inventory.txt"
set file_id to open for access file log_file_path with write permission
try
	write ("Start " & (current date) & return) to file_id
	my do_subfolders(this_folder, "")
	write (return & return) to file_id
	close access file_id
	say "finished"
on error errMsg
	close access file_id
	say errMsg
end try
on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		log name_prefix & (name of this_folder) as string
		write (return & name_prefix & (name of this_folder) as string) to file_id
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & "      " & (name of this_file) as string
			write (return & name_prefix & "      " & (name of this_file) as string) to file_id
		end repeat
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, "  " & name_prefix)
		end repeat
	end tell
end do_subfolders