Joel D
Joel D

Reputation: 39

Deleting file with space in the name in a loop

I'm trying to delete every file bigger than a certain size in a directory but i'm not able to delete file with space in the name. The below script is what I have written to achieve the same.

@echo off
setlocal enabledelayedexpansion
set "MaxSize=3"

for /r %%I in (*) do (
echo %%I %%~zI Bytes

set /a kb=%%~zI/1024 + 1
echo !kb!Ko

if !kb! GTR %MaxSize% (
echo TIME : [%date%, %time%] ^| The size of the file %%I is !kb! Ko 
the file is to big so the file was deleted >> Log_Remove.log
del /F %%I
echo file too big the file was deleted
) else (
echo file size is okay
)
)

Any idea about how i can delete the file with a space in the name ?

Upvotes: 1

Views: 38

Answers (1)

Compo
Compo

Reputation: 38623

I've posted this as an addition to my comment, to show you an easier way of structuring your script.

@Echo off
Set "MaxKB=3"
Set /A MaxB=MaxKB*1024
For /R %%I In (*)Do If %%~zI Gtr %MaxB% (
    Echo [%DATE%, %TIME%]: %%I was too large and therefore deleted>>"Log_Remove.log"
    Del /A /F "%%I"
    Echo file %%I was larger than %MaxKB% KB
)Else Echo file %%I was within %MaxKB% KB

Upvotes: 1

Related Questions