Re: Help please with code to find and move files.



On Sun, 30 Dec 2007 22:52:32 -0800, Dennis Lee Bieber
<wlfraed@xxxxxxxxxxxxx> wrote:

On Sun, 30 Dec 2007 23:58:17 -0500, inFocus@xxxxxx declaimed the
following in comp.lang.python:


I am sorry i thought I did say what I was tryng to do.

The only thing I picked up from the thread is that you attempted to
move any file, whose name contained -- in any order/position -- two
specific substrings, from some specified source directory tree to a
single specified directory.

Among the unknowns: what happens if two source directories have
files with identical names! Does the second overwrite the first? Does
the second NOT get moved? Should the second have the name modified with
a suffix count?

a/something.wht -> dest/something.wht
b/something.wht -> ?????
1) replace the first something.wht
(thereby losing a/something.wht)
2) don't move -- leaving
b/something.wht unmoved
3) rename as
dest/something1.wht

Neither do I have any idea of what type of problem you really
encountered (you'll have to forgive me, but I do not intend to try
running your script, on my system, given that I do not know what the
effects, in the end, are to be).

The closest to what you seem to ask, that I've created in the past,
is a task to identify potential duplicate files (I have a large number
of downloaded images). Note the date -- I think it predated os.walk()

-=-=-=-=-=-=-
#
# DupCheck.py -- Scans a directory and all subdirectories
# for duplicate file names, reporting conflicts
# March 22 1998 dl bieber <wulfraed@xxxxxxxxxx>
#

import os
import sys
import string
from stat import *

Files = {}

def Scan_Dir(cd):
global Files, logfile

cur_files = os.listdir(cd)
cur_files.sort()
for f in cur_files:
fib = os.stat("%s\\%s" % (cd, f))
if S_ISDIR(fib[ST_MODE]):
Scan_Dir("%s\\%s" % (cd, f))
elif S_ISREG(fib[ST_MODE]):
if Files.has_key(string.lower(f)):
(aSize, aDir) = Files[string.lower(f)]
if fib[ST_SIZE] == aSize:
logfile.write(
"***** Possible Duplicate File: %s\n" % (f))
logfile.write(
" %s\t%s\n" % (fib[ST_SIZE], cd))
logfile.write(
" %s\t%s\n\n" % (Files[string.lower(f)]))
else:
Files[string.lower(f)] = (fib[ST_SIZE], cd)
else:
logfile.write(
"***** SKIPPED Not File or Dir: %s\n\n" % (f))


if __name__ == "__main__":
Cur_Dir = raw_input("Root Directory -> ")
Log_To = raw_input("Log File -> ")

if Log_To:
logfile = open(Log_To, "w")
else:
logfile = sys.stdout

Scan_Dir(Cur_Dir)

if Log_To:
logfile.close()
-=-=-=-=-=-

I am sorry if I was not clear in what I was trying to achieve. All I
wanted was simple way to achieve what windows does when you use search
for Files or Folders, and all the files that mach two words like foo
and bar in the file name to be moved or copied to a specified folder,
duplicates should not be copied just skipped.
.