Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now