AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

batch help: selectively deleting files

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Sat Jul 28, 2007 8:58 am    Post subject: batch help: selectively deleting files Reply with quote

Hey all,

Using a batch file, I need to delete all files > 100 KB from a given folder (./logs).
Unfortunately, I can't use AHK for this.

I've had to realize that my batch skills have become very rusty, as I can't seem to create a working script from the snippets of code I've collected from the interwebs:
Code:
REM check filesize
for %%F in (%currentFile%) do set fileSize=%%~zF
if %fileSize% geq 200000 goto delFile

REM delete all files in folder
dir /b E:\*.exe > %log%
For /F %%i in (%log%) do del %%i
rem Del %log%
rem Set log=
I guess the main problem is sharing the current filename between the two routines.

Any help would be greatly appreciated!
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
daniel2



Joined: 23 Jul 2007
Posts: 41

PostPosted: Sat Jul 28, 2007 7:11 pm    Post subject: Reply with quote

You can pass parameters using Call; they are handled in the same way as input params in AHK..
Code:
@Echo Off
for %%F in ("*.*") do call :LoopFiles  %%~zF %%F
echo. Done parsing through folder..
pause
Goto end

:LoopFiles
set A_LoopFileSize=%1%
set A_LoopFileName=%2%
echo. %A_LoopFileName% = %A_LoopFileSize%
if %A_LoopFileSize% geq 100000 Goto delFile
Goto end

:delFile
echo. !!!! delete file %A_LoopFileName% !!!!
echo.
::del %A_LoopFileName%

:End
You can obviously remove all the fluff.. I just left it in to help explain... Very Happy

EDIT: I forgot to mention; you should enclose your filename in "" if it may contain spaces..
Code:
for %%F in ("*.*") do call :LoopFiles  %%~zF "%%F"
Back to top
View user's profile Send private message
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Sat Jul 28, 2007 9:18 pm    Post subject: Reply with quote

Thank you very much, Daniel!

Here's the complete script now:
Code:
@Echo Off

echo ***** Deleting obsolete log files. *****
cd logs
for %%F in ("*.*") do call :loopFiles %%~zF "%%F"
cd..
echo ***** Done deleting log files. *****
echo.
::pause
goto launch
goto end

:launch
echo ***** Launching application. *****
echo.
start foo.exe -bar
goto end

:loopFiles
set A_LoopFileSize=%1%
set A_LoopFileName=%2%
::echo current file: %A_LoopFileName% (%A_LoopFileSize%)
if %A_LoopFileSize% leq 1024 goto delFile
if %A_LoopFileSize% geq 102400 goto delFile
goto end

:delFile
::echo.
echo deleting file: %A_LoopFileName%
::echo.
del %A_LoopFileName%
goto end

:end
(As for the two IFs in loopFiles, it seems there is no ELSE in batch... )
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group