Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

need to create a batch file that will move a certain file to the trash

Posted on 2006-10-29
4
Medium Priority
?
276 Views
Last Modified: 2010-04-14
Hello. I have to repeatedly delete a certain file, in a specific directory. I would like to know if anyone can help me create a batch file that will do this work for me? I could do this for a PC is no time, but I've never done this on a Mac before. It's a Macd OS X (10.3.9). Thanks in advance.
0
Comment
Question by:effincomputers
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
heteronymous earned 2000 total points
ID: 17829213
You could use Folder actions via AppleScript, but for a repeating action, I would tend to use a shell script and cron to fire off the script.

Fire up /Applications/Utilities/Terminal
type:
sudo mkdir /usr/local/bin

Then go with pico to start out.
sudo pico /usr/local/bin/mycleanup.sh

Which will create a file named "mycleanup.sh" in the directory listed above.
Enter in:
#! /bin/sh
/bin/rm /Path/To/Your/Folder/nameofyourfile.ext

(where "ext" is the file's extension, eg: txt , rtf , qxp , doc )
Then press ctl-x to save the script.
Type in: sudo chmod +x /usr/local/bin/mycleanup.sh to make the script executable.


Then setup a crontab entry to fire off the script.
http://linuxweblog.com/node/24

A good tutorial for starting out learning shell scripting (which is platform-agnostic, and common to all of unix/linux with some key caveats) is here:
http://www.shelldorado.com/
eg.
http://www.credochs.org/courses/IT/unix/ostart.htm

Pico is very simple. As of 10.4 it's replaced by nano - as the name implies, "little editors". In time you'll want to learn vi or emacs (and people will say one is better than the other for ever and forever).
0
 

Author Comment

by:effincomputers
ID: 17829349
Thank you for the info. I'm going to give it a shot later today. I'll be sure to give you the points if it works. Also, thank you for the additional info on this subject.
0
 
LVL 9

Expert Comment

by:heteronymous
ID: 17829990
Sure thing. While 10.4 now works with launchd and some periodic tasks run via that mechanism, cron is supported and is a more universal tool as far as *nix goes (linux, unix).

I should point out that to edit the crontab (file that lists cron jobs, repeating/periodic tasks)
must ALWAYS be edited via: (sudo) crontab -e
which by default will invoke the vi editor. Which could be a little confusing !
Some basic vi editor comands to help you get your crontab edit going :
press i to switch to edit mode (insert or delete existing text), or press "o" to start inserting text on a new line (below the current one). Each line in your crontab represents a specific job.

esc to exit edit mode/return to command mode, followed by
:w to write (literally, the ":" character with the "w" character), followed by
:q  to quit.
If you make a mistake, use  :q!  to quit without saving.
When in command mode (not in insert/edit mode), "h" moves the cursor left, "l" moves the cursor right.

Find more about vi (pronounced "vee-eye") at one of the sites I linked to earlier:
http://eserver.bell.ac.uk/mirrors/unix/unix11.htm
0
 
LVL 9

Expert Comment

by:heteronymous
ID: 17829999
I put (sudo)  above, don't actually type the brackets, if you want to edit the root/system crontab. Use:
sudo crontab -e

And the password asked for will be your main, administrator account password.
Do *not* delete any existing entries you find there.
Add what you want on a new line below the existing ones. Tasks that you invoke via the root crontab will run with root-user priveleges. So, it's not necessary if you can delete the file as an admin without needing to authenticate to do so.
In which case, use:
crontab -e

which will add your job to your admin-account crontab file.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Apple's Mac OS X has become an official member of the malware club. The Flashback Trojan has affected over half million Macs, worldwide. It is behavior that ultimately gets malware onto a person’s computer. Obsolete or out-of-date software helps…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question