Computing.Net > Forums > Programming > Script to Del files based on size

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to Del files based on size

Reply to Message Icon

Name: anthonyaykut
Date: March 25, 2009 at 00:45:25 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 25, 2009 at 01:05:33 Pacific
Reply:

@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


0

Response Number 2
Name: anthonyaykut
Date: March 25, 2009 at 01:50:10 Pacific
Reply:

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?


0

Response Number 3
Name: Mechanix2Go
Date: March 25, 2009 at 05:53:02 Pacific
Reply:

Post your whole script.


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

M2


0

Response Number 4
Name: anthonyaykut
Date: March 25, 2009 at 05:58:28 Pacific
Reply:

@echo off & setLocal EnableDelayedExpansion

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


0

Response Number 5
Name: Mechanix2Go
Date: March 25, 2009 at 06:53:02 Pacific
Reply:

I dunno; works here.


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

M2


0

Related Posts

See More



Response Number 6
Name: anthonyaykut
Date: March 25, 2009 at 07:32:59 Pacific
Reply:

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!


0

Response Number 7
Name: anthonyaykut
Date: March 25, 2009 at 07:37:27 Pacific
Reply:

Yep, checked the copy script under XP = works, under Vista gives above error.


0

Response Number 8
Name: Mechanix2Go
Date: March 25, 2009 at 07:59:39 Pacific
Reply:

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


0

Response Number 9
Name: anthonyaykut
Date: March 25, 2009 at 08:15:53 Pacific
Reply:

Yes it is ... I have zipped my batch file, files.txt and the output I get (output.log). You can get it from:

Link


0

Response Number 10
Name: anthonyaykut
Date: March 25, 2009 at 08:51:58 Pacific
Reply:

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.


0

Response Number 11
Name: Mechanix2Go
Date: March 25, 2009 at 08:58:02 Pacific
Reply:

I got the zip; checking it now...


Give me a few minutes.


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

M2


0

Response Number 12
Name: Razor2.3
Date: March 25, 2009 at 11:26:46 Pacific
Reply:

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)

0

Response Number 13
Name: Mechanix2Go
Date: March 25, 2009 at 11:33:45 Pacific
Reply:

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


0

Response Number 14
Name: Mechanix2Go
Date: March 25, 2009 at 13:42:17 Pacific
Reply:

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


0

Response Number 15
Name: anthonyaykut
Date: March 25, 2009 at 14:17:09 Pacific
Reply:

Thanks, I will give this a try!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Script to Del files based on size

dos copy files based on filename www.computing.net/answers/programming/dos-copy-files-based-on-filename/17353.html

Help with Script to process files www.computing.net/answers/programming/help-with-script-to-process-files/14989.html

Move a XML file based on content www.computing.net/answers/programming/move-a-xml-file-based-on-content/18945.html