Saturday, November 27, 2021

#1 2021-11-24 09:12:28 pm

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 14

Create a file/folder structure from a TXT list

Hello,

The below script was written by Mr. StefanK from this post:
https://macscripter.net/viewtopic.php?id=33823

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?

Applescript:

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:

clothes/new/list.docx
clothes/new/summer/coast/sandals.jpg
clothes/new/summer/skirt.jpg
clothes/old/winter/coat.jpg
clothes/stock.xlsx


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

clothes/
├── new/
│     ├── summer/
│     │     ├── coast/
│     │     │     └── sandals.jpg
│     │     └── skirt.jpg
│     └── list.docx
├── old/
│     └── winter/
│             └── coat.jpg
└── stock.xlsx


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

Offline

 

#2 2021-11-25 05:11:52 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11755
Website

Re: Create a file/folder structure from a TXT list

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

https://macscripter.net/search.php?acti … rch=Submit

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

https://macscripter.net/search.php?acti … rch=Submit


regards

Stefan

Offline

 

#3 2021-11-25 09:41:33 am

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 14

Re: Create a file/folder structure from a TXT list

thank you.
I will see it.

Offline

 

#4 2021-11-25 10:07:20 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Create a file/folder structure from a TXT list

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.

Applescript:

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
   try
       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

Last edited by peavine (2021-11-25 10:17:04 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2021-11-25 11:14:31 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 150

Re: Create a file/folder structure from a TXT list

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).

Applescript:

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:

"/usr/local/bin/rsync -Itvn --files-from='/Users/me/Downloads/filelist.txt' '/Users/me/Downloads/fired/' '/Users/me/Desktop/cru'"
-Itvn    combines the below options
    -I    ignore times 
    -t    keep modified times
    -v    verbose
    -n    dry run (remove once the actions are confirmed correct)

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'

Offline

 

#6 2021-11-25 12:37:26 pm

belinha
Member
From:: Brasil
Registered: 2017-03-27
Posts: 14

Re: Create a file/folder structure from a TXT list

StefanK
peavine
Mockman

Thank you very much for your kind help.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)