Ta bort en nummerserie av filer

[python]
#!/usr/bin/env python

import sys, glob, re, os, getopt

def main (argv):

startMatch=””
fromIndex=-1
toIndex=-1
endMatch=””

options, reminder = getopt.getopt(argv, ’s:f:t:e:’, [’startmatch=’, ’fromindex=’, ’toindex=’, ’endmatch=’])

for opt, arg in options:
if opt in (’-s’, ’–startmatch’):
startMatch = arg
elif opt in (’-f’, ’–fromindex’):
fromIndex = int(arg)
elif opt in (’-t’, ’–toindex’):
toIndex = int(arg)
elif opt in (’-e’, ’–endmatch’):
endMatch = arg

if len(startMatch) > 0 and len(endMatch) > 0 and fromIndex > -1 and toIndex > 0 and fromIndex < toIndex:
files = glob.glob(”*”);
files.sort();

filesInRange = findFilesInRange(files, startMatch, fromIndex, toIndex, endMatch)
anwser = raw_input(”Do you which to delete these files: ” + str(filesInRange) +”?\n\n(y)es or (n)o:”)
if anwser == ”y”:
for file in filesInRange:
os.remove(file)
else:
print ”””
rms.y usage

mandatory parameters –startmatch=, –fromindex=, –toindex=, –endmatch=

example: ~$ rms.py –startmatch=’picture’, –fromindex=3, –toindex=24, –endmatch=’.jpg’
”””
sys.exit()

def isFileInRange (file, fileStartMatch, fromIndex, toIndex, fileEndMatch):
match = re.search(’(’+fileStartMatch+’)([0-9]{1,100})(’+fileEndMatch+’)’, file)
if (match and match.group(2) is not None):
fileIndex = int(match.group(2))
if(fileIndex >= fromIndex and fileIndex <= toIndex):
return file

def findFilesInRange(files, fileStartMatch, fromIndex, toIndex, fileEndMatch):
filesInRange=[]
for file in files:
outputFile=isFileInRange(file, fileStartMatch, fromIndex, toIndex, fileEndMatch)
if(outputFile is not None):
filesInRange.append(outputFile)
return filesInRange

if __name__ == ’__main__’:
main(sys.argv[1:])

[/python]

Exempel på användning
Används på egen risk!
[bash]

peter@E5410:~/bashtest$ ls
bild0.jpg bild17.jpg bild24.jpg bild31.jpg bild38.jpg bild45.jpg bild52.jpg bild59.jpg bild66.jpg bild73.jpg bild80.jpg bild87.jpg bild94.jpg
bild100.jpg bild18.jpg bild25.jpg bild32.jpg bild39.jpg bild46.jpg bild53.jpg bild60.jpg bild67.jpg bild74.jpg bild81.jpg bild88.jpg bild95.jpg
bild12.jpg bild19.jpg bild26.jpg bild33.jpg bild40.jpg bild47.jpg bild54.jpg bild61.jpg bild68.jpg bild75.jpg bild82.jpg bild89.jpg bild96.jpg
bild13.jpg bild20.jpg bild27.jpg bild34.jpg bild41.jpg bild48.jpg bild55.jpg bild62.jpg bild69.jpg bild76.jpg bild83.jpg bild90.jpg bild97.jpg
bild14.jpg bild21.jpg bild28.jpg bild35.jpg bild42.jpg bild49.jpg bild56.jpg bild63.jpg bild70.jpg bild77.jpg bild84.jpg bild91.jpg bild98.jpg
bild15.jpg bild22.jpg bild29.jpg bild36.jpg bild43.jpg bild50.jpg bild57.jpg bild64.jpg bild71.jpg bild78.jpg bild85.jpg bild92.jpg bild99.jpg
bild16.jpg bild23.jpg bild30.jpg bild37.jpg bild44.jpg bild51.jpg bild58.jpg bild65.jpg bild72.jpg bild79.jpg bild86.jpg bild93.jpg makefiles.sh
peter@E5410:~/bashtest$ rms -s’bild’ -f0 -t19 -e’.jpg’
Do you which to delete these files: [’bild0.jpg’, ’bild12.jpg’, ’bild13.jpg’, ’bild14.jpg’, ’bild15.jpg’, ’bild16.jpg’, ’bild17.jpg’, ’bild18.jpg’, ’bild19.jpg’]?

(y)es or (n)o:
[/bash]