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

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.
bkishan315Asked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
Try just

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

Steve
0
 
bkishan315Author Commented:
It's getting same error:
%%F was unexpected at this time
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Bill PrewCommented:
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
 
bkishan315Author Commented:
It is working fine...
0
 
bkishan315Author Commented:
Is there any way i can get the list of files that has size greater than "0" to my email....
0
 
Steve KnightIT ConsultancyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.