n4thanl's Avatar
n4thanl 3
24 Asked
260 Answered
78 Best
0
No one has voted on this question yet :(
2 years, 9 months ago

How do I write a batch file to delete everything excluding things?

If I want to delete all files in a directory but not moose.txt,,how do I do that?
Tip for best answer: M$0.25
Separate topics with commas, or by pressing return. Use the delete or backspace key to edit or remove existing topics.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

What is Your Answer?

0
0
0

2 Answers

0
rishiku's Avatar
rishiku | 2 years, 9 months ago
13
:: DELE.bat
::
:: Deletes Directory Entries *Except* for Specified File(s)
:: Wildcards (* and ?) may be Used in the File Name
:: (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF

MD SAVE Makes a Temporary "SAVE" Directory.
XCOPY %1 SAVE > NUL "> NUL" Suppresses On-Screen Messages.
ECHO Y | DEL . > NUL Deletes all Files in the Current
Directory showing no Prompts.

MOVE SAVE\*.* . > NUL Returns Excepted File(s) to the
RD SAVE Current Directory.
Removes "SAVE" Directory.
ECHO. Adds a Blank Line to the Display.
C:\BATCH\DR Displays the Results of the Operation.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$
geekmiser's Avatar
geekmiser | 2 years, 9 months ago Report

the only part i don't like about this script is that it doesn't take into account the possibility of subdirectories and the possible need to also delete these or the files in them.

n4thanl's Avatar
n4thanl | 2 years, 9 months ago Report

OK: Time to be direct. I wan to delete all user folders inside documents and settings except "All Users", "Administrator", "Default User", and selected others.

rishiku's Avatar
rishiku | 2 years, 9 months ago Report

The above should delete everything in the directory except for the Hidden, System, and Read-Only Files.

I may be mistaken though, question......what are you using this for....it seems like it might be used for some.....cruel idea.

patrickmc's Avatar
patrickmc | 2 years, 8 months ago Report

> OK: Time to be direct. I wan to delete all user folders
> inside documents and settings except "All Users",
> "Administrator", "Default User", and selected others.

Here is an alternate script.

# Script deletusers.txt

# Change directory to Documents and Settings.
cd "C:/Documents and Settings"

# Collect a list of all subfolders.
var str list
lf -n "*" "C:/Documents and Settings" ($ftype=="d") > $list

# Process subfolders one by one.
while ($list <> "")
do
# Get the next subfolder.
var str dir ; lex "1" $list > $dir

# Does this folder name contain "All Users",
# "Administrator" or "Default User" ?
if ( ( {sen "^All Users^" $dir} <= 0 ) AND ( {sen "^Administrator^" $dir} <= 0 ) AND ( {sen "^Default User^" $dir} <= 0 ) ) # This entire if statement must appear on one line.
do
# No this is not a user from our list.
# Delete this folder.
script "SS_SlashBack.txt" ospath($dir) > $dir
system -s "rmdir /SQ" ("\""+$folder+"\"")

done
endif

done

I have not tested the script. Test it first.

The script is in biterscripting ( http://www.biterscripting.com ). To try, save the script as C:/Scripts/deleteusers.txt, enter the following command in biterscripting.

script "C:/Scripts/deleteusers.txt"

I can see a use for such a script - an employee leaves company, you want to delete his/her "stuff" from all company computers.

Patrick

Report Abuse

Post Reply Cancel
0
dward's Avatar
dward | 2 years, 9 months ago
7
This might help it a question about renaming large batches of files. It highlights a program mentioned on the TWIT network. Not sure if the program has a delete feature but at the very least it would be easy to rename everything with the extension (somethling like .del).

the run a DOS command "delete *.del" or something like that.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$
geekmiser's Avatar
geekmiser | 2 years, 9 months ago Report

neat, hadn't thought of that. does it recurse through subdirectories?

Report Abuse

Post Reply Cancel

Learn something new with our FREE educational apps!

Private lessons in the comfort of your own home. Get back in shape or finally pick up a guitar with our great experts guiding you the whole way!
Learn Guitar
Learn Hip Hop
Learn Pilates