Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Batch file help


  • Please log in to reply

#1
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
I'm never sure where to ask questions about batch files, but since I'm writing it for XP, this is as good a place as any.

My question is: how can I get a batch file to delete itself? I tried "del /f", but to no avail. I was thinking maybe I could get it to "move" itself to the trash can, but how would I do this? (what is the trash can's file location?)
  • 0

Advertisements


#2
Neil Jones

Neil Jones

    Member 5k

  • Member
  • PipPipPipPipPipPipPipPip
  • 8,476 posts
Not possible AFAIK. Would have been under old DOS, but XP won't let you delete something its using itself. This could also include batch files.
  • 0

#3
stettybet0

stettybet0

    Trusted Tech

  • Topic Starter
  • Technician
  • 2,579 posts
no chance even by using my "move" to trash can theory? I know a file can move itself... so why not to the trash can? I just don't know how to reference the trash can.
  • 0

#4
Whiskeyman

Whiskeyman

    Member

  • Member
  • PipPipPip
  • 469 posts
To test this create a folder and place a text file within it. I made a new folder in C:\ named Test. I then created a text file in Notepad named test.txt. I used the following batch file (test.bat) to remove the text file from the folder then delete the batch file itself.

del C:\Test\test.txt
del test.bat

  • 0

#5
stettybet0

stettybet0

    Trusted Tech

  • Topic Starter
  • Technician
  • 2,579 posts
umm... im not sure what the text file thing was for... but del .\test.bat works... But why wasn't it working before??? Hmm... the only thing I can think of is that right before I have the file delete itself, I have it run shutdown -r -t 0. So, it begins the restart process immediately. I would think it would be able to delete the file while shutting down, but maybe not... but I need the file to initialize a restart... how can I make it do that and delete itself?!? I tried adding more time before the restart process started, but that didn't help.
  • 0

#6
Whiskeyman

Whiskeyman

    Member

  • Member
  • PipPipPip
  • 469 posts
Following my same setup.

shutdown /f /r
del C:\Test\test.txt
del test.bat

The only reason I use C:\Test\test.txt is because I constantly use the Test folder for testing purposes. No sense in continually recreating it. This way only the file located within it gets deleted.
  • 0

#7
dsenette

dsenette

    Je suis Napoléon!

  • Community Leader
  • 26,047 posts
  • MVP
when you initiate the shutdown command with the /f command it FORCES anything else running to terminate..therefor as soon as you hit the line shutdwon /f /s it terminates the batch file

why are you trying to delete a batch file as soon as it runs?
  • 0

#8
stettybet0

stettybet0

    Trusted Tech

  • Topic Starter
  • Technician
  • 2,579 posts
Im using it as part of the installation process for a program of mine to automatically install an included font file. However, once the person runs it, they have no reason to run it again, and it would have errors (the font file to install would no longer be there!) So I want it to delete itself. I need it to restart, because newly installed fonts don't always work immediately unless a restart occurs.

I could write a second batch file that deletes the original one and itself and make a registry file that lets it run on startup, but I really don't want to (more trouble than its worth).
  • 0

#9
dsenette

dsenette

    Je suis Napoléon!

  • Community Leader
  • 26,047 posts
  • MVP
well...theoretically...you could have the installer create the second batch that would delete the first and itself in the startup folder...that way as soon as the PC restarts they both get deleted....you could also have it pop up a message box that says "you must restart your computer" and have it delete itself but NOT force the restart
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP