How to delete from all folders "certain files"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How to delete from all folders "certain files"

#1 Post by alan_b » 04 May 2014 10:23

The perfect solution is to remove from each folder all excepting the smallest file

An adequate solution is to remove from each folder every file above a certain size.

My reason is that I wish to have a SMALL archive of a partition with very unusual characteristics,
and after much investigation I think the problem is the folder structure and NOT the files,
and I suspect the possibility that some DOS commands and Windows API's and third party utilities,
may handle an EMPTY folder differently than a folder with one or more files,
so I expect to preserve the unusual characteristics if I preserve at least one file.

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to delete from all folders "certain files"

#2 Post by foxidrive » 04 May 2014 10:31

The reasoning isn't clear, but You want to keep the smallest file in every folder in a folder tree?

Test this:

Code: Select all

@echo off
for /d /r "d:\base\folder"  %%a in (*) do (
   pushd "%%a"
      for /f "skip=1 delims=" %%b in ('dir /b /os /a-d ') do del "%%b"
   popd
)

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to delete from all folders "certain files"

#3 Post by Compo » 04 May 2014 13:54

I'm not sure if you've thought of this, (it seems to me to be a quicker way of potentially proving your theory). If you use RoboCopy with the /Create switch it should copy the entire folder structure and include every file, (it will simply create those files at 0 bytes)!

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to delete from all folders "certain files"

#4 Post by alan_b » 06 May 2014 06:21

@foxidrive
Thanks - that worked well - too well and the train jumped off the rails :cry:

I placed the script in R:\Drive_D\VHD and tweaked it to work from the root of P:\.
It had a lot of work to do and after waiting a while I hit Ctrl'C to see what was happening.
Then I let it resume and it started telling me of files it could not delete from R:\Drive_D\VHD because they were in use.

I think that when the script resumed, the pushd and popd may have become unbalanced,
and the files in use were my running script and the *.VHD files that were attached as Virtual Hard Drives - one of which was P:\
All my other experimental files *.VHD files etc in R:\Drive_D\VHD were deleted.

Fortunately there was nothing really important,
AND I am currently Beta testing some fantastic Data Recovery software which has fully recovered 18 files,
and there are only 3 missing files and these I can quickly recreate should I wish.


@Compo
Thanks, BUT I actually suspect the problem is related to the MFT of an NTFS partition having erroneous artifacts
and when a partition image backup is restored those artifacts are resulting strange consequences related to the existing folder structure.
Deleting a file will delete some sort of entry in the MFT, but hopefully the folder artifacts will not be disturbed.
I fear that replacing a deleted file with a zero byte file might cause greater MFT disturbance.
N.B. My current interest is in the nature of the artifacts, not in eradicating them - that comes later :)

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to delete from all folders "certain files"

#5 Post by foxidrive » 06 May 2014 06:38

FWIW I've never used control C on a batch file unless I wanted to abort it.
If you need to keep track of progress in a batch file then just add an echo command.


The old maxim is true - files that you don't have backed up are only temporary files.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to delete from all folders "certain files"

#6 Post by alan_b » 07 May 2014 11:39

Help, this gives me a problem :-

Code: Select all

for /d /r "d:\base\folder"  %%a in (*) do (
)

It "Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree"
AND THEN IT JUMPS TO ANOTHER DRIVE
and continues to execute the delete commands there also.

If possible I would like a "code tweak" that only acts upon the contents of directories and not upon the destinations of Reparse Points.

This demonstrates that CMD.EXE is aware that "Pale Moon" is a reparse point with a destination on drive D:\
but once it has walked into that it thinks that this is a valid target
C:\Users\Alan\AppData\Roaming\Moonchild Productions\Pale Moon\profiles.ini
and the only clue appears to be that Free Space has fallen from
43,100,663,808 bytes free down to
9,659,375,616 bytes free

Code: Select all

C:\Users\Alan\AppData\Roaming\Moonchild Productions>DIR
 Volume in drive C is C_OCZ_System
 Volume Serial Number is 6E59-9E7D

 Directory of C:\Users\Alan\AppData\Roaming\Moonchild Productions

26/10/2012  22:13    <DIR>          .
26/10/2012  22:13    <DIR>          ..
26/10/2012  21:53    <JUNCTION>     Pale Moon [D:\Junctions\PaleMoon\Alan\Roaming\Pale Moon]
               0 File(s)              0 bytes
               3 Dir(s)  43,100,663,808 bytes free

C:\Users\Alan\AppData\Roaming\Moonchild Productions>CD PALE MOON

C:\Users\Alan\AppData\Roaming\Moonchild Productions\Pale Moon>DIR
 Volume in drive C is C_OCZ_System
 Volume Serial Number is 6E59-9E7D

 Directory of C:\Users\Alan\AppData\Roaming\Moonchild Productions\Pale Moon

29/05/2012  21:45    <DIR>          .
29/05/2012  21:45    <DIR>          ..
29/05/2012  21:45    <DIR>          Profiles
10/03/2012  18:23               111 profiles.ini
               1 File(s)            111 bytes
               3 Dir(s)   9,659,375,616 bytes free

C:\Users\Alan\AppData\Roaming\Moonchild Productions\Pale Moon>


Regards
Alan

Post Reply