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