Computing.Net > Forums > Programming > How to Do this in a Script File

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

How to Do this in a Script File

Reply to Message Icon

Original Message
Name: einstein8
Date: October 9, 2008 at 14:28:34 Pacific
Subject: How to Do this in a Script File
OS: XP
CPU/Ram: 1.8/2Gb
Model/Manufacturer: Dell
Comment:

how do i do this in a script file?

i have files in a folder with the file names similar to below:

fluffy.b1
fluffy.b14
fluffy.b25

i want to write a script that will search my folder for these files, and auto delete the lower .b files and just leave the highest b file that is out there.. thus, the file in the folder that will be left will only be fluffy.b25!

thanks
Marie

hopeless but trying


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: October 10, 2008 at 02:25:00 Pacific
Reply: (edit)


@echo off
pushd \MyFolder
for /F "skip=1" %%a in ('dir fluffy.b* /B /O-E') do del %%a
popd
:: [End_Of_Batch]


Report Offensive Follow Up For Removal

Response Number 2
Name: einstein8
Date: October 10, 2008 at 08:50:46 Pacific
Reply: (edit)

thanks for the reply IVO but that script deleted the wrong files for those fluffy files that have extension ending more than fluffy.b9.. example,

fluffy.b1
fluffy.b3
fluffy.b15
fluffy.b24

that script deleted everything and left fluffy.b3 wherein it shouldv left the b24 instead..

thanks! - M

hopeless but trying


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: October 10, 2008 at 12:50:46 Pacific
Reply: (edit)

Sorry,

replace the switch /O-E with /O-N.

I used -E to perform better and that resulted into a mistake. Sorry again and I hope you had no harm from that.


Report Offensive Follow Up For Removal

Response Number 4
Name: IVO
Date: October 10, 2008 at 13:34:27 Pacific
Reply: (edit)

Please, ignore the previous post as it is ineffective. I'm working on the problem.

I hpe to post the solution as soon as posible


Report Offensive Follow Up For Removal

Response Number 5
Name: Judago
Date: October 10, 2008 at 13:43:30 Pacific
Reply: (edit)

It's a wonderful idea, but it suffers from that old problem of sorting. 2 comes before 100, 5 comes before 40 an so on. Here's a sample of dir /b /o-n n* in a test directory:


n81
n5681
n321
n30
n3
n20
n2
n100
n1

Obviously this is a problem with the way results are sorted and not a problem with the script.

[edit]
The problem was probably realised and posted about while I was typing.


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: October 10, 2008 at 14:13:24 Pacific
Reply: (edit)

The following solves the problem Judago pointed out

@echo off & setlocal EnableDelayedExpansion
pushd \MyFolder
type Nul > $1.tmp
for /F %%a in ('dir Fluffy.B* /B') do (
set ext=%%~xa
if /I !ext:~-2^,1!==B set ext=.B0!ext:~-1,1!
echo.%%~na!ext! >> $1.tmp
)
for /F "skip=1" %%a in ('sort /R ^< $1.tmp') do (
set ext=%%~xa
if /I !ext:~-2^,1!==0 set ext=.B!ext:~-1,1!
del %%~na!ext!
)
del $1.tmp
popd
:: [End_Of_Batch]



Report Offensive Follow Up For Removal

Response Number 7
Name: Judago
Date: October 10, 2008 at 14:20:29 Pacific
Reply: (edit)

I got stuck into it and came up with a different solution, may as well post it anyway.


@ECHO OFF
PUSHD /MYFOLDER
SET HGHEXT=-1
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%G IN (FLUFFY.B*) DO (
SET EXT=%%~xG
SET EXT=!EXT:~2!
IF NOT DEFINED EXT SET EXT=0
IF !EXT! GTR !HGHEXT! SET HGHEXT=!EXT!&&SET HGHFLNM=%%G
)
FOR %%F IN (FLUFFY.B*) DO IF /I NOT %%F==!HGHFLNM! ECHO DEL %%F
POPD

It will echo the del statement so it can be checked prior to using.

[edit]
Forgot about running externally(Added pushd/popd like IVO's script)


Report Offensive Follow Up For Removal

Response Number 8
Name: Judago
Date: October 10, 2008 at 14:55:49 Pacific
Reply: (edit)

IVO, I really hate to do this to you but sort /r has exactly the same problem backwards,

dir /b fluffy.b*|sort /r


FLUFFY.B96
FLUFFY.B870
FLUFFY.B77
FLUFFY.B26
FLUFFY.B15
FLUFFY.B10
FLUFFY.B1

With these file names your script(I made small changes to suit my enviroment) is keeping fluffy.b96


They should add a /p switch, sort properly:)


Report Offensive Follow Up For Removal

Response Number 9
Name: IVO
Date: October 11, 2008 at 02:23:03 Pacific
Reply: (edit)

Hi Judago,

you missed I normalized the extension by inserting a leading 0 if the number is less than 10. Re-read my code carefully and advise me if anything is wrong. By the way I tested it with many sequences with no problem.


Report Offensive Follow Up For Removal

Response Number 10
Name: Judago
Date: October 11, 2008 at 02:47:33 Pacific
Reply: (edit)

IVO,

Ok I see what you were doing now, but it only seems to work if all of the numbers are less than one hundred, though this may be all einstein8 wants/needs. I tested your script and saw it didn't keep the fluffy.b870 file(with the files listed above) I had for testing and wrongly jumped to a conclusion. Without paying much attention, I thought you were trying to account for the possibility of a fluffy.b file with that bit of code. I definitely should have been paying more attention. Sorry, I should be sure before I post such things.


Report Offensive Follow Up For Removal

Response Number 11
Name: einstein8
Date: October 11, 2008 at 10:15:04 Pacific
Reply: (edit)

thank you IVO and Judago.. IVO is right in assuming that i would only need this for 99 sequences and lower.. i knew ud look after me Judago as you helped on my very first post here ;)
i tried IVOs script on response no. 6 and its working great. would sure try Judago's on no. 7.. im assuming both will do the job!
great stuff! hugs and kisses - Marie

hopeless but trying


Report Offensive Follow Up For Removal

Response Number 12
Name: IVO
Date: October 11, 2008 at 11:49:41 Pacific
Reply: (edit)

Thanks Marie,

a kiss from a nice girl is the best reward!

To Judago,

I assumed the extension no more than three chrs with two digits as longer extensions are not usual even under Windows. Your code is indeed better than what I worked out, more straightforward and not bound to an external command as Sort.


Report Offensive Follow Up For Removal

Response Number 13
Name: Judago
Date: October 11, 2008 at 15:23:55 Pacific
Reply: (edit)

a kiss from a nice girl is the best reward!

Agreed, glad to be of some assistance.


Report Offensive Follow Up For Removal

Response Number 14
Name: einstein8
Date: October 14, 2008 at 09:05:15 Pacific
Reply: (edit)

hello IVO and Judago..
gud am.. its me again.. im afraid my circumstances for this script has changed overnight! :( i need the fluffy.b* files with the latest timestamp (now, irregardless of the numbers after .b!)

its like this:
1. search the directory for all fluffy.b* files
2. delete all else fluffy.b* leaving only the latest fluffy.b* based on timestamp

thanks for the help again!

hopeless but trying


Report Offensive Follow Up For Removal

Response Number 15
Name: Holla
Date: November 6, 2008 at 00:59:12 Pacific
Reply: (edit)

einstein,
after checking, please remove the echo before the del commdand


@echo off
setLocal EnableDelayedExpansion
set count=0
for /f "tokens=* delims=" %%A in ('dir /b/a-d/o-d fluffy.b*') do (
set /a count+=1
if !count! geq 2 echo del %%A
)
endlocal

--
Holla.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to Do this in a Script File

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 





Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge
Poll History




Data Recovery Software