Solved

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

Posted on 2006-10-29
4
266 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 500 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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Information security is a multi-billion dollar industry. Just as lucrative is the black market industry which trades stolen identities, credit card numbers and software exploits all over the world. Nothing is hack-proof. The best one can do is make …
A lot of new and distinct gadgets are making their appearance every other day. The latest gadget that has wooed the attention of all gadget lovers and non gadget lovers alike is the Smartwatch. This tiny gadget is capable of offering live access to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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