Tuesday, September 21, 2021

#1 2012-09-13 05:13:51 am

tubaad
Member
Registered: 2011-09-17
Posts: 5

Create folder and move files depending on data in CSV file

I have one large folder with thousands of mp3 files.
I want to create sub folders and move the mp3 files to these folders.
I have a CSV file that have all file names and info about what folder to add the file to.

CSV file looks like this. Note that some folders will contain several files.

"filename","foldername"
"examplefile1.mp3","examplefolder1"
"examplefile2.mp3","examplefolder1"
"examplefile3.mp3","examplefolder2"
"examplefile4.mp3","examplefolder3"

So I need a script that will read the CSV file line by line, create folder if not existing and move file to the corresponding folder.

Doable?
Greatful for any hints!

Last edited by tubaad (2012-09-13 05:15:18 am)

Offline

 

#2 2012-09-14 12:40:12 pm

Hans-Gerd Classen
Member
From:: Aachen, Germany
Registered: 2010-02-19
Posts: 192

Re: Create folder and move files depending on data in CSV file

Hi,

boring for the cracks i think ;-)

Applescript:

set baseDir to "" & (choose folder with prompt "Please choose the baseFolder to operate on.")
set theCsvArray to paragraphs of (read (choose file))

set ASTID to AppleScript's text item delimiters
repeat with i from 1 to count of theCsvArray
   try
       set theItem to item i of theCsvArray
       set AppleScript's text item delimiters to ","
       set fileName to text 2 thru -2 of (text item 1 of theItem)
       set folderName to text 2 thru -2 of (text item 2 of theItem)
       set newFolder to baseDir & folderName
       do shell script "mkdir -p " & quoted form of POSIX path of newFolder
       set oldFile to baseDir & fileName
       set newFile to baseDir & folderName & ":" & fileName
       do shell script "mv -f " & quoted form of POSIX path of oldFile & space & quoted form of POSIX path of newFile
   on error e
       display dialog e giving up after 5
   end try
end repeat
set AppleScript's text item delimiters to ASTID

Hope it'll work

Last edited by Hans-Gerd Classen (2012-09-14 01:19:45 pm)


Hans
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
AppleScript amateur with just basically english skills – so please be patient … ;-)

Offline

 

#3 2012-09-14 02:06:38 pm

tubaad
Member
Registered: 2011-09-17
Posts: 5

Re: Create folder and move files depending on data in CSV file

Phenomenal, it works like a charm!
Thank you very much!
big_smile

Offline

 

#4 2012-09-15 04:16:02 am

Hans-Gerd Classen
Member
From:: Aachen, Germany
Registered: 2010-02-19
Posts: 192

Re: Create folder and move files depending on data in CSV file

thx for replying smile


Hans
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
AppleScript amateur with just basically english skills – so please be patient … ;-)

Offline

 

#5 2012-09-15 06:43:39 am

tubaad
Member
Registered: 2011-09-17
Posts: 5

Re: Create folder and move files depending on data in CSV file

Actually, You helped me last time I posted something here as well:)
http://macscripter.net/viewtopic.php?id=37058

Offline

 

#6 2021-08-02 04:59:38 pm

4bitm
Member
Registered: 2021-01-12
Posts: 13

Re: Create folder and move files depending on data in CSV file

Hans-Gerd Classen, can you make this script work with part of the name instead of complete names? My CVS looks like this:

88806    A
88807    A
10838    B
10837    B
10840    B
88808    C
88809    C
88852    C
88854    C
88819    D
11167    D
88849    A
88818    B
11053    D
11673    A



my file list looks like this:

MTHOOD_88806_0728_OFT.zip
CORVALIS_88807_0728.zip
WINDRIVER_10838_0728_MODM.zip
EDMONTON_10837_0728_MILF.zip
STJAMES_10840_0728_CCC7W.zip
EVERETT_88808_0728.zip
STMARTEN_88809_0728_AMF.zip
REDMOND_88852_0728_JIGGY.zip
KIRKLAND_88854_0728_RDMIZR.zip
MEDINA_88819_0728_LIT.zip
ORCAS_11167_0728_TTM.zip
YARROW_88849_0728.zip
HUNTS_88818_0728_PWP.zip
OVERALKE_11053_0728_DWD.zip
ROSEHILL_11673_0728_FOM.zip



Thank you

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)