Computer Hope

Microsoft => Microsoft Windows => Windows NT/2000 => Topic started by: mdibarra on December 07, 2010, 08:52:05 AM

Title: Deleting files equal to 1KB in a batch file
Post by: mdibarra on December 07, 2010, 08:52:05 AM
How to create a script to delete files (ej.: *.xml or *.html) equal to 1KB in batch file?
Title: Re: Deleting files equal to 1KB in a batch file
Post by: Salmon Trout on December 07, 2010, 09:28:21 AM
Did you mean files whose size is exactly 1024 bytes? Not more? Not less?

Title: Re: Deleting files equal to 1KB in a batch file
Post by: mdibarra on December 07, 2010, 09:34:42 AM
I refer to files that are 1KB, may be more or less bytes. I do not know much about the subject of bytes.
thanks for you reply...
Title: Re: Deleting files equal to 1KB in a batch file
Post by: Salmon Trout on December 07, 2010, 10:44:34 AM
1024 bytes = 1 kilobyte = 1 KB.

I ask again. Do you want to only delete files that are 1 KB in size? (Did you mean size?)
Title: Re: Deleting files equal to 1KB in a batch file
Post by: mdibarra on December 07, 2010, 10:55:53 AM
only the files of size 1KB
Title: Re: Deleting files equal to 1KB in a batch file
Post by: Salmon Trout on December 07, 2010, 12:36:09 PM
Code: [Select]
@echo off
REM example
set mask=*.xml
for /f "delims=" %%A in ('dir /b %mask%') do if %%~zA equ 1024 del "%%~dpnxA"
Title: Re: Deleting files equal to 1KB in a batch file
Post by: mdibarra on December 07, 2010, 01:15:05 PM
D:\Temp>dir
 Volume in drive D has no label.
 Volume Serial Number is 1C5A-305C

 Directory of D:\Temp

12/07/2010  17:07    <DIR>          .
12/07/2010  17:07    <DIR>          ..
12/07/2010  11:45             2,873 1.xml
12/07/2010  11:46            25,857 10.xml
09/13/2010  13:15               115 2.xml
12/07/2010  11:45             2,873 3.xml
09/13/2010  13:15               115 4.xml
12/07/2010  11:45             2,873 5.xml
09/13/2010  13:15               115 6.xml
12/07/2010  11:45             2,873 7.xml
09/13/2010  13:15               115 8.xml
12/07/2010  11:45             2,873 9.xml
12/07/2010  17:07                93 del.bat
              11 File(s)         40,775 bytes
               2 Dir(s)  14,180,757,504 bytes free

D:\Temp>del.bat

D:\Temp>set mask=*.xml

D:\Temp>for /F "delims=" %A in ('dir /b *.xml') do if %~zA EQU 1024 del "%~dpnxA"
File Not Found

D:\Temp>

Files 2.xml, 4.xml, 6.xml, 8.xml are files of 1KB to windows.
Title: Re: Deleting files equal to 1KB in a batch file
Post by: Salmon Trout on December 07, 2010, 01:36:12 PM
Files 2.xml, 4.xml, 6.xml, 8.xml are files of 1KB to windows.

In the Details view, Windows Explorer rounds file sizes, roughly to the nearest 1 KB. Files under 1024 bytes are shown as "1 KB". The file 2.xml is in fact 115 bytes in size as you can see. so are 4, 6 and 8.xml.


Quote
File Not Found

Is the batch file in the same folder?


Title: Re: Deleting files equal to 1KB in a batch file
Post by: mdibarra on December 07, 2010, 01:40:20 PM
Yes, the batch file name is del.bat
Title: Re: Deleting files equal to 1KB in a batch file
Post by: Salmon Trout on December 07, 2010, 01:45:38 PM
Yes, the batch file name is del.bat

And the .xml files are in the same folder? Are they hidden, system or read-only? What happens if you run this batch in that folder?


Code: [Select]
@echo off
echo.
echo This file is %~dpnx0
echo.
echo 1
for /f "delims=" %%A in ('dir /b *.*') do echo File %%A %%~zA bytes
echo.
echo 2
echo.
for /f "delims=" %%A in ('dir /b *.xml') do echo File %%A
echo.
echo Finished