PDA

View Full Version : DOS scripts


Chris Wilson
22-02-08, 14:49
I want to write a script that will delete files in a directory that are duplicates. The duplicates have (for example) the name chevron[1].jpg

or chevron[2].jpg

the original being say chevron.jpg

How can I write a single script to delete the [1] [2] [3] et cetera dupes?

I can do any one by

del *[1]*.jpg

but not sure how to do the [2] [3] et cetera with one script and make it run contiguously.

I have got this mess by carelessly adding the same files to a directory with an app that adds [1] to the first dupe [2] to the next and so on.

JustGav
22-02-08, 14:52
del *[?]*.jpg MAY work... * is a global sub, whereas ? is a character sub...

I would however suggest testing it carefully first, because I'm not 100% sure what the first * might match...

cheekymonkey
22-02-08, 15:03
You might be able to test by doing dir *[?]*.jpg which will list the matching files.

EDIT tested, and yes JustGav's method is fine :)

JustGav
22-02-08, 15:05
You might be able to test by doing dir *[?]*.jpg which will list the matching files.

Yup, I'm not 100% sure because I'm having to think in regex and do a little conversion


for i in [1 2 3 4 5 6 7 8 9 10] do
rm -f cetron$i
done

But then I'm just geeky, Chris ignore the above snippit as it won't work in DOS :)

Schtuv
22-02-08, 15:05
* is difficult to use, as it'll look for anything before the '.'. '?' can be used to define a wildcard single space, but that leads to looong scripts if you can't predict the location of the '[1]'. I'm sure someone intelligent will be along soon to sort this. I may have a crack at it later :-)

I suspect the easiest way is to export a list of files in a directory to a file, and pipe '<' that back in to a batch file which can select out anything using the 'FIND' command, and output that to a delete statement.

Then again, if you could sort by date to organize dupes.

Chris Wilson
22-02-08, 15:09
del *(*).jpg works, as the dupes all have the (*) just before the .jpg. Seems to be fine on a duplicate directory, so I'll risk it on the real thing now ;) Cheers.