Jump to content

Batch emptying a selection of folders?


Recommended Posts

Guest googleicq@yahoo.com
Posted

Can someone please tell me how I can delete the files from 100+

folders *without opening each one*, selecting all the files and then

deleting them?

 

TIA!

  • Replies 17
  • Created
  • Last Reply
Guest Pegasus \(MVP\)
Posted

Re: Batch emptying a selection of folders?

 

 

<googleicq@yahoo.com> wrote in message

news:821651a4-3612-4c78-a8fc-ce1fe6e79daf@f63g2000hsf.googlegroups.com...

> Can someone please tell me how I can delete the files from 100+

> folders *without opening each one*, selecting all the files and then

> deleting them?

>

> TIA!

 

You could right-click each of the folders, then press the Delete

key on your keyboard.

 

When you say "Batch", are you thinking of a batch file program?

If so then you need to post exactly where your 100+ folders

reside and how the batch file would "know" which ones to delete.

Guest John John (MVP)
Posted

Re: Batch emptying a selection of folders?

 

googleicq@yahoo.com wrote:

> Can someone please tell me how I can delete the files from 100+

> folders *without opening each one*, selecting all the files and then

> deleting them?

>

> TIA!

 

What does the directory structure look like? Are all the folders under

one tree and do you want to delete all the files in all the folders in

the tree? Or are the folders all over the place, or do you want to only

delete files in select folders in a tree and leave files in other folders?

 

John

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 09 Jun 2008 07:48:50 -0300, "John John (MVP)"

<audetweld@nbnet.nb.ca> wrote:

>What does the directory structure look like? Are all the folders under

>one tree and do you want to delete all the files in all the folders in

>the tree? Or are the folders all over the place, or do you want to only

>delete files in select folders in a tree and leave files in other folders?

>

>John

 

 

Yes all the folders are under one tree.

No I don't want to delete all the files in all the folders.

Yes, I want to delete files in select folders in a tree and leave

files in other folders in that tree.

 

I'm hoping there can be a batch file written or some other painless

way of deleting all of these files.

 

Maybe selecting all the folders in the tree, and then de-selecting the

ones (just a few ) whose file I want to keep?

 

TIA!

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 9 Jun 2008 12:23:38 +0200, "Pegasus \(MVP\)"

<I.can@fly.com.oz> wrote:

>You could right-click each of the folders, then press the Delete

>key on your keyboard.

 

 

It is my understanding that when you press Delete after

right-clicking a folder, you delete the *folder* plus its contents.

 

>When you say "Batch", are you thinking of a batch file program?

>If so then you need to post exactly where your 100+ folders

>reside and how the batch file would "know" which ones to delete.

 

Yes I am thinking about perhaps a batch file (.bat).

The folders all reside under one other folder.

 

Again, I don't want to delete entire folders, just their contents.

Knowing which folders to leave alone would be the tricky part, because

this is changed each time I need to go through this exercise.

 

So of approx. 100 folders, I'd like to keep the files in approx.

10-20.

Guest Pegasus \(MVP\)
Posted

Re: Batch emptying a selection of folders?

 

 

<googleicq@yahoo.com> wrote in message

news:h3vq449i5qcb3c6iu165me2p696hq7do4i@4ax.com...

> On Mon, 9 Jun 2008 12:23:38 +0200, "Pegasus \(MVP\)"

> <I.can@fly.com.oz> wrote:

>

>>You could right-click each of the folders, then press the Delete

>>key on your keyboard.

>

>

> It is my understanding that when you press Delete after

> right-clicking a folder, you delete the *folder* plus its contents.

>

>

>>When you say "Batch", are you thinking of a batch file program?

>>If so then you need to post exactly where your 100+ folders

>>reside and how the batch file would "know" which ones to delete.

>

> Yes I am thinking about perhaps a batch file (.bat).

> The folders all reside under one other folder.

>

> Again, I don't want to delete entire folders, just their contents.

> Knowing which folders to leave alone would be the tricky part, because

> this is changed each time I need to go through this exercise.

>

> So of approx. 100 folders, I'd like to keep the files in approx.

> 10-20.

>

 

Unless you can spell out a rule that says which folder should

be retained (or which ones should be deleted), a batch file

solution is not feasible.

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 9 Jun 2008 22:22:48 +0200, "Pegasus \(MVP\)"

<I.can@fly.com.oz> wrote:

>

>Unless you can spell out a rule that says which folder should

>be retained (or which ones should be deleted), a batch file

>solution is not feasible.

 

 

OK.

 

Well in my initial post I wasn't necessarily looking for a 'batch

file' solution. I thought perhaps I could go this route.

 

I was looking for some software solution that would do the job on a

'batch' of folders.

 

Would you have any suggestions for a software solution?

 

If I selected all the folders and then de-selected the ones I wanted

left alone, is there some way to have the *contents* of the selected

folders deleted, all at the same time?

 

TIA

Guest Twayne
Posted

Re: Batch emptying a selection of folders?

 

> On Mon, 9 Jun 2008 22:22:48 +0200, "Pegasus \(MVP\)"

> <I.can@fly.com.oz> wrote:

>>

>> Unless you can spell out a rule that says which folder should

>> be retained (or which ones should be deleted), a batch file

>> solution is not feasible.

>

>

> OK.

>

> Well in my initial post I wasn't necessarily looking for a 'batch

> file' solution. I thought perhaps I could go this route.

>

> I was looking for some software solution that would do the job on a

> 'batch' of folders.

>

> Would you have any suggestions for a software solution?

>

> If I selected all the folders and then de-selected the ones I wanted

> left alone, is there some way to have the *contents* of the selected

> folders deleted, all at the same time?

>

> TIA

 

That would work. And when you have a folder selected and issue a

Delete, that folder and ALL of its contents will be deleted. If that's

what you want, it saves picking out files in the folder; just choose the

folder itself, but realize, the folder itself will also be deleted.

 

I'm not so sure your question if fully understood; but that's how I read

it.

 

Twayne

Guest Pegasus \(MVP\)
Posted

Re: Batch emptying a selection of folders?

 

 

<googleicq@yahoo.com> wrote in message

news:nr5r44pi3jfkum0jn7qhrrsendr0mgrq6j@4ax.com...

> On Mon, 9 Jun 2008 22:22:48 +0200, "Pegasus \(MVP\)"

> <I.can@fly.com.oz> wrote:

>>

>>Unless you can spell out a rule that says which folder should

>>be retained (or which ones should be deleted), a batch file

>>solution is not feasible.

>

>

> OK.

>

> Well in my initial post I wasn't necessarily looking for a 'batch

> file' solution. I thought perhaps I could go this route.

>

> I was looking for some software solution that would do the job on a

> 'batch' of folders.

>

> Would you have any suggestions for a software solution?

>

> If I selected all the folders and then de-selected the ones I wanted

> left alone, is there some way to have the *contents* of the selected

> folders deleted, all at the same time?

>

> TIA

 

The way to do this would be to create an extra option that

becomes visible when right-clicking one of the selected files,

and linking it to a batch file that performs the job. Unfortunately

I am not sufficiently familiar with the technique to shake it out

of my sleeve.

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Tue, 10 Jun 2008 00:29:58 +0200, "Pegasus \(MVP\)"

<I.can@fly.com.oz> wrote:

 

>The way to do this would be to create an extra option that

>becomes visible when right-clicking one of the selected files,

>and linking it to a batch file that performs the job. Unfortunately

>I am not sufficiently familiar with the technique to shake it out

>of my sleeve.

 

 

OK, this sounds more along the lines of what I'm after.

But the 'right-clicking' would have to be an option that

becomes accessible after I have selected _all the folders I'd like

to act upon. (You mentioned files, but I believe you were thinking

folders).

 

Doing it one by one would not be much more helpful than what I have to

do now.

 

I hope someone here knows how to write such a .bat file.

 

What I'm asking for seems quite fundamental, when you look at the

monumental size and abilities of Win XP.

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 9 Jun 2008 17:47:08 -0400, "Twayne"

<nobody@devnull.spamcop.net> wrote:

 

>

>That would work. And when you have a folder selected and issue a

>Delete, that folder and ALL of its contents will be deleted. If that's

>what you want, it saves picking out files in the folder; just choose the

>folder itself, but realize, the folder itself will also be deleted.

>

>I'm not so sure your question if fully understood; but that's how I read

>it.

 

Thanks for jumping in Twayne!

 

No, the important thing is to be able to KEEP the folders and just

delete their contents!

 

These same folders with their unique names will eventually be loaded

up again with files. It would not be fun remembering all of their

names and typing them in again!

Guest John John (MVP)
Posted

Re: Batch emptying a selection of folders?

 

googleicq@yahoo.com wrote:

> On Mon, 09 Jun 2008 07:48:50 -0300, "John John (MVP)"

> <audetweld@nbnet.nb.ca> wrote:

>

>

>>What does the directory structure look like? Are all the folders under

>>one tree and do you want to delete all the files in all the folders in

>>the tree? Or are the folders all over the place, or do you want to only

>>delete files in select folders in a tree and leave files in other folders?

>>

>>John

>

>

>

> Yes all the folders are under one tree.

> No I don't want to delete all the files in all the folders.

> Yes, I want to delete files in select folders in a tree and leave

> files in other folders in that tree.

>

> I'm hoping there can be a batch file written or some other painless

> way of deleting all of these files.

>

> Maybe selecting all the folders in the tree, and then de-selecting the

> ones (just a few ) whose file I want to keep?

>

> TIA!

 

If this is a one time affair it's probably easier to delete the files

manually. If the directory structure is only one sub-folder deep it can

be pretty easy to do in a semi-automatic way, if the folders are nested

several folders deep it will be a bit harder.

 

You have to create a base list of folders to work with. The basis for

the command is easy to grasp and completely safe to test by issuing the

following commands at the command prompt:

 

Using the CD command navigate to your desired starting folder and issue:

 

dir /ad /b /s >c:\dirlist.txt

 

Then issue:

 

for /f "usebackq delims=" %A in ("c:\dirlist.txt") do echo "%A"

 

Now you understand what you have to do. Change the echo command in the

above line with your favorite commands and you will be able to do what

you want. Be careful with this, test on sample folders first and make

sure that you have backups before you proceed! Files deleted at the

command prompt do not go to the Recycle Bin, they are permanently deleted!

 

If the folders are only one folder deep inside a base directory, like such:

 

D:\Test\Test1

D:\Test\Test2

D:\Test\Test3

D:\Test\Test4

D:\Test\Test5

 

and if the files to delete are only in the numbered Test folders then

you can simply:

 

1- Create a folder list.

 

Using the CD command navigate to D:\Test and issue:

 

dir /b /ad /s >d:\dirlist.txt

 

 

2- Manually delete the numbered test folders as needed.

 

3- Issue:

 

for /f "usebackq delims=" %A in ("d:\dirlist.txt") do md "%A"

 

This will create new empty directories to replace the ones you have

manually deleted, the folders and files which were not deleted will be

unaffected and the directory structure will be restored.

 

In the second example let's assume that you want to only keep the files

in the Test1 folder and delete all the files in the other numbered Test

folders.

 

1- Create a folder list.

 

Navigate to D:\Test and issue:

 

dir /b /ad /s >d:\dirlist.txt

 

 

2- Edit the dirlist.txt file so that it contains only the folders to be

deleted:

 

D:\Test\Test2

D:\Test\Test3

D:\Test\Test4

D:\Test\Test5

 

 

3- Issue the RD command against the folder list:

 

for /f "usebackq delims=" %A in ("d:\dirlist.txt") do rd /s /q "%A"

 

This will delete all but the Test\Test1 folder.

 

 

4- Issue the MD command to recreate the deleted folders:

 

for /f "usebackq delims=" %A in ("d:\dirlist.txt") do md "%A"

 

As explained in the last example below you could also do this with this

command at step 3 and forgo step 4:

 

for /f "usebackq delims=" %A in ("d:\dirlist.txt") do del "%A\*.*" /q

 

 

In this last example lets change the directory structure so that it is

deeper than one sub-directory and lets assume files may be in any folder

in the structure. The folder structure is like so:

 

D:\Test\Test1\Folder 1

D:\Test\Test2\Folder 2

D:\Test\Test3\Folder 3

D:\Test\Test4\Folder 4

D:\Test\Test5\Folder 5

 

Lets assume that all of the folders and sub-folders contain files and

that we want to delete all the files only in the following directories:

 

D:\Test

D:\Test\Test3

D:\Test\Test5\Folder 5

 

Once again we use the dir command to create the folder list and then we

edit the file so that it only contains the above mentioned folders.

 

However, we cannot globally use the RD command against the list as it

would also delete sub-folders which contain files that we want to keep.

Instead we use the DEL command with wildcard characters:

 

for /f "usebackq delims=" %A in ("d:\dirlist.txt") do del "%A\*.*" /q

 

This will delete all files in the folders in the list only, it will not

delete directories or files in other folders.

 

Don't try to use the RD command from a folder to be deleted. This was

tested at the Command Prompt, in a batch file double up the %:

 

for /f "usebackq delims=" %%A in ("d:\dirlist.txt") do md "%%A"

 

Use with care, make sure you have backups and pay attention to what you

are doing. No one but you can be held responsible if you delete all

your important files!

 

John

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 09 Jun 2008 21:46:38 -0300, "John John (MVP)"

>

>If this is a one time affair it's probably easier to delete the files

>manually. If the directory structure is only one sub-folder deep it can

>be pretty easy to do in a semi-automatic way, if the folders are nested

>several folders deep it will be a bit harder.

 

 

No it's not a one time affair. I'd need to run it every few months.

Yes the directory structure is just one sub-folder deep.

 

 

Your commands look great! Thanks very much!

 

I set up some test folders and files and just gave it a run through.

I can get the dirlist OK but the step after that isn't working.

 

Here's the message I'm getting at the DOS prompt:

 

E:\Test>mdbat

\test\dirlist.txt") do md "A" was unexpected at this time.

E:\Test>for /f "usebackq delims=" \test\dirlist.txt") do md "A"

 

Some of the brackets and quote symbols may not look right above but

they were returned when I ran the .bat file.

The ) actually looked more like a >, but I don't have that symbol on

my keyboard (a rounded arrow).

 

>If the folders are only one folder deep inside a base directory, like such:

>

>D:\Test\Test1

>D:\Test\Test2

>D:\Test\Test3

>D:\Test\Test4

>D:\Test\Test5

>

>and if the files to delete are only in the numbered Test folders then

>you can simply:

>

>1- Create a folder list.

>

>Using the CD command navigate to D:\Test and issue:

>

>dir /b /ad /s >d:\dirlist.txt

>

>

>2- Manually delete the numbered test folders as needed.

>

>3- Issue:

>

>for /f "usebackq delims=" %A in ("d:\dirlist.txt") do md "%A"

>

 

This is as far as I got and got stuck as outlined above.

TIA

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Mon, 09 Jun 2008 21:46:38 -0300, "John John (MVP)"

 

 

Hi John,

 

I was anxious to try the creation of the dirlist on my actual folders

and just tried it now.

It works just fine, but what would the command be to have the

subfolders listed in alpha order?

 

Most of the subfolders are first last name, so sorting by

last name would be nice (sorting by first name would be my second

choice).

 

here's what it's like now:

E:\Jazz\Lage Lund

E:\Jazz\John Stowell

E:\Jazz\Adam Rogers

E:\Jazz\Oliver Nelson

E:\Jazz\Jack Wilkins

E:\Jazz\Dan Hicks

etc.

 

Thanks again!

 

Ric

Guest John John (MVP)
Posted

Re: Batch emptying a selection of folders?

 

googleicq@yahoo.com wrote:

> On Mon, 09 Jun 2008 21:46:38 -0300, "John John (MVP)"

>

>>If this is a one time affair it's probably easier to delete the files

>>manually. If the directory structure is only one sub-folder deep it can

>>be pretty easy to do in a semi-automatic way, if the folders are nested

>>several folders deep it will be a bit harder.

>

>

>

> No it's not a one time affair. I'd need to run it every few months.

> Yes the directory structure is just one sub-folder deep.

>

>

> Your commands look great! Thanks very much!

>

> I set up some test folders and files and just gave it a run through.

> I can get the dirlist OK but the step after that isn't working.

>

> Here's the message I'm getting at the DOS prompt:

>

> E:\Test>mdbat

> \test\dirlist.txt") do md "A" was unexpected at this time.

> E:\Test>for /f "usebackq delims=" \test\dirlist.txt") do md "A"

 

 

It works fine here, this is the output at the Command Prompt:

 

Microsoft Windows XP [Version 5.1.2600]

© Copyright 1985-2001 Microsoft Corp.

 

C:\>

 

C:\>cd drivers

 

C:\DRIVERS>dir /ad /b /s >c:\dirlist.txt

 

C:\DRIVERS>cd\

 

C:\>for /f "usebackq delims=" %A in ("c:\dirlist.txt") do echo "%A"

 

C:\>echo "C:\DRIVERS\AUDIO"

"C:\DRIVERS\AUDIO"

 

C:\>echo "C:\DRIVERS\MODEM"

"C:\DRIVERS\MODEM"

 

C:\>echo "C:\DRIVERS\MOUSE"

"C:\DRIVERS\MOUSE"

 

C:\>echo "C:\DRIVERS\NETWORK"

"C:\DRIVERS\NETWORK"

 

C:\>echo "C:\DRIVERS\NETWORK2"

"C:\DRIVERS\NETWORK2"

 

C:\>echo "C:\DRIVERS\OTHER"

"C:\DRIVERS\OTHER"

 

C:\>echo "C:\DRIVERS\OTHER2"

"C:\DRIVERS\OTHER2"

 

C:\>echo "C:\DRIVERS\OTHER3"

"C:\DRIVERS\OTHER3"

 

C:\>echo "C:\DRIVERS\VIDEO"

"C:\DRIVERS\VIDEO"

 

C:\>echo "C:\DRIVERS\WLAN"

"C:\DRIVERS\WLAN"

 

 

I suggest that you copy and paste to avoid syntax errors. Try the above

at a Command Prompt. Your error is replicated below when the first %

sign is omitted from the command:

 

C:\>

C:\>for /f "usebackq delims=" A in ("c:\dirlist.txt") do echo "%A"

A was unexpected at this time.

C:\>

 

Don't forget, in a batch file you have to double up the % in the commands:

 

for /f "usebackq delims=" %%A in ("c:\dirlist.txt") do echo "%%A"

 

John

Guest John John (MVP)
Posted

Re: Batch emptying a selection of folders?

 

googleicq@yahoo.com wrote:

> On Mon, 09 Jun 2008 21:46:38 -0300, "John John (MVP)"

>

>

> Hi John,

>

> I was anxious to try the creation of the dirlist on my actual folders

> and just tried it now.

> It works just fine, but what would the command be to have the

> subfolders listed in alpha order?

>

> Most of the subfolders are first last name, so sorting by

> last name would be nice (sorting by first name would be my second

> choice).

>

> here's what it's like now:

> E:\Jazz\Lage Lund

> E:\Jazz\John Stowell

> E:\Jazz\Adam Rogers

> E:\Jazz\Oliver Nelson

> E:\Jazz\Jack Wilkins

> E:\Jazz\Dan Hicks

> etc.

>

> Thanks again!

 

At the command prompt do: dir /?

 

Look at the /O switch.

 

dir /ad /b /O:N /s >c:\dirlist.txt

 

John

Guest googleicq@yahoo.com
Posted

Re: Batch emptying a selection of folders?

 

On Tue, 10 Jun 2008 00:03:21 -0300, "John John (MVP)"

>

>Don't forget, in a batch file you have to double up the % in the commands:

>

>for /f "usebackq delims=" %%A in ("c:\dirlist.txt") do echo "%%A"

 

 

Thanks John, that was my mistake. I forgot to add an extra % for

the .bat file.

 

It works perfectly! I tested it and also used it on my actual

folders/files.

 

Thanks again, I really appreciate it. This will be a big time saver!

 

Ric

Guest John John (MVP)
Posted

Re: Batch emptying a selection of folders?

 

googleicq@yahoo.com wrote:

> On Tue, 10 Jun 2008 00:03:21 -0300, "John John (MVP)"

>

>

>>Don't forget, in a batch file you have to double up the % in the commands:

>>

>>for /f "usebackq delims=" %%A in ("c:\dirlist.txt") do echo "%%A"

>

>

>

> Thanks John, that was my mistake. I forgot to add an extra % for

> the .bat file.

>

> It works perfectly! I tested it and also used it on my actual

> folders/files.

>

> Thanks again, I really appreciate it. This will be a big time saver!

 

You're welcome.

 

John


×
×
  • Create New...