how to move files based on file-ending from dirs and subdirs to specific dir?

how to move files based on file-ending from dirs and subdirs to specific dir?

Post by Evan Carm » Wed, 03 Jan 2007 16:14:38


hey,

i am trying to move files with a specific file-ending (.msf) to dir above their
current location. my code so far works as long as all the files are on the same
dir level.

but how can i make it work if there are multiple subdirs with files inside of
them on different dir levels?


my code so far is:
---
top = 'f:\\test\\mail'


import os, time, itertools

#walk and find all files
allfiles = []
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
allfiles.append(os.path.join(root, name))
#remove all non .msf files
index = []
for match in allfiles:
if match.endswith('.msf'):
index.append(match)

indexdest = []
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind
ex]

#\\.. to remove the ending and than basename to add it back on
indexdest = [os.path.normpath(i) for i in indexdest]
indexdest = [i.replace('Mail', 'backups-msf') for i in indexdest]

for a, b in itertools.izip(index, indexdest):
os.renames(a, b)
i want the .msf files to be in a dir on the same level as where the top string
points to (in this case f:\test\backups-msf).

thanks,
Evan

p.s. is there a more accurate word for a directory level or something on the
same "tier" of a directory?
 
 
 

1. Removing old name: no such file or directory, /myhome/.emacs-flc/#dir#dir#file.c.flc

2. External prog get file from main dir, want file in diff dir


I am running a external program(2) from another ruby program (1). This
external program requre a local file (the file must lay in the prog 1
dir) . It uses some kind of custom get comand to get this file.

Say my dir is calld TestCase

Then this fle must be in TesCase/file.txt


But i want to store the file in TestCase/CaseX/file.txt (or in some
other folder)


The problem is that i cant change the extern program, and it asks for
the file to be in TestCase folder.

Some code almost similar to the code in the extern program (I cant
change it):
up = some proxy method to connect to user
input up.getfile("file.txt")

How do i redirect the extern programs file request to another folder.
And i cant change the externprogram.

Is ther som way to do it, or must i give up and just keep all files
cluttered in the main prog directory?
--
Posted via http://www.yqcomputer.com/

3. Program to list all dirs, subdirs and files older than 1 year

4. hi... how to search files in a dir based on specific pattern

5. cant run asp file from subdir of Web root dir

6. Getting all files in Dir and Subdirs

7. from AS400 program copy files on NT server (from dir. to dir.)

8. Windows dir. & system32 dir clogged with 0byte files

9. OpenVMS V8.4 I64 DCL-dir command fails Part II: file named *.dir causes the problem

10. OpenVMS V8.4 I64 DCL-dir command fails Part II: file named *.dir causes the problem

11. Copy worksheet from multiple files in one DIR to another DIR & rename

12. :! starts in home dir, not in dir containing file

13. Is ATL file supposed to be in main project dir or source dir?

14. Howto : rename files dir/*.asc to dir/*.asc.sent

15. OpenVMS V8.4 I64 DCL-dir command fails Part II: file named *.dir