Need help with File/Folder handling on a NAS

Hi all

Although I learned a lot from all the posts here, now I have a problem and can’t solve it… so here comes my first post, hope you guys can help.

I have several Macs, one of them works as a ‘Server’ so I can use Spotlight on his document from all other Macs (which I can’t if stored on my NAS). On a daily basis I wanna copy 2 Folders from my Mac Server → NAS, I start the script with crontab, that works fine.

To let the NAS ‘sleep’ when not used, I don’t mount the drive permanently. So in my script I wanna do the following steps :

  • Mount the drive from NAS
  • Delete the 2 Folder on my NAS
  • Copy the 2 folder from Mac → NAS
  • Unmount the drive

It used to work with this script (I know, no error handling)… but suddenly it doesn’t anymore (maybe a firmware upgrade on my NAS). Today it works as long as the folders do not exist on the NAS, if they do, the script can’t delete them and stops… so basically the script works only once… after that I get sometimes errors 10004, 1728 because it can’t delete the folders (seems to be a permission problem)


-- 1x pro Tag muss die ganze Rechnungsablage und der ganze Rechnungseingang
-- als Sicherheitskopie aufs NAS kopiert werden

try
	tell application "Finder"
		
		-- Externes Volume mounten
		mount volume "smb://<name>:<pw>@<correct IP>/Dokumente"
		--mount volume "smb://<correct IP>/Dokumente"
		
		-- zuerst löschen wir die Ordner Rechnungseingang und Ablage auf dem NAS
		delete folder "Dokumente:Gemeinsam:aa_Rechnungseingang:"
		delete folder "Dokumente:Gemeinsam:Ablage:"
		
		-- Dann werden die Inhalte von hier lokal raus aufs NAS in die richtigen Ordner kopiert
		duplicate folder "Snow Leopard:Users:Giusi:Documents:aa_Rechnungseingang:" to folder "Dokumente:Gemeinsam:"
		duplicate folder "Snow Leopard:Users:Giusi:Documents:Ablage:" to folder "Dokumente:Gemeinsam:"
		
		-- Externes Volume wieder abhängen
		tell application "Finder" to eject disk "Dokumente"
		
	end tell
end try

Now I tried a couple of different things but I can’t figure out what I’m doing wrong. Here the newest version, I even changed from smb to afp… This script runs once (if I clean the NAS first), it mounts the drive, tries to delete, then copies everything I want over to the NAS and then unmounts the drive… the second time it runs, I get errors because he can’t delete the folders and stops.


if "Dokumente" is not in (do shell script "ls /Volumes") then
	try
		mount volume "afp://<name>:<pw>@<correct ip>/Dokumente"
	on error
		return -- abort the script
	end try
end if
try
	tell application "Finder"
		
		if "aa:Rechnungseingang" is in (do shell script "ls /Volumes/Dokumente/Gemeinsam") then
			do shell script "/bin/rm -r /Volumes/Dokumente/Gemeinsam/aa_Rechnungseingang"
		end if
		if "Ablage" is in (do shell script "ls /Volumes/Dokumente/Gemeinsam") then
			do shell script "/bin/rm -r /Volumes/Dokumente/Gemeinsam/Ablage"
		end if
		
		-- Dann werden die Inhalte von hier lokal raus aufs NAS in die richtigen Ordner kopiert
		duplicate folder "Snow Leopard:Users:Giusi:Documents:aa_Rechnungseingang:" to folder "Dokumente:Gemeinsam:"
		duplicate folder "Snow Leopard:Users:Giusi:Documents:Ablage:" to folder "Dokumente:Gemeinsam:"
		
		-- Externes Volume wieder abhängen
		tell application "Finder" to eject disk "Dokumente"
	end tell
end try


Interesting is that it

  • can mount the drive
  • can’t delete the first folder
  • can delete the second folder
  • fails to copy the first folder with error 15267 (Folder exists) and stops
  • then it can’t unmount the drive as it probably leaves the script with the step described above

I’m sorry if this go too long but I would really appreciate a hint/help in this matter.

thanks

oraguru

Model: Mac Mini
AppleScript: 2.2.1
Browser: Chrome 25
Operating System: Mac OS X (10.7)

Hello.

I think you are almost there, just add an -f option to the two rm commands, so that they read

/bin/rm -rf ..

in your latest version.

That should force the deletion. This is of course all your responsibility. rm’s -f switch forces deletion, also of folders, -with contents.

Please come back if that doesn’t work.

In general, it is wise to work with a copy of some data on a memory stick, (with the differences that it imposes between a memory stick and an external volume.), because if you give a directory name that doesn’t exist or have some typos in an rm command, especially rm -rf, you may wipe out everything.

A way to ensure that the paths exists, and are correct, is to first:

  • fire up the Terminal
  • cd to the directory, from there (you can drag the folder over from findre
  • type pwd.
  • copy into a script editor window and make sure you are able to assign the text to a variable, by escaping it where it has to be escaped.
  • make a do shell script
do shell script "test -d " & yourvariable & " && echo true || echo false"
display dialog (result as text)

  • when the above works, that is, when the dialog returns true, then you can use your variable in an rm command.

Hey MacUsrII

Man you’re good!!! wow… that’s it ?! I mean I spent hours in different versions but didn’t think about to level up the power of ‘rm’…

Thank you very much, I tested it a couple of times and it works just fine!

thanks

Regards

oraguru

Hello.

How good I am, is up to you to decide. Yes flattering works, but seriously, there are a bunch of even awesome guys here. Which you’ll find out if you have more problems.

I am just glad you solved your problem, and didn’t wipe out your NAS in the process. We were dealing with the single most dangerous command on any *nix platform, the rm -rf, and I wouldn’t have you come back telling me that an error occured. That’s why I went into detail.

Good luck with your NAS, I am a bit jealous…