Dear All,
I have one text file which contains folders names & their hirerachy see below example.
[Root]
[[1]]
[[[11]]]
[[[[111]]]]
[[2]]
[[[11]]]
[[[[111]]]]
[Root1]
[[2]]
Now, I want develop one script to read this file and create the folders of Root, 1, 2 Root1 as per the hirerachy. Withing Root there two main sub folder called 1 & 2. Within the folder 1 there is another folder called 11, etc.
Just I need one example of it.
It is a part of my project, some script i have already developed like missing fonts, missing images, etc. In fact I have already created one script for folder creation but it requires so many information from user, I want to defile those things in the text file and as user will run this script, it will ask for the location where the folders will get created.
Thanks
Macrajeev
Hi Macrajeev,
You might want to study one of my AppleScripts named subidoo, which can create complete folder structures from predefined sets.
In this script I used the following technique:
Folder tree sets are saved in a text file according to the following format:
Customers/Correspondence/-> Incoming/
Customers/Correspondence/-> Outgoing/
Customers/Invoices/
Customers/Offers/
Customers/Orders/
Suppliers/Correspondence/-> Incoming/
Suppliers/Correspondence/-> Outgoing/
Suppliers/Invoices/
Suppliers/Offers/
Suppliers/Orders/
These folder sets are then read and parsed into lists of relative folder paths, which can be easily created anywhere using the «mkdir -p» command, which creates intermediate directories as required.
So if your user chooses ‘/Users/foo/Documents/MyDir/’ as the target directory, you can easily use code as follows to create the subfolders:
set targetfolderpath to "/Users/martin/Desktop/test/"
set relfolderpaths to {"Customers/Correspondence/-> Incoming/", "Customers/Correspondence/-> Outgoing/", "Customers/Invoices/", "Customers/Offers/", "Customers/Orders/", "Suppliers/Correspondence/-> Incoming/", "Suppliers/Correspondence/-> Outgoing/", "Suppliers/Invoices/", "Suppliers/Offer"}
repeat with relfolderpath in relfolderpaths
set fullpath to targetfolderpath & relfolderpath
set fullpath to fullpath as «class utf8»
do shell script "mkdir -p " & quoted form of fullpath
end repeat
Does this help a bit? I hope so!
Hi Martin,
Thanks a lot for giving such tip.
Exactly I want to define in a text file and parse the text file and create the folder structure. I don’t want to update the code every time. User will update the text file, and so that they will easily get the folder structure. Your code is creating the folders as per mention in the script.
Could you please tell me how can I do it.
Thanks
This is a very simple script that reads a folder set from a text file, which you need to slightly modify (e.g. filepath). The text file is supposed to have Mac OS Roman text encoding with the relative folder paths just entered as given in my first example.
But keep in mind that this can get tricky. If the users edit the text file themselves and they also accidentally change the text encoding, then your script code might get in trouble.
That is why subidoo hides the folder set database from the user Only the script itself reads and writes to the database. Anyway, here we go:
set filepath to POSIX file "/Users/martin/Desktop/foldersets.txt"
try
set openfile to open for access file filepath
set filecont to read openfile
close openfile
on error errmsg number errnum
try
close openfile
end try
error errmsg number errnum
end try
set relfolderpaths to paragraphs of filecont
set targetfolderpath to "/Users/martin/Desktop/test/"
repeat with relfolderpath in relfolderpaths
set fullpath to targetfolderpath & relfolderpath
set fullpath to fullpath as «class utf8»
do shell script "mkdir -p " & quoted form of fullpath
end repeat
Hi, Martin.
That’s an interesting line! I didn’t think ‘as «class utf8»’ did anything outside of the File Read/Write commands. Do you have any particular reason for using it? Getting the class of fullpath immediately afterwards returns ‘unicode text’ anyway, which is utf16.
Sorry, that was just a silly mistake. Since I started to use Python helper scripts in my AppleScript projects, I formed a habit of converting every command to UTF-8 text encoding before executing it. Therefor the line should really be as follows:
repeat with relfolderpath in relfolderpaths
set fullpath to targetfolderpath & relfolderpath
set command to "mkdir -p " & quoted form of fullpath
set command to command as «class utf8»
do shell script command
end repeat
Hi Martin,
I am getting one error of “NSCannotCreateScriptCommantError”. How to take care of it.
Thanks
But my point was that, as a coercion, ‘as «class utf8»’ actually returns UTF-16 (pre-Leopard AppleScript’s ‘Unicode text’, Leopard AppleScript’s ‘text’). It only produces UTF-8 when ‘as’ is a parameter of ‘write’.
-- 'as' as AppleScript coercion:
set command to command as «class utf8»
--> 'command' is Unicode text (UTF-16).
-- 'as' as 'write' parameter:
write command as «class utf8» to fRef
--> UTF-8 written to file.
-- 'as' as 'read' parameter:
set command to (read fRef as «class utf8»)
--> File data interpreted as UTF-8, returned as UTF-16.
@Nigel:
So then maybe it is about time to drop my bad habit I remember having a lot of problems with passing arguments to a Python script, it did not properly import special characters like German umlauts and other obscure signs and symbols. Then I read about converting the whole command to UTF-8 and when I did that, everything worked fine. But you are absolutely right, it seems to be plain stupid :lol: So thanks for your advice!
@Macrajeev:
Have you changed the file paths in the script code according to your own environment? And have you created a text file containing one relative file path per line with Mac Roman text encoding? Please give me/us some more details about your current system and how you executed the given script code. Because on my Mac it runs just fine. Thanks!
Model: PowerBook 17" 1.33 GHz 1024 MB RAM
Browser: Safari 523.15
Operating System: Mac OS X (10.5)