• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Delete all files except one folder

L

liu

Flightless Bird
I have over 200 folders I need to delete all the files inside but keep
one folder (same name for all the folders) intact. Instead of going to
each folder to delete files manually, is there a way to batch that
process?

Thanks,
 
D

dadiOH

Flightless Bird
liu wrote:
> I have over 200 folders I need to delete all the files inside but keep
> one folder (same name for all the folders) intact. Instead of going to
> each folder to delete files manually, is there a way to batch that
> process?
>
> Thanks,


Search
Select all
De-select the one you want to keep
Delete

--

dadiOH
____________________________

dadiOH's dandies v3.06...
....a help file of info about MP3s, recording from
LP/cassette and tips & tricks on this and that.
Get it at http://mysite.verizon.net/xico
 
B

Bob I

Flightless Bird
use Xcopy /T /E to duplicate the empty directory structure then delete
the full one.

liu wrote:
> I have over 200 folders I need to delete all the files inside but keep
> one folder (same name for all the folders) intact. Instead of going to
> each folder to delete files manually, is there a way to batch that
> process?
>
> Thanks,
 
L

liu

Flightless Bird
On Jun 9, 3:07 pm, "dadiOH" <dad...@invalid.com> wrote:
> liu wrote:
> > I have over 200 folders I need to delete all the files inside but keep
> > one folder (same name for all the folders) intact. Instead of going to
> > each folder to delete files manually, is there a way to batch that
> > process?

>
> > Thanks,

>
> Search
>     Select all
>         De-select the one you want to keep
>             Delete
>

That probably needs a lot of work. You would have to know every
filename inside the folder you don't want to delete or visually going
through the list.
 
P

Pegasus [MVP]

Flightless Bird
"liu" <spamfreeliu@yahoo.com> wrote in message
news:503a80b8-5214-4cd1-bf69-b491ba554a5c@a42g2000vbl.googlegroups.com...
> I have over 200 folders I need to delete all the files inside but keep
> one folder (same name for all the folders) intact. Instead of going to
> each folder to delete files manually, is there a way to batch that
> process?
>
> Thanks,


I'm sure it can be done with a simple batch file but I confess that I am not
clear about your requirements. Please elaborate with an example.
 
L

liu

Flightless Bird
> I'm sure it can be done with a simple batch file but I confess that I am not
> clear about your requirements. Please elaborate with an example.


Thank you very much for the offer to help. Here is what I have:
Multiple projects each one with a runtime (many HTML, ASP, images
etc), In each project there is a Source folder named "Source".
I'd like to delete the runtime while keep the Source folder intact.
There are many images, etc files in the Source folder too. Basically,
delete everything in root except "Source" folder and the content
inside.

Thanks,
 
P

Pegasus [MVP]

Flightless Bird
"liu" <spamfreeliu@yahoo.com> wrote in message
news:a73eaee2-4e38-44cd-af32-d0c6786e51c7@g18g2000vbl.googlegroups.com...
>> I'm sure it can be done with a simple batch file but I confess that I am
>> not
>> clear about your requirements. Please elaborate with an example.

>
> Thank you very much for the offer to help. Here is what I have:
> Multiple projects each one with a runtime (many HTML, ASP, images
> etc), In each project there is a Source folder named "Source".
> I'd like to delete the runtime while keep the Source folder intact.
> There are many images, etc files in the Source folder too. Basically,
> delete everything in root except "Source" folder and the content
> inside.
>
> Thanks,


Your description is still rather general. Please quote a specific example of
what you wish to keep and what you wish to delete.
 
D

dadiOH

Flightless Bird
liu wrote:
> On Jun 9, 3:07 pm, "dadiOH" <dad...@invalid.com> wrote:
>> liu wrote:
>>> I have over 200 folders I need to delete all the files inside but
>>> keep one folder (same name for all the folders) intact. Instead of
>>> going to each folder to delete files manually, is there a way to
>>> batch that process?

>>
>>> Thanks,

>>
>> Search
>> Select all
>> De-select the one you want to keep
>> Delete
>>

> That probably needs a lot of work. You would have to know every
> filename inside the folder you don't want to delete or visually going
> through the list.


Why would you have to know the file names? ? You said you wanted to delete
*all* the files in all folders except those in one folder; you said nothing
about keeping *specific* files within a *specific* folder.

You need a better folder naming scheme.

--

dadiOH
____________________________

dadiOH's dandies v3.06...
....a help file of info about MP3s, recording from
LP/cassette and tips & tricks on this and that.
Get it at http://mysite.verizon.net/xico
 
D

dadiOH

Flightless Bird
liu wrote:
>> I'm sure it can be done with a simple batch file but I confess that
>> I am not clear about your requirements. Please elaborate with an
>> example.

>
> Thank you very much for the offer to help. Here is what I have:
> Multiple projects each one with a runtime (many HTML, ASP, images
> etc), In each project there is a Source folder named "Source".
> I'd like to delete the runtime while keep the Source folder intact.
> There are many images, etc files in the Source folder too. Basically,
> delete everything in root except "Source" folder and the content
> inside.


Ah, you want to keep *all* folders that have a given name, not just one
physical folder.

The only thing I can think of is to use a renaming program to batch rename
each "Source" folder to "Source1... Source2...etc". You could then use
Windows search to locate them, move all to a different location, then use
Windows search as I originally suggested on the root folder to delete what
was left in the project folders.


--

dadiOH
____________________________

dadiOH's dandies v3.06...
....a help file of info about MP3s, recording from
LP/cassette and tips & tricks on this and that.
Get it at http://mysite.verizon.net/xico
 
L

liu

Flightless Bird
> > Thank you very much for the offer to help. Here is what I have:
> > Multiple projects each one with a runtime (many HTML, ASP, images
> > etc), In each project there is a Source folder named "Source".
> > I'd like to delete the runtime while keep the Source folder intact.
> > There are many images, etc files in the Source folder too. Basically,
> > delete everything in root except "Source" folder and the content
> > inside.

>
> > Thanks,

>
> Your description is still rather general. Please quote a specific example of
> what you wish to keep and what you wish to delete.


project a
project a\file1.htm
project a\file2.htm
...
project a\file3.css
project a\file4.js
....
project a\images\file5.jpg
....
project a\Source\file6.fla
project a\Source\file7.fla
.....

I'd like to delete everything inside project a including folders
inside except Source folder. Keep everything inside the source folder
no matter what are inside.

project b
same structure but different files.

Do the same for the remaining 200 more projects with the same
structure.

Thanks for the help,
 
L

liu

Flightless Bird
> Ah, you want to keep *all* folders that have a given name, not just one
> physical folder.
>
> The only thing I can think of is to use a renaming program to batch rename
> each "Source" folder to "Source1... Source2...etc".  You could then use
> Windows search to locate them, move all to a different location, then use
> Windows search as I originally suggested on the root folder to delete what
> was left in the project folders.
>

Then I have to move them back to the original project folder. How do I
that non-manually?
That may be better than doing everything manually and probably easier
to write a batch to move them back.

Thanks for the suggestion!
 
P

Pegasus [MVP]

Flightless Bird
"liu" <spamfreeliu@yahoo.com> wrote in message
news:9feb6371-7331-41f7-b604-e41a20418119@18g2000vbi.googlegroups.com...
>> > Thank you very much for the offer to help. Here is what I have:
>> > Multiple projects each one with a runtime (many HTML, ASP, images
>> > etc), In each project there is a Source folder named "Source".
>> > I'd like to delete the runtime while keep the Source folder intact.
>> > There are many images, etc files in the Source folder too. Basically,
>> > delete everything in root except "Source" folder and the content
>> > inside.

>>
>> > Thanks,

>>
>> Your description is still rather general. Please quote a specific example
>> of
>> what you wish to keep and what you wish to delete.

>
> project a
> project a\file1.htm
> project a\file2.htm
> ...
> project a\file3.css
> project a\file4.js
> ...
> project a\images\file5.jpg
> ...
> project a\Source\file6.fla
> project a\Source\file7.fla
> ....
>
> I'd like to delete everything inside project a including folders
> inside except Source folder. Keep everything inside the source folder
> no matter what are inside.
>
> project b
> same structure but different files.
>
> Do the same for the remaining 200 more projects with the same
> structure.
>
> Thanks for the help,


OK, things are getting clearer. Try this:
[01] @echo off
[02] SetLocal EnableDelayedExpansion
[03] set Source=c:/Project
[04] set Target=c:/HoldingFolder
[05] set Action=echo
[06] for /d %%a in ("%Source%*.*") do (
[07] for %%b in ("%%a") do set Name=%%~na
[08] %Action% md "%Target%\!Name!"
[09] %Action% move "%%a\Source" "%Target%\!Name!"
[10] %Action% rd "%%a" /s /q
[11] %Action% move "%Target%\!Name!" C:/
[12] pause
[13] )

Instructions:
1. Copy & paste the code into a batch file of your choice. Remove the line
numbers.
2. The code assumes that your projects are kept in folders called
"c:/Project xxx" where xxx can be any character string. Modify line #3 if
this is not the case.
3. The drive letter used in lines #4 and #11 must be the same as the drive
letter used for Source in line #3.
4. Run the batch file as it is from a Command Prompt and examine its screen
output carefully.
5. If satisfied, modify line #5 to that it reads
set Action=
then run the batch file until it pauses for the first time. Check that it
has done the right thing.
6. If satisfied, remove line #12, then rerun the batch file.

Note: All care and no responsibility taken. You are responsible for backing
up your files before using this program.
 
L

liu

Flightless Bird
On Jun 12, 3:11 am, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "liu" <spamfree...@yahoo.com> wrote in message
>
> news:9feb6371-7331-41f7-b604-e41a20418119@18g2000vbi.googlegroups.com...
>
>
>
>
>
> >> > Thank you very much for the offer to help. Here is what I have:
> >> > Multiple projects each one with a runtime (many HTML, ASP, images
> >> > etc), In each project there is a Source folder named "Source".
> >> > I'd like to delete the runtime while keep the Source folder intact.
> >> > There are many images, etc files in the Source folder too. Basically,
> >> > delete everything in root except "Source" folder and the content
> >> > inside.

>
> >> > Thanks,

>
> >> Your description is still rather general. Please quote a specific example
> >> of
> >> what you wish to keep and what you wish to delete.

>
> > project a
> > project a\file1.htm
> > project a\file2.htm
> > ...
> > project a\file3.css
> > project a\file4.js
> > ...
> > project a\images\file5.jpg
> > ...
> > project a\Source\file6.fla
> > project a\Source\file7.fla
> > ....

>
> > I'd like to delete everything inside project a including folders
> > inside except Source folder. Keep everything inside the source folder
> > no matter what are inside.

>
> > project b
> > same structure but different files.

>
> > Do the same for the remaining 200 more projects with the same
> > structure.

>
> > Thanks for the help,

>
> OK, things are getting clearer. Try this:
> [01] @echo off
> [02] SetLocal EnableDelayedExpansion
> [03] set Source=c:/Project
> [04] set Target=c:/HoldingFolder
> [05] set Action=echo
> [06] for /d %%a in ("%Source%*.*") do (
> [07]    for %%b in ("%%a") do set Name=%%~na
> [08]    %Action% md   "%Target%\!Name!"
> [09]    %Action% move "%%a\Source" "%Target%\!Name!"
> [10]    %Action% rd   "%%a" /s /q
> [11]    %Action% move "%Target%\!Name!" C:/
> [12]    pause
> [13] )

Thank you for the script. I will give it try at work. I will try on a
duplicate first. So don't worry that I would delete files
unexpectedly.
 
L

liu

Flightless Bird
I tried it, but it didn't do anything.
I duplicated some of my project folders to c:/test and I created a
folder called "new" inside c:/test.
I copy, and revised, and saved it as domagic.bat. The text is below:

@echo off
SetLocal EnableDelayedExpansion
set Source=c:/tests
set Target=c:/tests\new
set Action=
for /d %%a in ("%Source%*.*") do (
for %%b in ("%%a") do set Name=%%~na
%Action% md "%Target%\!Name!"
%Action% move "%%a\Source" "%Target%\!Name!"
%Action% rd "%%a" /s /q
%Action% move "%Target%\!Name!" c:/

I typed domagic in CMD, but it goes to the next blank command prompt
right away.

??
Thanks for the help again.
 
P

Pegasus [MVP]

Flightless Bird
"liu" <spamfreeliu@yahoo.com> wrote in message
news:b037ef0c-31b5-41c1-9814-442d533427fd@h13g2000yqm.googlegroups.com...
> I tried it, but it didn't do anything.
> I duplicated some of my project folders to c:/test and I created a
> folder called "new" inside c:/test.
> I copy, and revised, and saved it as domagic.bat. The text is below:
>
> @echo off
> SetLocal EnableDelayedExpansion
> set Source=c:/tests
> set Target=c:/tests\new
> set Action=
> for /d %%a in ("%Source%*.*") do (
> for %%b in ("%%a") do set Name=%%~na
> %Action% md "%Target%\!Name!"
> %Action% move "%%a\Source" "%Target%\!Name!"
> %Action% rd "%%a" /s /q
> %Action% move "%Target%\!Name!" c:/
>
> I typed domagic in CMD, but it goes to the next blank command prompt
> right away.
>
> ??
> Thanks for the help again.


It's for situations like these that you must use the debugging mode that I
gave you. I also note that you dropped line 13. Can't do this! Here is the
original script again, this time with your folders. Leave Line 5 as it is
while debugging. Note also that you get into big problems when making the
target folder a subfolder of the source folder. They must be different!
[01] @echo off
[02] SetLocal EnableDelayedExpansion
[03] set Source=c:/Test
[04] set Target=c:/New
[05] set Action=echo
[06] for /d %%a in ("%Source%*.*") do (
[07] for %%b in ("%%a") do set Name=%%~na
[08] %Action% md "%Target%\!Name!"
[09] %Action% move "%%a\Source" "%Target%\!Name!"
[10] %Action% rd "%%a" /s /q
[11] %Action% move "%Target%\!Name!" C:/
[12] pause
[13] )
 
L

liu

Flightless Bird
Thanks again for the help.

I think it is missing a function to read the names of all the folders
inside the project folder.

I got:
md "c:/New\Tests"
move "c:/Tests\Source" "c:/New\Tests"
rd "c:/Tests" /s /q
move "c:/New\Tests" C:/

In fact, I have project folders inside Tests folder. Each project
folder has its Source folder.

What I need it to do:
md "c:/New\project a"
move "c:/Tests\project a\Source" "c:/New\project a"
rd "c:/Tests\project a" /s /q
move "c:/New\Tests\project a" C:/

then it will continue with project b, project c, etc...

So project name is missing in the path.

Thanks for the help,
 
P

Pegasus [MVP]

Flightless Bird
"liu" <spamfreeliu@yahoo.com> wrote in message
news:d3625860-6e34-4b03-9c8c-ed237f9ba9c4@a3g2000vbl.googlegroups.com...
> Thanks again for the help.
>
> I think it is missing a function to read the names of all the folders
> inside the project folder.
>
> I got:
> md "c:/New\Tests"
> move "c:/Tests\Source" "c:/New\Tests"
> rd "c:/Tests" /s /q
> move "c:/New\Tests" C:/
>
> In fact, I have project folders inside Tests folder. Each project
> folder has its Source folder.
>
> What I need it to do:
> md "c:/New\project a"
> move "c:/Tests\project a\Source" "c:/New\project a"
> rd "c:/Tests\project a" /s /q
> move "c:/New\Tests\project a" C:/
>
> then it will continue with project b, project c, etc...
>
> So project name is missing in the path.
>
> Thanks for the help,


This is getting too complex for me. Time to change direction - try this
batch file:
[1] @echo off
[2] set Source=C:/Tests
[3] set Key=\Source\
[4] set action=echo
[5] for /f "delims=" %%a in ('dir /s /b %Source% ^| find /i /v "%Key%"')
do %action% del "%%a"
[6] for /f "delims=" %%a in ('dir /s /b /ad %Source% ^| find /i /v "%Key%"')
do %action% rd /s /q "%%a"
 
A

Anthony Buckland

Flightless Bird
I'm chiming in at the end of what I imagine is
an extended conversation, but:

If the object is just what the subject line says, to
delete all of a large body of files except for one
folder in that body, then make the chosen folder
hidden, and make sure hidden files are not shown.
Then any deletion of the body as a whole will
leave the one hidden folder in existence, after
which it can be un-hidden whenever that becomes
desirable.

Or am I missing the point of the discussion?
 
P

Pegasus [MVP]

Flightless Bird
"Anthony Buckland" <anthonybucklandnospam@telus.net> wrote in message
news:D62dnavPPtzpu4TRnZ2dnUVZ_hednZ2d@giganews.com...
> I'm chiming in at the end of what I imagine is
> an extended conversation, but:
>
> If the object is just what the subject line says, to
> delete all of a large body of files except for one
> folder in that body, then make the chosen folder
> hidden, and make sure hidden files are not shown.
> Then any deletion of the body as a whole will
> leave the one hidden folder in existence, after
> which it can be un-hidden whenever that becomes
> desirable.
>
> Or am I missing the point of the discussion?


No, you're not and I love this bit of lateral thinking!
 
S

surferdude2

Flightless Bird
Consider using the freeware 'XXCOPY'
(http://www.xxcopy.com/xcpymain.htm) to do that job.

I created a structure similar to what you described and did a quick
batch file. It seems to do what you want:


Code:
--------------------
xxcopy /clone /yy F:/projects\project???\source\*.* F:/projectsxxcopy
xxcopy /clone /yy F:/projectsxxcopy F:/projects\
RD F:/projectsxxcopy /s /q
--------------------


Use quotes if you have embedded spaces in your path.

It simply copies all project folders to an intermediate folder and then
clones that folder back to the original, thus deleting all files and
folders except those named projects and source.

xxcopy is a great time saver for situations like yours.
 
Top