Format USB Drives and Copy a file to them

I am working on a script put together from some code I have pieced together through Google.
The script looks at drives attached, in this case USB, eliminates the Boot drive, reformats each drive and copies a file to it.

I’ve got the reformat working but getting the path to the /dev/disk# and copying the file is at a standstill.

The Code looks like this so far:
— Code starts here —
set tList to do shell script “diskutil list | grep ‘/dev/disk’” – Obtain list of current volumes, in a string format.

set {oAStid, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, return}
set tList to text items of tList – List of current volumes, in a list format.
–set mList to text items of mList

set tList to rest of tList – Remove ‘boot’ volume from list.
set AppleScript’s text item delimiters to oAStid

repeat with i in tList – Cycle through list, attempting to reformat each listed volume.
do shell script ("diskutil eraseVolume ‘MS-DOS FAT32’ ‘UDISK 28X’ " & i) – Reformat individual volume.
delay 10 --wait 10 seconds for reformat to finish
set mList to do shell script ("diskutil info|grep -i ‘Volume Name:’ " & i) --get the Volume name
set dsk to path to desktop
set disk_name to (POSIX path of mList) --use the Volume name to copy the file
set file_to_copy to quoted form of (POSIX path of dsk & “child_protection.doc”)
set target_file to quoted form of (POSIX path of disk_name & “:child_protection.doc”)
do shell script "cp " & file_to_copy & space & target_file
end repeat

— Code ends here —

The script is ending prematurely with: grep: /dev/disk1: Resource busy on the line: set mList to do shell script ("diskutil info|grep -i ‘Volume Name:’ " & i)
How can I grab the mount point name and then copy the file I want to that mount point?

Thanks…

Jacques,

You rock brother!

I’m not a programmer, obviously, and all I’m usually capable of is decent research. I will study this to get a greater understanding of what’s happening, but it does what I need it to!

Just in time, I might add.

You have no idea how appreciated you are!

If I wanted to pick a file instead of explicitly specifying one like “child_protection.doc” would I just use (choose file)? (without the quotes around it)???


set target_file to quoted form of POSIX path of (choose file)

I am ultimately trying to make this as flexible as possible so combined this with some other code from here to copy multiple files. So far, I have this, but it doesn’t seem to click:

--Code starts here ---
set theCommand to "cp "
set tList to paragraphs 2 thru -1 of (do shell script "/usr/sbin/diskutil list | /usr/bin/grep '/dev/disk'")
set the_files to choose file with prompt "What to Copy?" with multiple selections allowed without invisibles

repeat with Device_Node in tList
	-- Reformat individual volume and return the volume path
	set disk_path to last paragraph of (do shell script "/usr/sbin/diskutil eraseVolume 'MS-DOS FAT32' 'UDISK 28X' " & Device_Node & "
	/usr/sbin/diskutil info " & Device_Node & " | /usr/bin/grep 'Mount Point:.*/' | /usr/bin/sed  's/.*Mount Point: *//'")
	
	if disk_path starts with "/Volumes/" then
		repeat with the_file in the_files
			set the_unix_file_path to the quoted form of POSIX path of the_file
			do shell script theCommand & the_unix_file_path
		end repeat
	end if
end repeat



tell application "Finder"
	display dialog "Files Copied" buttons {"OK"} default button 1
end tell

Hi,

in the copy line the destination is missing


.
	do shell script theCommand & the_unix_file_path & space & quoted form of disk_path
.

It’s always the small things :slight_smile:

Thanks, Stefan

Hello, I need to format (to exFAT) , name and copy the same file to 200 USB’s.

I was wondering if someone could help me in how to do this. I’m not a scripter at all.

I tried using the above code but I don’t know if I’m using it correctly as it’s not working.

Would anyone be able to help me please?

This is a 12-year-old script. Nonetheless, when you run the script in Script Editor, it should tell you where it’s failing. It would be helpful to know that.