1.

Solve : DOS BatchFile for deleting file by size in many sub directories?

Answer»

I need help on this batch file...
I have a mainSub directory with many sub directories and sub-sub directories. I need to delete all the files less than 500 bites in this mainSub directory and in all the sub and sub-sub directories it contains.
I wrote a code as follows:

FOR %%F IN (C:\MaAbey\ *.*)s DO (
IF %%~zF LSS 500 DEL %%F
)
pause

This could delete all  the files less than 500 bites in MaAbey mainSub directory but not in its sub and sub-sub directories.

Please help me to adjust the code to delete all the files in all the sub, sub-sub directories which are less the 500 bites.

Thank you

Mahindause dir /s . check dir /? for more info.
another way, download findutils (see my sig) and use the GNU find command
Code: [Select]c:\test> find c:\tmp -type f -size -500c  -delete
WARNING - YOU ARE SHOOTING BLIND

The first line of your code is so WRONG.
Firstly you must remove the lower case 's' - DOS will not like it.
Then you will lose all the vital *.ini files, and ANYTHING else in your CURRENT DRIVE AND DIRECTORY ! !
I show in the code box below your version followed by a corrected version :-
Code: [Select]FOR %%F IN (C:\MaAbey\ *.*)s DO (
FOR %%F IN (C:\MaAbey\*.*) DO (

The corrected version will only process the files within C:\MaAbey\
Your version will try (and fail) to delete the ENTIRE folder with all its contents,
then it will actually delete all small files that are located in WHATEVER is your current directory and drive.

DO NOT USE DEL IF YOU DO NOT KNOW WHAT YOU ARE DELLING.

I strongly recommend that you replace "DEL %%F" with
Quote

DIR %%F >> To_Zap.txt
and/or
Quote
ECHO %%F >> To_Zap.txt
That will give a file, To_Zap.txt, which you can scrutinise at your leisure to decide if the potential victims should be subjected to DEL, or if there are further mistakes to be corrected before you need to reinstall WINDOWS ! !

I will not address the mechanisms for recursion through the sub-directories, I can safely leave that to others.  My priority is to warn/guide you so that you do not destroy vital files.

Regards
Alan
Thanks Alan and thanks ...0g74

I am using windows xp and sometimes work with the dos command prompt. So, ..0g74's UNIX like codes cannot be used I think?

Alan, thanks for the advise, I know exactly what is this SubMain directory. They are data files specially created to test the code. The 's' was a mistake, in my code ran it was not there.

I adjust the code with /S switch. But I could not get any good result to my question. It ask before deleting entire MaAbey directory 'are you sure, confirm, Y/N'. I NEVER wanted to delete entire directory.

for %%F IN (C:\MaAbey\*.* /s) DO (if %%~zF LSS 100 DEL %F) pause
pause

Quote from: mabeykoon on June 15, 2009, 02:38:51 PM
I am using windows xp and sometimes work with the dos command prompt. So, ..0g74's UNIX like codes cannot be used I think?
no you are wrong. The better and more useful find command in Unix has been ported to windows through GNU.


Discussion

No Comment Found