Moving files, thoughts please...

Hi All,

The other week I found a lovely snippet of code for listing files in a folder:-

set a to "Mac HD:Users:this_user:Desktop:this_folder:" --Mac file path
set p to POSIX path of a --Convert to POSIX path 
set the_text to (do shell script "ls " & p)

I’ve updated one of my scripts, that generates an XML file from a list of files in a folder, with this and the speed difference is fantastic.

I was wanting to take my script one stage further and, from a list of say 150 images, move 1-50, 51-100 and 101-150 into 3 separate folders.

I can write that script, I just wondered if there was a way of moving a batch, or group, of files using a shell script or do I have to loop through each one in the batch separately?

Thanks in advance,

Nick

Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

The “mv” command does accept multiple input in the form “‘/path/to/folder/’{‘name 1’,‘name 2’,.}”. The script below illustrates the principle. There’s a limit on the length of ‘do shell script’ strings, but I think 50 file names at a time should be OK.

set a to (choose folder with prompt "Source folder?")
set d to (choose folder with prompt "Destination folder?")

set p to quoted form of POSIX path of a --Convert to (quoted) POSIX path
-- Get an AppleScript list of the individual item names inside folder a.
set p_items to paragraphs of (do shell script "ls " & p)

-- Get the first 50 (or less).
set n to (count p_items)
if (n > 50) then set n to 50

-- Create a Unix multipath (?) in the form "'/path/to/folder/'{'name 1','name 2',.}"
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "','" -- Single-quote & comma & single-quote.
set multi_p to p & "{'" & (items 1 thru n of p_items) & "'}" -- NB. single quotes just inside the braces.
set AppleScript's text item delimiters to astid

-- Move the items to the destination.
do shell script ("mv " & multi_p & space & (quoted form of POSIX path of d))

Hi Nigel,

Thanks for the reply and for the example and explanation.
I’ve tried the script a few times and it’s great, very nippy ! :slight_smile:

Just a thought, I know I talked about ‘moving’ the files however, on my travels I found an article which showed a non-shell variation.
Here the person talked about copying the files to the new location and then deleting them from the original location. This was a precautionary measure to try and reduce the chance of files going missing in the event of a power loss.

Using the above approach would you say there is still the need for this? Or, again, would it be better to copy the files using ‘cp’ and then remove the files using ‘rm’ ? Just wondered. :confused:

Thanks again Nigel.

Regards,

Nick

Edit:-

Came up with this which seems to work.

set a to (choose folder with prompt "Source folder?")
set d to (choose folder with prompt "Destination folder?")

set p to quoted form of POSIX path of a --Convert to (quoted) POSIX path
-- Get an AppleScript list of the individual item names inside folder a.
set p_items to paragraphs of (do shell script "ls " & p)

-- Get the first 50 (or less).
set n to (count p_items)
if (n > 50) then set n to 50

-- Create a Unix multipath (?) in the form "'/path/to/folder/'{'name 1','name 2',.}"
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "','" -- Single-quote & comma & single-quote.
set multi_p to p & "{'" & (items 1 thru n of p_items) & "'}" -- NB. single quotes just inside the braces.
set AppleScript's text item delimiters to astid

-- Move the items to the destination.
--do shell script ("mv " & multi_p & space & (quoted form of POSIX path of d))


-- OR copy and remove...

-- Copy the items to the destination.
do shell script ("cp " & multi_p & space & (quoted form of POSIX path of d))

-- Remove the items from the source.
do shell script ("rm " & multi_p)

I’ve not heard of that one before. :slight_smile:

As you probably know, “moving” files to different folders on the same disk doesn’t do anything to the files themselves but simply changes the disk catalogue entries. It’s therefore much faster than “duplicating”, which physically reproduces the substance of each file or folder at another place on the disk. But I suppose a power failure during during a catalogue rewrite could result in the effective loss of the files. The smart thing to do would be to invest in a UPS. :wink:

“Moving” files to another disk necessarily involves duplication, of course. “mv” deletes the originals from the source disk; the Finder doesn’t.