Solved

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

Posted on 2006-10-29
4
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
A professional opinion on which Apple product to buy, and a tidbit about the WWDC.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 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