How do I effectively delete several files depending on their positions(edit: or retrieve the files by their number) in the current folder?
Preferably I need a solution with a normal .bat file.
The question is how to define more than one value for a set event(not only one value as shown in my example code below) and instead of defining multiple byte values, the new values should reference the positions of files in a folder.
for example(image below) delete the files in the folder at position 2, 11, 12, 14.


:: Size is in bytes
set "equal.size=20725"
for /f  "usebackq delims=;" %%A in ('dir /b /A:-D *.*') do If %%~zA EQU %equal.size% del "%%A"

Alternatively I could also name the files in their folders like this:


What's an effective method to delete multiple files, calling their numbers?
Can the above .bat code be easily extended/changed in that direction?
(using an Array or similar solution in one .bat? - I don't need information about how to delete files from a list of numbers).

  • 1
    you could just use sortorder in your dir command and define the output into an array, than perform actions on files according to the array index (File position in your chosen sort order) – T3RR0R May 17 at 14:06
  • 2
    May I ask why you need position and not some other determination method? – thex May 17 at 14:27
  • Well, determination method could be file names too in my case - with a small change in my workflow I can have the 77 files named as numbers 1-77. What I need is the deletion of all fully transparent("empty") .png tiles from multiple folders and the file size for these could vary, but the specific positions or numbers are always the same. – atereou May 17 at 15:12
  • File order in the directory is not what you seel, the underlying command grabs the names, sorts them and shows the result. – vonbrand May 17 at 17:01
  • What do you mean with position the way they are classified in File Explorer? – Ricardo Bohner May 17 at 21:58


Obs.: Also considering your comment:

No problem.
In the folder are .png tiles - 3 types basically(ABC):
some of them are filled completely with colored pixels(A)
some are only half filled with pixels(B)
and others are not filled with pixels at all(C).
Currently, your script deletes all C and B, but it should only delete all C.

If the current script is deleting both B and C, we have two things to test:

If in |findstr /b "99\. 100\."

  1. rem ::  where B and C :
    B == 99\.
    C == 100\.
    rem :: The findstr command would be with || 
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul || echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "100\." >nul || echo=del /q /f "%%~fi"
    rem :: The findstr command would by with && 
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul && echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "100\." >nul && echo=del /q /f "%%~fi"
  2. rem ::  where B and C :
    B == 100\.
    C == 99\.
    rem :: The findstr command would be with ||
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul || echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\." >nul || echo=del /q /f "%%~fi"
     rem :: The findstr command would be with &&
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul && echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\." >nul && echo=del /q /f "%%~fi"
  3. rem ::  where C :
    C == 0%.
    rem :: The findstr command would be with ||
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul || echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "0%" >nul || echo=del /q /f "%%~fi"
     rem :: The findstr command would be with &&
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul && echo=del /q /f "%%~fi"
    ')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "0%" >nul && echo=del /q /f "%%~fi"

Considering your comment:

  • Well, determination method could be file names too in my case - with a small change in my workflow I can have the 77 files named as numbers 1-77. What I need is the deletion of all fully transparent("empty") .png tiles from multiple folders and the file size for these could vary, but the specific positions or numbers are always the same.

You may need a third tool to check your files and, consequently, delete it if True in each case.

So, you can use ImageMagick, which can read the file and verify that it has an opacity/transparency frame:

@echo off && cd /d "%~pd0"

Set "_Path_to_Yours_Files=%userprofile%\Pictures\Seletive_PNGs"
Set "_Path_to_ImageMagick=C:\Program Files\ImageMagick-7.0.10-Q16-HDRI"

for /f %%i in ('%__APPDIR__%where.exe "%_Path_to_Yours_Files%:*.png"
')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%% fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul || echo=del /q /f "%%~fi"

%__APPDIR__%timeout.exe -1

After checking the output, just remove the echo= to effectively delete the files:

')do "%_Path_to_ImageMagick%\magick.exe" "%%~fi" -channel a -separate -format "%%[fx:100.00*mean]%%" info:|findstr /b "99\.  100\." >nul || echo=del /q /f "%%~fi"

Obs.: For testing purposes of this script, I downloaded ImageMagick-7.0.10-13-Q16-HDRI-x64-static.exe

| improve this answer | |
  • Ok, so "True" would delete all FULLY NON opaque images and keep all images which are FULLY opaque. For the opposite result it must be set to "False". Thanks, this was an efficient solution. – atereou May 18 at 15:39
  • No, what I said was actually wrong. It's true what I said about "True", but "False" would not be the exact opposite result. "False" would also delete all tiles with ANY FULLY opaque parts. – atereou May 18 at 15:55
  • Hm, could you write the complete line again please? I'm not sure if I did something wrong, but currently my adjustments will return File not found. – atereou May 18 at 16:17
  • Ok. Now this would delete all other tiles and the remaining files would be completely "empty" and partly "empty". What it should do, is delete all completely "empty" only and keep the partly "empty" and the other tiles. (for convenience and better understanding I say "empty" and this is related to opacity, not file size) – atereou May 18 at 17:19
  • That's it almost - but imagemagick related in the code would also delete the tiles which are "partly empty". If your solution could keep these images it would be perfect. – atereou May 18 at 17:48

If the naming convention is known, this is a very simple task.

@Echo off
Set "No.File=INFO: Could not find files for the given pattern(s)."
Set "Remove#=2 4 8 16 32 64" & REM Arbitrary index.
Set "Ext=Extension type to act upon"
::: - %%I Iterate over index of known file numbers of defined Extension type
::: - %%O Retrieve full path of file If exists using 'where' and Delete 
::: - (Nested Loops) 2>Nul Redirect to STDERR when file does not exist.
(For %%I in (%Remove#%) Do For /F "UsebackQ Tokens=* Delims=" %%O In (`"where *_%%I.%Ext%"`) Do If Not "%%~O" == "%No.File%" Del /Q %%O) 2>Nul
REM _ or other 'unique' string prior to file number should be used to ensure pattern is matched to the correct file number

As for operating the file accross multiple directories, depending on the circumstances there's a few ways you could modify the above.

  • Turn it into a subroutine and Call it from a for loop that iterates over each directory
  • Add an additional for Loop to the outside of the nest to iterate over directories
  • If the folders exist within a singular parent folder, modify the 'Where' command to include the /R recursive switch, followed by the Directory Path:
    • Where /R C:\Path_To\ParentDirectory *_%%I.%Ext%
| improve this answer | |

PowerShell is much better & less cryptic for this type of thing.

To create a collection of files of a specific type and assign a number to each, the following code works:

$Folder = 'c:\Users\Keith\Screenshots'
Get-ChildItem $Folder *.png | ForEach-Object {$i=1}{
        Index = $i++ ;
        Path  = $_.FullName

It will produce a collection similar to the following:

Index Path                                                                     
----- ----                                                                     
    1 C:\Users\Keith\Screenshots\Add user to Permissions.PNG                   
    2 C:\Users\Keith\Screenshots\Apply to Folders.png                          
    3 C:\Users\Keith\Screenshots\ArrangeBy.png                                 
    4 C:\Users\Keith\Screenshots\Attributes - ReadOnly.png                     
    5 C:\Users\Keith\Screenshots\Attributes Column Added.png                   
    6 C:\Users\Keith\Screenshots\Attributes Column.png                         
    7 C:\Users\Keith\Screenshots\Attributes Detail.png                         
    8 C:\Users\Keith\Screenshots\background on-off.png                         
    9 C:\Users\Keith\Screenshots\Capture.PNG                                   
   10 C:\Users\Keith\Screenshots\Command wrapped Shortcut.PNG                  
   11 C:\Users\Keith\Screenshots\Context Bacground.png                         
   12 C:\Users\Keith\Screenshots\Context Foreground.png                        
   13 C:\Users\Keith\Screenshots\Date Column Precedence.png                    
   14 C:\Users\Keith\Screenshots\dd-MM-yy.PNG                                  
   15 C:\Users\Keith\Screenshots\Deny Delete Permissions.PNG                   
   16 C:\Users\Keith\Screenshots\Display Scaling.png                           
   17 C:\Users\Keith\Screenshots\Display.PNG                                   
   18 C:\Users\Keith\Screenshots\Downloads FolderTyype Topview reg settings.png
   19 C:\Users\Keith\Screenshots\Explorer Taskbar Thumbnail Context Menu.png   
   20 C:\Users\Keith\Screenshots\FIle Type Different Display.png               
   21 C:\Users\Keith\Screenshots\Folder OPtions 1.PNG                          
   22 C:\Users\Keith\Screenshots\Folder options 2.PNG                          
   23 C:\Users\Keith\Screenshots\Folder Properties Customize tab.png           
   24 C:\Users\Keith\Screenshots\FolderTypes.png                               
   25 C:\Users\Keith\Screenshots\ForumLayoutWrong.PNG                          
   26 C:\Users\Keith\Screenshots\GPS Columns Added.png                         
   27 C:\Users\Keith\Screenshots\GPS Columns Available.png                     
   28 C:\Users\Keith\Screenshots\HKCR Namespace Venn Diagram.png               
   29 C:\Users\Keith\Screenshots\IDLIist_Absolute.png                          
   30 C:\Users\Keith\Screenshots\Inheritance.png                               
   31 C:\Users\Keith\Screenshots\JumpList Customized.png                       
   32 C:\Users\Keith\Screenshots\JumpList Default.png                          
   33 C:\Users\Keith\Screenshots\Junction Warning.PNG                          
   34 C:\Users\Keith\Screenshots\Libraries.png                                 
   35 C:\Users\Keith\Screenshots\Library.png                                   
   36 C:\Users\Keith\Screenshots\Location tab.png                              
   37 C:\Users\Keith\Screenshots\MyStats.PNG                                   
   38 C:\Users\Keith\Screenshots\ow1.png                                       
   39 C:\Users\Keith\Screenshots\ow2.png                                       
   40 C:\Users\Keith\Screenshots\ow3.PNG                                       
   41 C:\Users\Keith\Screenshots\Permission.PNG                                
   42 C:\Users\Keith\Screenshots\Permissions 1.PNG                             
   43 C:\Users\Keith\Screenshots\Permissions 2.PNG                             
   44 C:\Users\Keith\Screenshots\Permissions 3.PNG                             
   45 C:\Users\Keith\Screenshots\Permissions 4.PNG                             
   46 C:\Users\Keith\Screenshots\Permissions 5.PNG                             
   47 C:\Users\Keith\Screenshots\QuickStop.png                                 
   48 C:\Users\Keith\Screenshots\Read-only.png                                 
   49 C:\Users\Keith\Screenshots\Repace Permissions.png                        
   50 C:\Users\Keith\Screenshots\Replace Owner.PNG                             
   51 C:\Users\Keith\Screenshots\Reset Folders.png                             
   52 C:\Users\Keith\Screenshots\SaulsalitStats.PNG                            
   53 C:\Users\Keith\Screenshots\Screenshot (1).png                            
   54 C:\Users\Keith\Screenshots\Screenshot (10).png                           
   55 C:\Users\Keith\Screenshots\Screenshot (100).png                          
   56 C:\Users\Keith\Screenshots\Screenshot (101).png                          
   57 C:\Users\Keith\Screenshots\Screenshot (102).png                          
   58 C:\Users\Keith\Screenshots\Screenshot (103).png                          
   59 C:\Users\Keith\Screenshots\Screenshot (104).png                          
   60 C:\Users\Keith\Screenshots\Screenshot (105).png                          
   61 C:\Users\Keith\Screenshots\Screenshot (106).png                          
   62 C:\Users\Keith\Screenshots\Screenshot (107).png                          
   63 C:\Users\Keith\Screenshots\Screenshot (108).png                          
   64 C:\Users\Keith\Screenshots\Screenshot (109).png                          
   65 C:\Users\Keith\Screenshots\Screenshot (11).png                           
   66 C:\Users\Keith\Screenshots\Screenshot (110).png                          
   67 C:\Users\Keith\Screenshots\Screenshot (111).png                          
   68 C:\Users\Keith\Screenshots\Screenshot (112).png                          
   69 C:\Users\Keith\Screenshots\Screenshot (113).png                          
   70 C:\Users\Keith\Screenshots\Screenshot (114).png                          
   71 C:\Users\Keith\Screenshots\Screenshot (115).png                          
   72 C:\Users\Keith\Screenshots\Screenshot (116).png                          
   73 C:\Users\Keith\Screenshots\Screenshot (117).png                          
   74 C:\Users\Keith\Screenshots\Screenshot (118).png                          
   75 C:\Users\Keith\Screenshots\Screenshot (119).png                          
   76 C:\Users\Keith\Screenshots\Screenshot (12).png                           
   77 C:\Users\Keith\Screenshots\Screenshot (120).png                          
   78 C:\Users\Keith\Screenshots\Screenshot (121).png                          
   79 C:\Users\Keith\Screenshots\Screenshot (122).png                          
   80 C:\Users\Keith\Screenshots\Screenshot (123).png                          
   81 C:\Users\Keith\Screenshots\Screenshot (124).png                          
   82 C:\Users\Keith\Screenshots\Screenshot (125).png                          
   83 C:\Users\Keith\Screenshots\Screenshot (126).png                          
   84 C:\Users\Keith\Screenshots\Screenshot (127).png                          
   85 C:\Users\Keith\Screenshots\Screenshot (128).png                          
   86 C:\Users\Keith\Screenshots\Screenshot (129).png                          
   87 C:\Users\Keith\Screenshots\Screenshot (13).png                           
   88 C:\Users\Keith\Screenshots\Screenshot (130).png                          
   89 C:\Users\Keith\Screenshots\Screenshot (131).png                          
   90 C:\Users\Keith\Screenshots\Screenshot (132).png                          
   91 C:\Users\Keith\Screenshots\Screenshot (133).png                          
   92 C:\Users\Keith\Screenshots\Screenshot (134).png                          
   93 C:\Users\Keith\Screenshots\Screenshot (135).png                          
   94 C:\Users\Keith\Screenshots\Screenshot (136).png                          
   95 C:\Users\Keith\Screenshots\Screenshot (137).png                          
   96 C:\Users\Keith\Screenshots\Screenshot (138).png                          
   97 C:\Users\Keith\Screenshots\Screenshot (139).png                          
   98 C:\Users\Keith\Screenshots\Screenshot (14).png                           
   99 C:\Users\Keith\Screenshots\Screenshot (140).png                          
  100 C:\Users\Keith\Screenshots\Screenshot (141).png                          
  101 C:\Users\Keith\Screenshots\Screenshot (142).png                          
  102 C:\Users\Keith\Screenshots\Screenshot (143).png                          
  103 C:\Users\Keith\Screenshots\Screenshot (144).png                          
  104 C:\Users\Keith\Screenshots\Screenshot (145).png                          
  105 C:\Users\Keith\Screenshots\Screenshot (146).png                          
  106 C:\Users\Keith\Screenshots\Screenshot (147).png                          
  107 C:\Users\Keith\Screenshots\Screenshot (148).png                          
  108 C:\Users\Keith\Screenshots\Screenshot (149).png                          
  109 C:\Users\Keith\Screenshots\Screenshot (15).png                           
  110 C:\Users\Keith\Screenshots\Screenshot (150).png                          
  111 C:\Users\Keith\Screenshots\Screenshot (151).png                          
  112 C:\Users\Keith\Screenshots\Screenshot (152).png                          
  113 C:\Users\Keith\Screenshots\Screenshot (153).png                          
  114 C:\Users\Keith\Screenshots\Screenshot (154).png                          
  115 C:\Users\Keith\Screenshots\Screenshot (155).png                          
  116 C:\Users\Keith\Screenshots\Screenshot (156).png                          
  117 C:\Users\Keith\Screenshots\Screenshot (157).png                          
  118 C:\Users\Keith\Screenshots\Screenshot (158).png                          
  119 C:\Users\Keith\Screenshots\Screenshot (159).png                          
  120 C:\Users\Keith\Screenshots\Screenshot (16).png                           
  121 C:\Users\Keith\Screenshots\Screenshot (160).png 

With this collection, we can easily select files by their index/position:

$FileNums = @(2,11,12,14)
$Folder = 'c:\Users\Keith\Screenshots'
Get-ChildItem $Folder *.png | ForEach-Object {$i=1}{
        Index = $i++ ;
        Path  = $_.FullName
} | Where Index -in $FileNums

This gives us the followiong output:

Index Path
----- ----
    2 C:\Users\Keith\Screenshots\Apply to Folders.png
   11 C:\Users\Keith\Screenshots\Context Bacground.png
   12 C:\Users\Keith\Screenshots\Context Foreground.png
   14 C:\Users\Keith\Screenshots\dd-MM-yy.PNG

Since our goal is deletion of files, we can pipe these to Remove-Item. The reason we mapped 'FullName' to 'Path' is to match the value to Remove-Item's -Path parameter, which accepts pipeline input.

You can run the following code safely to test if it finds the right files. If it does, delete the -whatif parameter from the Remove-Item cmdlet

$FileNums = @(2,11,12,14)
Get-ChildItem $Folder *.png | ForEach-Object {$i=1} {
        Index = $i++ ;
        Path  = $_.FullName
} | Where Index -in $FIleNums | Remove-Item -whatif
| improve this answer | |
  • With this PowerShell solution the output in ISE Window is not equal to the specified values in the script for some reason. Can you explain the rules here and how would you set a specific fileext to ignore other files in the folder? – atereou May 18 at 14:48
  • 1
    Oddly enough that appears equivalent to the batch solution, and I can't particularly say it appears any less cryptic – T3RR0R May 18 at 16:11
  • @atereou: I'm not sure what you mean by "...output in ISE Window is not equal to the specified values in the script for some reason". I'll edit my answer to offer some explanation. I'm wondering if you're not seeing expected results bacause filenames with digits sort differently in PowerShell/cmd vs. Explorer. – Keith Miller May 18 at 18:37
  • @T3RR0R: I hope you have previous experience with batch & didn't learn it just to provide your answer. So if you're familar with batch & unfamilar with PowerShell, PowerShell will look more cryptic. But for anyone interested in learning scripting/automation for Windows, They should focus on PowerShell. More powerful, consistent, object-oriented, better docuementation, etc. – Keith Miller May 18 at 18:55
  • @Keith Miller: The problem is, the amount of .png files in this case will affect the result in a way that I don't understand and it's also not the correct result. I tested in a folder with 14 .pngs and the .ps1 file. 2,11,12,14 would delete 10,6,7,9 *.png. When I add another image 15.png - 2,11,12,14 would delete 10,5,6,8 *.png. Sorry, previously with ISE I meant Windows PowerShell ISE, which is the default program for editing .ps1 scripts. – atereou May 18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.