Delete all files except one folder

  • Thread starter Thread starter liu
  • Start date Start date
L

liu

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,
 
liu said:
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
 
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.
 
liu said:
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.
 
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,
 
liu said:
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.
 
liu said:
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
 
liu said:
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
 
Thank you very much for the offer to help. Here is what I have:
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,
 
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!
 
liu said:
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.
 
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.
 
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.
 
liu said:
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] )
 
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,
 
liu said:
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"
 
Anthony Buckland said:
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!
 
--------------------
    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.

It worked. THANKS A LOT!!!
 
surferdude2 said:
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.

I think shools should test students for criminal apptiude so they know they
aren't cut out for it and can lead more productive lives. Doubtful time in a
correctional facility will cure you and the citizens will be paying for your
keep the rest of your life. Wasting other peoples lives too, your family
included. I'd say 240grains of lead is the only thing to cure the two of
you. Don't really care if that's not what the two of you are doing.
Spreading this kind of crap makes you guilty too.

http://www.missingkids.com/

Anyone still wondering why newsgroups are going away.
 
Back
Top