Help - Search - Members - Calendar
Full Version: How do I delete files/folders based on a list in batch?
ieXbeta Board > Tech > Developer Center
dkreifus
Is there a command I can use to delete several folders, based on a list?

Basically, I want to have a list of files, called dellist.txt, each folder listed on a separate line.
CODE
windows\system32\ar-SA
windows\system32\bg-BG
windows\system32\cs-CZ
Program Files\IE
...etc

(random file names, not the exact ones in question)

The way I have it scripted currently, (which isn't working) is:
CODE
rd /s /q %direct%\mount < c:\dellist.txt


All the folders (windows, program files, etc) are located under the mount folder listed here.
Taco Bell
Batch files are too simplistic to handle that approach dk.

Instead, you'd have to pass those paths as arguments to a secondary batch file that did the actual work of deleting. For example:

deleter.bat
CODE
CALL deleter-sub.bat "windows\system32\ar-SA" "windows\system32\bg-BG" "windows\system32\cs-CZ" "Program Files\IE"

deleter-sub.bat
CODE
:LOOP
IF %1 == "" GOTO END
rd /s /q "%direct%\mount\%1"
GOTO LOOP
SHIFT
:END

The SHIFT command allows a batch file to operate on an unknown number of parameters as, with each call, it does a shift left of the values. As result, the original value in the 1st parameter is discarded and replaced by the value of the 2nd parameter. The value in the 2nd parameter is replaced by the value in the 3rd parameter, etc.

I'm sure there are other approaches, but that seemed like the easiest for now, so hope it helps.

By the way, I confirmed it works too. wink2.gif
dkreifus
That makes sense...but is there a way to keep a list of all the folders to delete in a different file?

There's like 20 folders..and if it came to that point, I could just "rd /s /q" each folder, if I have to type it out, y'know?

I was hoping to use the shift....alas
Taco Bell
I tried to use a list at first dk, but couldn't [easily/quickly] make it work.

With a more powerful language such as Perl or the new Windows PowerShell I could, but not a simple batch file.

Maybe some other clever soul can come up with a way though. clover.gif
dkreifus
thanks for the help man
Phonics Monkey
How is the "DelList.txt" file being generated?

If there are no spaces in the folder paths then the quotes won't be necessary (for you to add) for Taco's deleter.bat to run so if the dellist is auto-generated all you would need to do is add the call function to the top of it, change it's extension to bat, & light the fuse.

...or am I missing something?
Taco Bell
Well, there are spaces in at least some of the example paths (i.e. Program Files) Phonics.

Also, the calling function can't handle those arguments being a list format like dk orignally wanted.

Therefore, the best compromise is probably to store in the space separated format like I proposed and redirect that data file to feed the caller.
DangerousDave86
You could write this in PHP (as always, my favourite) and compile it to exe, like I did for AYHJA. Infact. This is so simple I'll do it now...
dkreifus
I don't know much scripting, just some batch.

I generate the txt file list myself, and add files to it.
They're listed one per line.

Currently, I just took the whole list and added it to my batch file, of files to be deleted.

Working well so far, so I can't knock it.
DangerousDave86
CODE
<?php
// File deleter...
if (empty($_SERVER['direct'])) die ("Deletion Path Not Found");
if ($_SERVER['argc'] != 2) die ("Invalid Input Parameters, Please Specify Filename");
if (!is_readable($_SERVER['argv'][1])) die ("Unable To Read Deletion List Filename");

# Read list into array!
$delete_list = file($_SERVER['argv'][1]);
foreach ($delete_list as $dir)
{
    # Tidy blank lines and line endings
    $dir = trim($dir);
    if ($dir == '') continue;
    # Tidy up to allow for trailing slashes
    $dir = implode(explode('\\', $dir), '\\');
    $_SERVER['direct'] = implode(explode('\\', $_SERVER['direct']), '\\');
    # run deletion
    delete_all($_SERVER['direct'].$dir);
}

function delete_all ($dir)
{
    # Directory doesnt exist, skip it
    if (!file_exists ($dir)) return;
    # Get directories in delete path
    $dirs = glob ($dir."\{,.}*", GLOB_ONLYDIR | GLOB_BRACE);
    # if directiories, run delete on them all, delete the tree
    if (count($dirs) > 0)
    {
        foreach ($dirs as $sub_dir)
        {
            # dont delete . or .. current or parent dir
            if ($sub_dir == $dir.'\.' OR $sub_dir == $dir.'\..') continue;
            # run deletion
            delete_all($sub_dir);
        }
    }
    # get all files and directories (which will now be empty)
    $delete = glob($dir."\\{,.}*", GLOB_BRACE);
    # loop them all
    foreach ($delete as $file)
    {
        # dont delete . or .. current or parent dir
        if ($file == $dir.'\.' OR $file == $dir.'\..') continue;
        # if directory, delete dir, if not delete file
        is_dir($file) ? rmdir($file) : unlink($file);
    }
    # remove original directory
    rmdir($dir);
}
Echo "Done";
?>

remove_directories.exe directoryListFile.txt

Just to be sure, '%direct%' is an environmental variable right?
macrat
How about deleting a bunch of files based on a list generated by another program like Zeroed.exe

It generates a list that looks like this:

C:\Documents and Settings\Smaug\My Documents\bpftp\Sites\Default 2.bps
C:\Documents and Settings\Smaug\My Documents\bpftp\Sites\Distinct 2.bps
C:\Documents and Settings\Smaug\My Documents\bpftp\Sites\ftp.blood.com(5).bps
C:\Documents and Settings\Smaug\My Documents\bpftp\Sites\ftp.distinct.com(6).bps
C:\Documents and Settings\Smaug\My Documents\Cashmaster\_UTOCASH_exe
C:\Documents and Settings\Smaug\My Documents\Cashmaster\c103507 - Inesoft Cash Organizer 2003 Deluxe v4_43 dt_exe

Each file takes up one line of text.

I rescued a couple of hard drives that were accidentally deleted, and for a couple of thousand files, in place of recoverable data, it has zeroed data. The list is really long, taking 20 MBs. Must I spend the rest of my life doing it manually?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.