[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

command at end of batch to delete itself

Posted on 2011-10-21
10
Medium Priority
?
775 Views
Last Modified: 2012-05-12
I have a batch that opens a few other scripts, and deletes everything when it is done.  but a few scripts take a while and run in the back ground.  

what can I add at the end of a batch file so that it deletes itself when it is done?  Because I dont want the user trying to execute it later by mistake.  
0
Comment
Question by:icecom4
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:ajcheung78
ID: 37007005
What I have done in the past is call another batch job in the background that that polls for the presence of an intermediate file that is created for the other earlier background scripts and when the background scripts are completed it deletes this intermediate file.  Therefore when this last batch job polls and finds those files missing it will delete the original script.
0
 
LVL 3

Expert Comment

by:ajcheung78
ID: 37007072
I suppose you also have some logic already in place to prevent a user from reexecuting while the background jobs are aready running as well - if not then having some file present that lets you know the operation is stil being completed would work well for this purpose.
0
 
LVL 5

Expert Comment

by:d_nedelchev
ID: 37007236
This should work:


:_end
start cmd.exe /C "ping -n 3 -w 1000 127.0.0.1>nul&del /q /f %~dpnx0"
exit
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 37007259
Do you want it to wait until the other tasks have completed, or just remove itself as soon as it starts them?

~bp
0
 
LVL 5

Expert Comment

by:d_nedelchev
ID: 37007357
My mistake. It will need another two sets of quotas:


:_end
start "WINDOW TITLE" cmd.exe /C "ping -n 3 -w 1000 127.0.0.1>nul&del /q /f "%~dpnx0""
exit
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 37007453
You could just add the following to the end of the BAT file.  It will delete the BAT file, and then one error will pop up, but it gets the job done.

del "%~f0"

Open in new window


~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37007653
Add this to the end of your batch (make it the last line) file and it will delete itself:


   del %~f0

Another way to handle this situation is to flatten the file completely. Like this:

   copy /y nul %~f0

Look at the DIR output of your files and notice it's size is now '0' bytes. The file is flat, empty, dead, gone.... etc.
0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 500 total points
ID: 37007669
Oops! Seems I'm a bit late...

Actually, I overlooked the quotes on %~f0... billprew didn't.

Got to try and synchronise my fingers and my brain...



0
 

Author Closing Comment

by:icecom4
ID: 37008787
thanks del "%~f0" worked like a charm
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37008849
Welcome.

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
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…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

872 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