Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Even though Mac OS X has been around for many years now, certain well-liked and eminently useful software apps never made the jump from Mac OS 9 to Mac OS X. Ever used Word Perfect for Mac? Version 3.5 was the last version, built for Mac OS 9. Who c…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

25 Experts available now in Live!

Get 1:1 Help Now