Jump to content

Deleting With a Batch File


Recommended Posts

Hey!

I have about 16GB of files (over 11,000) in a folder and most of them are unneeded. The only ones I need have a certain sequence of characters in the file name. How do I delete all files that don't contain this sequence using a batch file? Although, if this can be done easier in the command line I'd like to know how as well.

Thanks in advance.

Link to comment
Share on other sites

After a couple more hours of searching the internet, I have found a batch file that does the trick:

:: DELE.bat
:: Deletes Current Directory Entries Except for Specified Files
:: Wildcards (* and ?) may be Used in File Names
:: (Hidden, System, and Read-Only Files are Not Affected)
::
@ECHO OFF

IF "%1" == "" GOTO END

MD SAVE

:MOVE-FILES
XCOPY %1 SAVE > NUL
SHIFT
IF "%1" == "" GOTO FILES-DELETE
GOTO MOVE-FILES

:FILES-DELETE
ECHO Y | DEL . > NUL
MOVE SAVE\*.* . > NUL
RD SAVE
DR

:END

http://www.chebucto.ns.ca/~ak621/DOS/Bat-Adv1.html#DELE

Thanks anyway.

Link to comment
Share on other sites

  • 2 weeks later...
Hey!

I have about 16GB of files (over 11,000) in a folder and most of them are unneeded. The only ones I need have a certain sequence of characters in the file name. How do I delete all files that don't contain this sequence using a batch file? Although, if this can be done easier in the command line I'd like to know how as well.

Thanks in advance.

I will assume the following.

1. The path to the folder is C:\folder. This folder contains the 16 GB files.

2. The sequence of chars you are looking for is, to make it more general, "k0\naP l\nk<any number of unprintable characters>Q". (k0 followed by newline, then aP, a space, I, a new line, nk, any number of spaces/tabs etc.)

3. Files are text files.

Here is the script

# script delete_no_sequence.txt
# Collect the list of all files.
var str list; lf -n "*" "C:\folder" > $list
# Process one file at a time.
while ($list <> "")
do
# Get the next file.
var str file; lex "1" $list > $file
# Read file into a string variable.
var str content; cat $file > $content
# Does the content contain our sequence ?
if ( { sen -r "^k0\naP l\nk;Q^" $content } <= 0 )
# No, content does NOT contain the sequence of characters. Delete this file.
system delete ("\""+$file+"\"")
endif
done

Script is in biterscripting ( http://www.biterscripting.com ). We are using -r option to indicate regular expression with the sen command (string enumerator/counter). We are using ; (semicolon) to indicate any number of spaces/tabs/etc. . To try the script, save the script as C:\Scripts\delete_no_sequence.txt, start biterscripting, enter the following command.

script -l delete_no_sequence.txt

I have not been able to test. Test it first with test folder and test files.

Patrick

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy