Home > Back-end >  How to delete a file if .shp is less than(equal) 100 byte with batch?
How to delete a file if .shp is less than(equal) 100 byte with batch?

Time:01-11

I have a folder Esri shapes. Every shape consists of 5 seperate files. (.dbf .prj .qpj .shp .shx) I want delete 5 seperate file if .shp is less than(equal) 100byte

For example if I have the following files:

A.dbf
A.prj
A.qpj
A.shp (100byte)
A.shx
B.dbf
B.prj
B.qpj
B.shp (150byte)
B.shx
C.dbf
C.prj
C.qpj
C.shp
C.shx (243byte)

Then I want these files as below:

B.dbf
B.prj
B.qpj
B.shp
B.shx
C.dbf
C.prj
C.qpj
C.shp
C.shx

I don't know much about the code language needed to write a batch file. I found some code elsewhere on Stackoverflow, but it 100byte all delete

@echo off
setlocal
:: Size is in bytes
set "min.size=100"

for /f  "usebackq delims=;" %%A in (`dir /b /A:-D *.*`) do If %%~zA LSS %min.size% del "%%A"

CodePudding user response:

for /f  "usebackq delims=;" %%A in (`dir /b /A:-D *.shp`) do If %%~zA LEQ %min.size% ECHO del "%%~nA.*"
  • The *.shp makes the dir return only those files named whatever.shp
  • The ECHO has been inserted as a safety measure to simply report what files are intended to be deleted. Once you have finished testing, remove the echo keyword on that line to actually delete the files reported.
  • The ~n modifier picks just the name part of the file %%A
  • The .* then says this name - all extensions.

Always verify against a test directory before applying to real data.

CodePudding user response:

What about this forfiles solution:

forfiles /M *.shp /C "cmd /c if @fsize LEQ 100 echo @file, @fsize"

This shows all *.shp files, smaller than 100 bytes, together with their size.
Chaning the command (del @file instead of echo @file, @fsize) and it should work.

  • Related