Create a file/folder structure from a TXT list


The below script was written by Mr. StefanK from this post:

It is the way to what I am trying to achieve.
How to implement to the code to create files and folders keeping the structure?

display dialog " --Make Lotsa' Folders--
This AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
set textFile to (choose file with prompt "Select the text file you wish to use")
set folderNames to paragraphs of (read textFile)
repeat with oneName in folderNames
   tell application "Finder" to make new folder at destination with properties {name:oneName}
end repeat
display dialog "Your folders are done." buttons {"OK"}

My situation:
I have a plain txt file containing names of 100s of files with path in various sub-directories.
I need to create another directory copying the files in the list, while maintaining their directory structure in the destination.

This is my txt file:

and this would be my directory structure desired based on the above txt file:

Sorry for the long post.
highly appreciated your help. Thank you.

Model: mac mini 2012
AppleScript: 2.11 (208)
Browser: Safari 537.36
Operating System: macOS 10.15

I wrote much more scripts to create a folder structure. Please have a look

But you don’t need to create the folders explicitly. The shell command ditto is able to copy files to a destination base folder and to create the intermediate directories on the fly.

Here are some examples

thank you.
I will see it.

belinha. I’ve included my suggestion below. Please note:

  • The script prompts for the folder that contains the existing “clothes” folder, the destination folder, and the text file. To avoid these, actual paths could be put in the script and/or in the text file.

  • Existing files at the destination are overwritten.

  • I tested the script and it worked as expected.

set sourceFolder to POSIX path of (choose folder with prompt "Where are the source folders located?")
set destinationFolder to POSIX path of (choose folder with prompt "Where would you like to copy the files?")
set textFile to (choose file with prompt "Select the text file you wish to use")
set theFileRelativePaths to paragraphs of (read textFile)

repeat with aFile in theFileRelativePaths
		do shell script "ditto " & quoted form of (sourceFolder & aFile) & space & quoted form of (destinationFolder & aFile)
	on error
		display dialog "A file was not found:" & linefeed & linefeed & sourceFolder & aFile
	end try
end repeat

For this task I would probably use rsync. I think that all macs come with rsync but the default installation is archaic (v2). I’ve installed something newer for myself (v3) however the usage here is fairly basic so it should work with v2 or newer. As rsync is a deep tool, I recommend testing with the -n option, to see what files will be copied where but not actually make any changes. After confirming that it would perform the desired copying, remove the ‘n’ option and run again. You can type ‘man rsync’ in Terminal to see its documentation.

This script will copy each of the files in the filelist to the destination folder, including the necessary folder structure. In this case, it will make a ‘cru’ folder on the desktop to deposit the listed files in. Pay attention to leading and trailing ‘/’ as they can either break the script or modify its results (or do nothing).

set srcParent to POSIX path of (path to downloads folder) & "fired/"
set desParent to POSIX path of (path to desktop) & "cru"
set filesFromList to POSIX path of (path to downloads folder) & "filelist.txt"

set qsrc to quoted form of srcParent
set qdes to quoted form of desParent
set qList to quoted form of filesFromList

do shell script "/usr/local/bin/rsync -Itv --files-from=" & qList & space & qsrc & space & qdes
--> "/usr/local/bin/rsync -Itv --files-from='/Users/me/Downloads/filelist.txt' '/Users/me/Downloads/fired/' '/Users/me/Desktop/cru'"

The command the script passes to the shell is:

[format]“/usr/local/bin/rsync -Itvn --files-from=‘/Users/me/Downloads/filelist.txt’ ‘/Users/me/Downloads/fired/’ ‘/Users/me/Desktop/cru’”[/format]

[format]-Itvn combines the below options
-I ignore times
-t keep modified times
-v verbose
-n dry run (remove once the actions are confirmed correct)[/format]

When using --files-from, there should be three file references:
–files-from= specify text file containing list of files to copy
e.g. --files-from=‘/Users/me/Downloads/filelist.txt’

source folder parent of items in file list, in this case, parent of ‘clothes’
e.g. ‘/Users/me/Downloads/fired/’

destination folder parent of copied items, in this case, parent of the copied ‘clothes’


Thank you very much for your kind help.