?
Solved

windows batch/cmd script to delete file greater than size "0"

Posted on 2012-08-21
7
Medium Priority
?
1,780 Views
Last Modified: 2012-08-21
I am using below script
@ECHO OFF
FOR /R "C:\Documents and Settings\Desktop\Testingfilesize" %%F IN (*.*) DO (
       IF %%~zF GTR 0 (DEL /F %%F))
EXIT /B /0

But it is getting error message
%%F was unexpected at this tim
       IF %%~zF GTR 0 (DEL /F
) was unexpected at this time.
0
Comment
Question by:bkishan315
  • 3
  • 3
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38317640
Try just

FOR /R "C:\Documents and Settings\Desktop\Testingfilesize" %%F IN (*.*) DO IF %%~zF GTR 0 DEL /F "%%~F"

Steve
0
 

Author Comment

by:bkishan315
ID: 38317818
It's getting same error:
%%F was unexpected at this time
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 38317979
Hmm, works for me.  Are you running this from cmd.exe prompt or batch file.  In batch file you use %%F and it cmd.exe you use %F:

I did this from cmd prompt:

for /r "c:\documents and settings\stephen\desktop" %F in (*.*) do if %~zF GTR 0 echo del /f "%~F"

from a batch file it should work the same with %%F as posted above.

Steve

C:\Documents and Settings\stephen\Desktop>if 407 GTR 0 echo del /f "c:\documents and settings\stephen\desktop\stuff to read\win7 powerfree.lnk"
del /f "c:\documents and settings\stephen\desktop\stuff to read\win7 powerfree.lnk"

C:\Documents and Settings\stephen\Desktop>if 399 GTR 0 echo del /f "c:\documents and settings\stephen\desktop\stuff to read\Win7_9steps.lnk"
del /f "c:\documents and settings\stephen\desktop\stuff to read\Win7_9steps.lnk"

etc.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 38318006
I ran your original code here just changing the directory name and it ran without error.  Did you copy that code right out of the BAT file?  Have you tried a different folder, like something simple, maybe C:\Temp to see if it errors as well?

Also, I would have expected the username to be in the path you are using, but that shouldn't cause an error I don't think.

~bp
0
 

Author Closing Comment

by:bkishan315
ID: 38318010
It is working fine...
0
 

Author Comment

by:bkishan315
ID: 38318019
Is there any way i can get the list of files that has size greater than "0" to my email....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38318342
Well you could use a VBScript solution for that, or just replace the "DEL" command here with something like this:

@echo off
(for /r "c:\documents and settings\stephen\desktop" %%F in (*.*) do if %%~zF GTR 0 (
  echo "%~dpF","%~nxF",%~zF
  del /f "%~F" 2>NUL
))> file.csv

Then that gives you a "file.csv" with

"Path","filename.ext",size

which you can open in Excel say.

If you want to email it then look at some suggestions here.  if not sure post another Q now as this is closed.

http://scripts.dragon-it.co.uk/links/email-from-batch

Steve
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question