spiky
spiky

Reputation: 13

batch script to delete log file if size is more

Hi I am trying to create a Batch script to delete the log files in an application if the file size is more, here is my code. I am getting Syntax error after second "pushd"

@echo off
pushd "C:\Program Files\temp\Logs" 
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do @del "%%F" 
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do @del "%%F" 
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set maxbytesize = 10
 FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
   if %size% GTR %maxbytesize% (
        del "%%A"
) 

Upvotes: 0

Views: 2388

Answers (2)

user7818749
user7818749

Reputation:

Copy this code exactly and try please.

@echo off
setlocal enabledelayedexpansion
pushd "C:\Program Files\temp\Logs" 
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d E*.log') do @del "%%F" 
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d A*.log') do @del "%%F" 
popd
sleep 1
pushd "C:\Program Files\temp\modules\Logs"
set file="P*.log"
set "maxbytesize=10"
for %%A in ("%file%") do (
   set size=%%~zA
   if !size! GTR %maxbytesize% del "%%A"
)

Upvotes: 0

Stephan
Stephan

Reputation: 56180

for /f pocesses the content of a file - not what you want here. Use a plain for:

for %%A in ("%file%") do set size=%%~zA

Upvotes: 1

Related Questions