1.

Solve : How to create batch file to delete files from subdirectories?

Answer»

Hi,
I was trying to create a batch file, that deletes only files with extention "TMP" from a directory and all its subdirectories.
None of the subdirectories should be deleted. I couldnt do this.
Can any one help me? I figured it out, you just have to give,
DEL /S *.tmp
to delete all the files in the subdirectories with extention tmp.
But now I have another issue, it deletes files with *.tmpl also,
I think the problem is DOS matches only 3 character extensions
Can any one suggest me a work around?what version of DOS are you using?

The behaviour you describe does not happen in Windows XP.

BEFORE

Quote


dir /b /s

F:\test\wildcard\text.tmpl
F:\test\wildcard\text.tmpm
F:\test\wildcard\text.tmpn
F:\test\wildcard\text.tmp
F:\test\wildcard\subdir\text.tmpl
F:\test\wildcard\subdir\text.tmpm
F:\test\wildcard\subdir\text.tmpn
F:\test\wildcard\subdir\text.tmp

dir /b /s *.tmp

F:\test\wildcard\text.tmp
F:\test\wildcard\subdir\text.tmp

dir /b /s *.tmp*

F:\test\wildcard\text.tmpl
F:\test\wildcard\text.tmpm
F:\test\wildcard\text.tmpn
F:\test\wildcard\text.tmp
F:\test\wildcard\subdir\text.tmpl
F:\test\wildcard\subdir\text.tmpm
F:\test\wildcard\subdir\text.tmpn
F:\test\wildcard\subdir\text.tmp


Quote

DEL /S *.tmp
Deleted file - F:\test\wildcard\text.tmp
Deleted file - F:\test\wildcard\subdir\text.tmp


AFTER

Quote

dir /b /s

F:\test\wildcard\text.tmpm
F:\test\wildcard\text.tmpn
F:\test\wildcard\text.tmpl
F:\test\wildcard\subdir\text.tmpm
F:\test\wildcard\subdir\text.tmpn
F:\test\wildcard\subdir\text.tmpl

dir /b /s *.tmp

File Not Found

dir /b /s *.tmp*

F:\test\wildcard\text.tmpm
F:\test\wildcard\text.tmpn
F:\test\wildcard\text.tmpl
F:\test\wildcard\subdir\text.tmpm
F:\test\wildcard\subdir\text.tmpn
F:\test\wildcard\subdir\text.tmpl



Thanks for your response. I am using the DOS version that comes with Windows XP Pro SP2.
I dont know why my computer is acting in this manner
PASTING the command i executed in DOS:
Quote
E:\Shared\Test>dir
Volume in drive E is New Volume
Volume Serial Number is C451-6686

Directory of E:\Shared\Test

07/27/2007 12:25 AM <DIR> .
07/27/2007 12:25 AM <DIR> ..
07/27/2007 12:24 AM 0 test.tmp
07/27/2007 12:24 AM 0 test.tmpl
07/27/2007 12:24 AM 0 test.tmpm
07/27/2007 12:24 AM 0 test.tmpp
4 File(s) 0 bytes
2 Dir(s) 34,714,984,448 bytes free

E:\Shared\Test>del /s *.tmp
Deleted file - E:\Shared\Test\test.tmp
Deleted file - E:\Shared\Test\test.tmpl
Deleted file - E:\Shared\Test\test.tmpm
Deleted file - E:\Shared\Test\test.tmpp
Can you please help me out of this?this is very strange. I am also using XP Pro SP2. I would be surprised if this is a feature of XP Home. What happens when you type VER?

Quote

F:\test\wildcard>ver

Microsoft Windows XP [Version 5.1.2600]

F:\test\wildcard>dir /s *.t*
Volume in drive F is Various
Volume Serial Number is 4CCF-8F52

Directory of F:\test\wildcard

26/07/2007 18:39 8 text.tmpm
26/07/2007 18:39 8 text.tmpn
26/07/2007 18:39 8 text.tmpl
3 File(s) 24 bytes

Directory of F:\test\wildcard\subdir

26/07/2007 18:39 8 text.tmpm
26/07/2007 18:39 8 text.tmpn
26/07/2007 18:39 8 text.tmpl
3 File(s) 24 bytes

Total Files Listed:
6 File(s) 48 bytes
0 Dir(s) 26,841,501,696 bytes free

F:\test\wildcard>del /s *.tmp
Could Not Find F:\test\wildcard\*.tmp

Really weird. The version is same,
Quote
E:\Shared\Test>ver

Microsoft Windows XP [Version 5.1.2600]

E:\Shared\Test>

Version of C:\WINDOWS\system32\cmd.exe is 5.1.2600.2180You could try asking in Google Groups in alt.msdos.batch.nt. That is where the real experts live.
Thanks for giving me this information about google groups. I have posted the question and WAITING for the reply. I have the answer to why we had different results. The behaviour you experienced happens if you are using the FAT32 filesystem. It does not happen if you are using the NTFS filesystem.

I tried the same operation on a FAT32 partition I have for use with Norton Ghost, and it happened just like you described.

Quote

D:\>del /s *.tmp
Deleted file - D:\wildcard\text.tmpl
Deleted file - D:\wildcard\text.tmpm
Deleted file - D:\wildcard\text.tmpn
Deleted file - D:\wildcard\subdir\text.tmpl
Deleted file - D:\wildcard\subdir\text.tmpm
Deleted file - D:\wildcard\subdir\text.tmpn


So, convert to NTFS for better, more predictable file name handling!

Have you got some special reason for sticking with FAT32?


Thanks a lot... i was loosing my sleep becoz of this
I tried in NTFS file system, and u r rite.. it works fine.

Actually the system where i need to run this batch file is NTFS(luckly)
I was trying it out in my system which was UNFORTUNATELY FAT32.

But that was really a good piece of knowledge.
Again thanks a lot!!if you had to do this on FAT32 you would have to use some batch tricks to filter out all the .tmpl etc extensions. An INTERESTING little project perhaps... but not tonight.



Discussion

No Comment Found