Jump to content


- - - - -

How do I delete files/folders based on a list in batch?


12 replies to this topic

#1 dkreifus

    I am the decider!

  • Moderators
  • 4701 posts

Posted 23 May 2007 - 10:00 PM

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.

#2 Taco Bell

    Life is drama ...

  • Retired Crew
  • PipPipPipPipPip
  • 15588 posts
  • Gender:Male
  • Location:Detroit, Michigan USA
  • Interests:Everything, but sports and politics.

Posted 24 May 2007 - 02:22 AM

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

#3 dkreifus

    I am the decider!

  • Moderators
  • 4701 posts

Posted 24 May 2007 - 03:41 AM

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

#4 Taco Bell

    Life is drama ...

  • Retired Crew
  • PipPipPipPipPip
  • 15588 posts
  • Gender:Male
  • Location:Detroit, Michigan USA
  • Interests:Everything, but sports and politics.

Posted 24 May 2007 - 03:50 AM

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

#5 dkreifus

    I am the decider!

  • Moderators
  • 4701 posts

Posted 24 May 2007 - 04:13 AM

thanks for the help man

#6 Phonics Monkey

    Custom member title

  • Moderators
  • 3191 posts
  • Gender:Male
  • Location:Florida, USA

Posted 24 May 2007 - 05:26 PM

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?

#7 Taco Bell

    Life is drama ...

  • Retired Crew
  • PipPipPipPipPip
  • 15588 posts
  • Gender:Male
  • Location:Detroit, Michigan USA
  • Interests:Everything, but sports and politics.

Posted 24 May 2007 - 05:43 PM

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.

#8 DangerousDave86

    Royal Family Member

  • Members
  • PipPipPipPipPip
  • 3085 posts
  • Gender:Male
  • Location:England

Posted 25 May 2007 - 05:17 PM

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...

#9 dkreifus

    I am the decider!

  • Moderators
  • 4701 posts

Posted 25 May 2007 - 05:19 PM

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.

#10 DangerousDave86

    Royal Family Member

  • Members
  • PipPipPipPipPip
  • 3085 posts
  • Gender:Male
  • Location:England

Posted 25 May 2007 - 10:19 PM

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?

Attached Files


Edited by DangerousDave86, 25 May 2007 - 10:22 PM.


#11 macrat

    n00b

  • Rookies
  • Pip
  • 2 posts

Posted 10 June 2007 - 08:57 PM

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?

#12 Angela

    n00b

  • Members
  • Pip
  • 1 posts

Posted 22 June 2011 - 02:56 PM

Hello -- This is the .vba script I used to delete files based on a .txt list of filenames. The ", True" at the end of line 8 was necessary to force delete since these were read-only files. The .bat file I use to run this script has the location of the .vba file, the location of the .txt file (containing filenames to be deleted -- make sure there's no header row in this file, only a list of filenames), and the location of the folder where these files should be deleted from.

Batch File:
"C:\Users\acox\Desktop\FileDelete.vbs" "F:\Delete_List.txt" "F:\TestImages"

VBA Script:
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
strSourcePath = objArgs(1)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
strLine = objFile.ReadLine
objfs.DeleteFile strSourcePath &"\"& strLine, True
On Error Resume Next
Loop

Edited by Angela, 22 June 2011 - 02:56 PM.


#13 zudrick

    n00b

  • Members
  • Pip
  • 1 posts
  • Gender:Male
  • Location:Scunny\UK

Posted 15 December 2011 - 02:19 AM

I use the following in a batch file:

takeown /f C:\Windows\System32\ar-SA /r /d y
icacls C:\Windows\System32\ar-SA /grant administrators:F /T
icacls \"%1\ /grant administrators:F"
RD /S /Q C:\Windows\System32\ar-SA

takeown /f C:\Windows\System32\bg-BG /r /d y
icacls C:\Windows\System32\bg-BG /grant administrators:F /T
icacls \"%1\ /grant administrators:F"
RD /S /Q C:\Windows\System32\bg-BG

takeown /f C:\Windows\System32\ca-ES /r /d y
icacls C:\Windows\System32\ca-ES /grant administrators:F /T
icacls \"%1\ /grant administrators:F"
RD /S /Q C:\Windows\System32\ca-ES

all the way through to zh-TW it then does the syswow64 folder then it takes ownership of the winsxs folder and deletes ar-SA through to zh-TW in there,
then deletes the licences except ultimate in system32 and syswow64 folders and boot folder too then i have it to renamed some files then copies new files to the folders, runs a reg file into the registry then shutsdown/ reboots.
This releaves a large amount of space which should'nt have been installed in the first place, and which i am working on at the minute ie:remove the forigen files from the boot.wim and the install.wim files





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users