Third module to a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xendistar
Posts: 4
Joined: 11 Sep 2016 03:31

Third module to a batch file

#1 Post by xendistar » 11 Sep 2016 03:57

I currently have a batch file that does two things

1) It deletes specific files (.jpg & .pdf) older than X days old in a specific directory and sub folders
2) It scans subfolder of the same directory (above) and delete specific files extension (.jpg & .pdf)

This runs daily on a 2008R2 Server, it is (probably) not fancy, but it gives me the results I want and as I am happy to leave it as it is.

What I would like to do is to add a third module to my batch file which will delete any specific files (.jpg) over a certain size say 1536kb (this size may change) when the batch file runs. If I can get a small script to do this I may even opt to run this as a separate script and run it several times a day. The path the files reside in is

m:\global\pix\pix

Can anybody offer any suggestions please?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Third module to a batch file

#2 Post by aGerman » 11 Sep 2016 12:17

The unit of file sizes in batch is bytes. You can determine them using the ~z modifier of FOR variables. E.g.

Code: Select all

for %%i in ("m:\global\pix\pix\*.jpg") do echo %%~zi (%%i)

Just insert an IF statement in order to process files greater than a certain size.

Code: Select all

for %%i in ("m:\global\pix\pix\*.jpg") do if %%~zi gtr 1572864 echo "%%i"

(replace ECHO with DEL to delete the files)
Problem is that batch only supports signed integers with a width up to 32 bits. That means the greatest number that you can compare numerically is 2147483647.

Steffen

xendistar
Posts: 4
Joined: 11 Sep 2016 03:31

Re: Third module to a batch file

#3 Post by xendistar » 12 Sep 2016 14:33

Thanks eGerman, I will have a play and see how I get on

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Third module to a batch file

#4 Post by pieh-ejdsch » 14 Sep 2016 11:07

use an empty Folder and robocopy.

Code: Select all

@echo off
setlocal
set "T=%temp%\DF%time::=%"
md "%T%" ||exit /b 1
PushD "%T%"
:List
 rem Delim=TAB
 rem minage - not before 
for /f "tokens=3 delims=   " %%D in (
'robocopy /L /fp /np /njh /njs /mir "m:\global\pix\pix" "%T%" "*.jpg" "*.pdf" /minage:25 /min:1572864  /XX /NDL'
) do echo  del "%%D"

popD
rd "%T%"
exit /b 0


Phil

xendistar
Posts: 4
Joined: 11 Sep 2016 03:31

Re: Third module to a batch file

#5 Post by xendistar » 14 Sep 2016 12:29

pieh-ejdsch wrote:use an empty Folder and robocopy.

Code: Select all

@echo off
setlocal
set "T=%temp%\DF%time::=%"
md "%T%" ||exit /b 1
PushD "%T%"
:List
 rem Delim=TAB
 rem minage - not before 
for /f "tokens=3 delims=   " %%D in (
'robocopy /L /fp /np /njh /njs /mir "m:\global\pix\pix" "%T%" "*.jpg" "*.pdf" /minage:25 /min:1572864  /XX /NDL'
) do echo  del "%%D"

popD
rd "%T%"
exit /b 0


Phil


Hi Phil

Can you explain what each line is doing , I have an idea of bits of it but not most of it

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Third module to a batch file

#6 Post by foxidrive » 14 Sep 2016 13:57

xendistar wrote:Thanks eGerman, I will have a play and see how I get on

It would repay the kindness if you let aGerman know how you got on.

xendistar
Posts: 4
Joined: 11 Sep 2016 03:31

Re: Third module to a batch file

#7 Post by xendistar » 17 Sep 2016 16:46

foxidrive wrote:
xendistar wrote:Thanks eGerman, I will have a play and see how I get on

It would repay the kindness if you let aGerman know how you got on.


I had every intention of replying to eGerman (and anybody else who offered a solution once I had tried them), it has just taken me longer to try eGerman's script than I had intended. I asked Phil the question regarding his script simply as I was reading his contribution over a cup of tea and to be perfectly honest I could not make head of tail of it (I understood just about eGerman's script).

In regards to eGerman's script it worked perfectly in the tests I have run on a folder with 142 mixed files types, of which 72 are .jpg and 27 of those .jpg have a size greater than 1572864. When I get the time next week I will try the script for real.

My thanks to eGerman

Post Reply