Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Go through and copy files to new structure

Hello People!


I got a gigantic task i would like to simplify with a bit of Applescript.

Basically i'd like to move or copy a lot of files to a new folder structure.


I have a few different folders with about 500-4000 jpg's, see image below.

First image shows old structure, 2nd image show the new structure i would like to accomplish.

User uploaded fileUser uploaded file

What i would like to do is have a script loop through and take the first 4 characters of the filename and create a folder with that name in the location of my choosing.

Then create a new folder in the parent folder called JPEG and copy all the files with that specific name to that folder.


So far i have been able to automate the creation of folders and sub-folders.

any help is very much appreciated 🙂

set loc to choose folder "Choose Parent Folder Location"

repeat with i from 1 to 3

setjtotext -2 through -1 of ("51" & iastext)

try

tell application "Finder" to set targetFolder to make new folder at loc with properties {name:"61" & j}

tell application "Finder" to makenewfolderattargetFolderwith properties {name:"JPEG"}

tell application "Finder" to makenewfolderattargetFolderwith properties {name:"PSD"}

end try

end repeat

MacBook Pro (Retina, 15-inch, Late 2013), OS X El Capitan (10.11.3)

Posted on Mar 11, 2016 3:19 AM

Reply
Question marked as Best reply

Posted on Mar 14, 2016 5:27 AM

Hello


You might try the following AppleScript script which is mere wrapper of bash script.


It will let you choose source and destination directories and then scan the source tree for jpeg files and copy jpeg file with name starting with excatly 4 digits IJKL to the corresponding directory -I000/IJKL in the destination. The directories in the destination path are created as is needed. For example by your screenshots, you may choose the folder "7000-8000" or its parent directory as source directory and the folder "OBJECTS" as destination directory.


Any jpeg file which does not have name starting with exactly 4 digits is ignored. Verbose outputs of cp(1) and any errors will be reported in the result pane/window of (Apple)Script Editor.



--APPLESCRIPT set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where destination tree is rooted at")'s POSIX path if dst starts with src then error "Source tree may not contain destination." number 8000 set args to "" repeat with a in {src, dst} set args to args & a's quoted form & space end repeat do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=${1%/} DST=${2%/} { export LC_CTYPE=UTF-8 while IFS= read -r -d $'\\0' f do n=${f##*/} [[ $n =~ ^(([0-9])[0-9]{3})[^0-9] ]] || continue p=${BASH_REMATCH[1]} q=${BASH_REMATCH[2]} jpg=${DST}/-${q}000/${p}/JPEG psd=${DST}/-${q}000/${p}/PSD [[ -d $jpg ]] || mkdir -p \"$jpg\" || continue [[ -d $psd ]] || mkdir -p \"$psd\" || continue cp -v -pPR \"$f\" \"$jpg\" done < <(find \"$SRC\" -type f -iname '*.jpg' -print0) } 2>&1 exit 0 EOF" --END OF APPLESCRIPT




Briefly tested under OS X 10.6.8 but no warranties of any kind. Please make sure you have complete backup of the original directories before running this sort of script.


Good luck,

H

9 replies
Question marked as Best reply

Mar 14, 2016 5:27 AM in response to PJ.Henning

Hello


You might try the following AppleScript script which is mere wrapper of bash script.


It will let you choose source and destination directories and then scan the source tree for jpeg files and copy jpeg file with name starting with excatly 4 digits IJKL to the corresponding directory -I000/IJKL in the destination. The directories in the destination path are created as is needed. For example by your screenshots, you may choose the folder "7000-8000" or its parent directory as source directory and the folder "OBJECTS" as destination directory.


Any jpeg file which does not have name starting with exactly 4 digits is ignored. Verbose outputs of cp(1) and any errors will be reported in the result pane/window of (Apple)Script Editor.



--APPLESCRIPT set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where destination tree is rooted at")'s POSIX path if dst starts with src then error "Source tree may not contain destination." number 8000 set args to "" repeat with a in {src, dst} set args to args & a's quoted form & space end repeat do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=${1%/} DST=${2%/} { export LC_CTYPE=UTF-8 while IFS= read -r -d $'\\0' f do n=${f##*/} [[ $n =~ ^(([0-9])[0-9]{3})[^0-9] ]] || continue p=${BASH_REMATCH[1]} q=${BASH_REMATCH[2]} jpg=${DST}/-${q}000/${p}/JPEG psd=${DST}/-${q}000/${p}/PSD [[ -d $jpg ]] || mkdir -p \"$jpg\" || continue [[ -d $psd ]] || mkdir -p \"$psd\" || continue cp -v -pPR \"$f\" \"$jpg\" done < <(find \"$SRC\" -type f -iname '*.jpg' -print0) } 2>&1 exit 0 EOF" --END OF APPLESCRIPT




Briefly tested under OS X 10.6.8 but no warranties of any kind. Please make sure you have complete backup of the original directories before running this sort of script.


Good luck,

H

Mar 15, 2016 11:21 AM in response to PJ.Henning

Hello


You're quite welcome and glad to know it serves you well. 🙂


As for the later question, you might also try the following script depending upon your needs. It will treat name starting with 4 to 6 digits and use the leading 1 to 3 digits for grouping.


E.g.,


1234a.jpg -> -1000/1234/1234a.jpg 12345a.jpg -> -12000/12345/12345a.jpg 123456a.jpg -> -123000/123456/123456a.jpg




set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where destination tree is rooted at")'s POSIX path if dst starts with src then error "Source tree may not contain destination." number 8000 set args to "" repeat with a in {src, dst} set args to args & a's quoted form & space end repeat do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=${1%/} DST=${2%/} { export LC_CTYPE=UTF-8 while IFS= read -r -d $'\\0' f do n=${f##*/} # [[ $n =~ ^(([0-9])[0-9]{3})[^0-9] ]] || continue # 4 digits (leading 1 digit for grouping) # [[ $n =~ ^(([0-9]{2})[0-9]{3})[^0-9] ]] || continue # 5 digits (leading 2 digit for grouping) # [[ $n =~ ^(([0-9]{3})[0-9]{3})[^0-9] ]] || continue # 6 digits (leading 3 digit for grouping) [[ $n =~ ^(([0-9]{1,3})[0-9]{3})[^0-9] ]] || continue # 4 to 6 digits (leading 1 to 3 digtis for grouping) p=${BASH_REMATCH[1]} q=${BASH_REMATCH[2]} jpg=${DST}/-${q}000/${p}/JPEG psd=${DST}/-${q}000/${p}/PSD [[ -d $jpg ]] || mkdir -p \"$jpg\" || continue [[ -d $psd ]] || mkdir -p \"$psd\" || continue cp -v -pPR \"$f\" \"$jpg\" done < <(find \"$SRC\" -type f -iname '*.jpg' -print0) } 2>&1 exit 0 EOF"




All the best,

H

Mar 16, 2016 2:53 AM in response to Hiroto

That is fantastic!

thank you so much 🙂


Now Im trying to figure out how add a folder for the 5&6 digits files.

like this.

0005.jpg -> -0000/0005/JPEG/0005.jpg

15101.jpg -> 150000/150100/15101/JPEG/15101.jpg

160101.jpg -> 160000/160100/160101/JPEG/160101.jpg

160255.jpg -> 160000/160200/160255/JPEG/160255.jpg

160322.jpg -> 160000/160300/160322/JPEG/160322.jpg

Just to explain for the 6 digits.

16=year 01=month 01= sequential object number.

As you can see the 2015 filenames are missing the first zero 01 of the month-number, someone messed this up, I'm going to rename them at the end of this year.


Anyhow, you have helped me ton.

Thanks!

Mar 16, 2016 7:07 PM in response to PJ.Henning

Hello


You might try the following script for such file names starting with exactly 5 or 6 digits. This will ignore other names.


* For simplicity, script will not consider year part being 15 or 16 in pattern matching and consequently it will process 5 digits 16* name and 6 digits 15* name as well, such as


16188a.jpg -> 160000/160100/16188/16188a.jpg 151288a.jpg -> 150000/151200/151288/151288a.jpg



--APPLESCRIPT set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where destination tree is rooted at")'s POSIX path if dst starts with src then error "Source tree may not contain destination." number 8000 set args to "" repeat with a in {src, dst} set args to args & a's quoted form & space end repeat do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=${1%/} DST=${2%/} { export LC_CTYPE=UTF-8 while IFS= read -r -d $'\\0' f do n=${f##*/} [[ $n =~ ^(([0-9]{2})([0-9]{1,2})[0-9]{2})[^0-9] ]] || continue # 6-digit (yymmkk) or 5-digit (yymkk) p=${BASH_REMATCH[1]} y=${BASH_REMATCH[2]} m=${BASH_REMATCH[3]} m='0'$m m=${m: -2} jpg=${DST}/${y}0000/${y}${m}00/${p}/JPEG psd=${DST}/${y}0000/${y}${m}00/${p}/PSD [[ -d $jpg ]] || mkdir -p \"$jpg\" || continue [[ -d $psd ]] || mkdir -p \"$psd\" || continue cp -v -pPR \"$f\" \"$jpg\" done < <(find \"$SRC\" -type f -iname '*.jpg' -print0) } 2>&1 exit 0 EOF" --END OF APPLESCRIPT




And in case, the following script will normailse the parent folder name to 6 digits even for file name starting with 5 digits, such as.



15188a.jpg -> 150000/150100/150188/15188a.jpg



--APPLESCRIPT set src to (choose folder with prompt "Choose source folder where source tree is rooted at")'s POSIX path set dst to (choose folder with prompt "Choose destination folder where destination tree is rooted at")'s POSIX path if dst starts with src then error "Source tree may not contain destination." number 8000 set args to "" repeat with a in {src, dst} set args to args & a's quoted form & space end repeat do shell script "/bin/bash -s <<'EOF' - " & args & " SRC=${1%/} DST=${2%/} { export LC_CTYPE=UTF-8 while IFS= read -r -d $'\\0' f do n=${f##*/} [[ $n =~ ^([0-9]{2})([0-9]{1,2})([0-9]{2})[^0-9] ]] || continue # 6-digit (yymmkk) or 5-digit (yymkk) y=${BASH_REMATCH[1]} m=${BASH_REMATCH[2]} k=${BASH_REMATCH[3]} m='0'$m m=${m: -2} jpg=${DST}/${y}0000/${y}${m}00/${y}${m}${k}/JPEG psd=${DST}/${y}0000/${y}${m}00/${y}${m}${k}/PSD [[ -d $jpg ]] || mkdir -p \"$jpg\" || continue [[ -d $psd ]] || mkdir -p \"$psd\" || continue cp -v -pPR \"$f\" \"$jpg\" done < <(find \"$SRC\" -type f -iname '*.jpg' -print0) } 2>&1 exit 0 EOF" --END OF APPLESCRIPT




Good luck,

H

Go through and copy files to new structure

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.