How to make a .bat file (or likewise) to remove a bunch of files? [Resolved]

I have never been a programmer except for some HTML, ages ago. Now I’d like to ask how you guys make those scripts - .bat or something else? - that removes stuff on the system. There are quite a few system files I remove manually after every reinstalltion of Windows XP, it would be nice to run a file that automatically removes all those files (which I’ve put on a nice list in a .txt file).

Thanks for advice.

LA

Wow, now I can finally be a programmer expert (:KWL)

For files:


cd "(location of files)"
DEL "(file name.extension)"

Create one DEL-line with file name for each file you want to delete.

Example:


cd "C:\WINDOWS\"
DEL "explorer.exe"
DEL "anotherthing.exe"

cd "C:\WINDOWS\system32\"
DEL "winlogon.exe"
DEL "services.exe"
DEL "svchost.exe"

For folders:


cd "(location of folder to remove)"
RD /S /Q "folder name"

Create one RD /S /Q-line with folder name for each folder you want to delete.

Example:


cd "C:\WINDOWS\system32\"
RD /S /Q "dllcache"
RD /S /Q "useless"

cd "C:\WINDOWS\system32\RTC"
RD /S /Q "something"

You can use wildcards like something.* or *.old.

Cheers,
Ragwing

I’ve sent you a URL That Contains A Batch File That Deletes the files you want gone,
W/ The Code To Show you how to do so…

Thanks guys (:HUG)

In case any of the files in my list doesn’t exist, what will happen? Will the batch file just run normally and delete all other files in the list?

LA

It will just run normally and delete the other files on the list

Excellent! This will be very convenient the next time I reinstall Windows, which I suppose will happen quite soon. 88)

LA

Good Luck :smiley:

and thank’s

For the lines that contain RD (Remove Directory), you can prefix those lines with "echo y| "

(N.B. the " | " is the PIPE symbol, not a lower case “L”)

This prefix “pipes” “Y” for YES to automatically answer the request to remove the directory.

At the end of each line that contains a “del” command, you can suffix “>nul:”. This redirects screen output to the nul: device, as opposed to the default output of con:. This keep your screen output clean.

Cheers,
Ewen :slight_smile:

I don’t think he even has the null driver (null.sys) if he followed Bold Fortune lol

ROFL!!

If only he could get rid of that large, pesky video driver - he’d save so much space and have so much more time. :slight_smile:

LOL. I do have the null.sys driver. :-X

Can I add sc delete proper name of any service in the batch file? Would that work to remove a service?

LA

Yes, it would remove the service from the registry, and unlist it from services.msc. However, the files for the service will remain.

OK thanks. Then I’m lucky to say that the files I wanted to delete through the batch file are actually files related to services. So this will be a nice remove-services-and-related-files batch file. :-TU

LA

Thank you LA for starting this thread I did not realize that running a batch file had the same effect as running from a DOS prompt without starting Windows I should have really :-[
Prior to XP you could do this.
Thanks again
Dennis

So you didn’t follow BF all the way and left DOS intact, otherwise these commands are useless.

I think Panic’s ROFL was because the “nul” in his post is not a real device or driver, but just an alias, meaning, “instead of outputting to the default output device, con (screen), output to nul (that is output to nothing, don’t give output)”. I’d bet the null.sys file is unrelated to this, and it’s “nul” not “null”. Not sure, LOL. Another example, if instead of “> nul” you added “> prn”, the output would be sent to the printer–at least in the old DOS days.

You can wirte in a .BAT or .CMD file whatever you can write in the command line interface (cmd.exe), one command in each line. (If you don’t want the commands to be shown as they’re run, put a “[ at ]” in front of those you don’t want shown, or “[ at ]echo off” as the first command if you want none shown. This echo is a separate thing from the output.

By the way you don’t need to change the active folder everytime you want to delete files, for example instead of

cd "C:\WINDOWS"
DEL "explorer.exe"

you can enter

del "C:\WINDOWS\explorer.exe"

(I wouldn’t recommend running this though, LOL.) The quotation marks are really necessary only if the pathname includes spaces. And I think that the DEL command can delete folders, empty or not, in WinXP, it’s true that in DOS it didn’t so you needed RD and it was a pain so they made DELTREE, but now DEL will detele anything I think.

And it’s better to name the script with a .CMD extension instead of a .BAT one. The difference is that a .BAT file is taken as a DOS script and run in a 16 bits virtual DOS machine (VDM)–when available: ntvdm.exe in Windows 32 bits, not available in Windows 64 bits. A .CMD file is taken as a Win32/64 file and runs natively. Of course either option works the same most times if it includes simple commands like DEL etc., although ntvdm.exe is processor-intensive. Not sure what happens when you run a .BAT file in a system without builtin VDM (64 bits), I guess it’s just run natively like a .CMD one since there’s no other option… so this would only apply to 32 bits Windows.

I think they’re also need if the executable has its own switches you want to include. e.g.

"C:\Program Files\ABC\abc.exe" /L:enu

Cool. I didn’t know. I’'ll rename my .bat’s to .cmd’s. BTW, is it ntvdm.exe or ntvmd.exe?

Oops, it’s ntvdm.exe, that stands for “NT Virtual DOS Machine”. Just run a DOS program or script in Win32 and browse the task manager (and see the cpu usage…). If it’s a Windows 3.x program you’ll also see a wowexec.exe.

No, for example the following would work without quotation marks:

C:\abc\abc.exe /L:enu

In you example it was necessary however, because there’s a space in “program files”: so you need quotation marks because without them the command line would understand that you want to run a “program” executable in the C:\ root folder, and everything else would be passed as parameter for that executable. In DOS the quotations marks weren’t needed nor used and would have caused an error, since spaces weren’t allowed in filenames; and still many commands and programs accepted switches.

Better be safe than sorry :P. I sometimes forget these little things.