Script to Del files based on size

March 25, 2009 at 00:45:25
Specs: Windows XP
Hi,

I have a txt file (eg. files.txt) with the following info: filename filesize. ie:

01a2316a90e2ef0490a7dbb5fd4a033c.exe 6689
010c1cc11110a1c6e4ece159c8f44c2e.rar 1028

I need a batch or vbscript to loop through this files.txt and only if a file exists with that name AND exact file size, then to delete that file.

If anyone can help me with this, that would be great.

TIA, Anthony


See More: Script to Del files based on size

Report •


#1
March 25, 2009 at 01:05:33
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=1-2 delims= " %%a in (files.txt) do (
if %%~Za equ %%b del %%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 25, 2009 at 01:50:10
Hi,

Thanks for the quick turnaround and this works GREAT! As I needed to copy some files as well based on the same criteria (name/size) I have modified your example to copy the files instead of deleting by changing:

if %%~Za equ %%b del %%a

to

if %%~Za equ %%b copy %%a k:\test

but I get an "syntax of the command is incorrect" error for the files it does find when I execute this - I removed the echo off, and I see this:

(if  EQU  copy 01a2316a90e2ef0490a7dbb5fd4a033c.exe  6689 k:\test )
The syntax of the command is incorrect.

What am I doing wrong?


Report •

#3
March 25, 2009 at 05:53:02
Post your whole script.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
March 25, 2009 at 05:58:28
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=1-2 delims= " %%a in (files.txt) do (
if %%~Za equ %%b copy %%a c:\tempdir
)


Report •

#5
March 25, 2009 at 06:53:02
I dunno; works here.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
March 25, 2009 at 07:32:59
Hmm, weird ... keeps giving me the 'syntax of the command is incorrect' error ?? Could it be because the copy action is being run under Vista? The initial batch (delete) was under XP, though it runs also under Vista - I have just checked. I'm stumped!

Report •

#7
March 25, 2009 at 07:37:27
Yep, checked the copy script under XP = works, under Vista gives above error.

Report •

#8
March 25, 2009 at 07:59:39
I'd be very surprised if Viista COPY was much different.

The really troubling bit is the line when you disabled ECHO OFF and got this:

(if EQU copy 01a2316a90e2ef0490a7dbb5fd4a033c.exe 6689 k:\test )

Is that EXACTLY the msg?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
March 25, 2009 at 08:15:53
Yes it is ... I have zipped my batch file, files.txt and the output I get (output.log). You can get it from:

Link


Report •

#10
March 25, 2009 at 08:51:58
Hi Mechanix2Go,

It should do - I have just checked it is OK (its a ZIP file).
Otherwise I could mail it. I'll use the mail form.


Report •

#11
March 25, 2009 at 08:58:02
I got the zip; checking it now...


Give me a few minutes.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#12
March 25, 2009 at 11:26:46
Whatever whitespace character separates the filename and size (a tab, apparently) isn't what you specify in your delims (a space). Replace your delims= " to
delims=	 "
(there's a tab there)

Report •

#13
March 25, 2009 at 11:33:45
It turns out that in your files.txt the name and size are separated by a tab. When I changed the tab to a space the problem went away. I have no idea why.

Here's a script to change tab to space.

===============================
:: NOTAB.BAT
:: chg tab to space

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=1-2 delims= " %%a in (files.txt) do (
echo %%a %%b >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#14
March 25, 2009 at 13:42:17
oops

The solution is to fix the batch.

==================================
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=1-2" %%a in (files.txt) do (
if %%~Za equ %%b copy %%a c:\tempdir
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#15
March 25, 2009 at 14:17:09
Thanks, I will give this a try!

Report •


Ask Question