0

I have several files of different kinds in different folders and subfolders. They are huge in number. I am looking to extract all the files from all locations and copy to a single directory.

I wrote a Python script as follows, [for .doc file only], but is taking too much of time.

import os
from fnmatch import fnmatch
def listallfiles1(n):
    root = 'C:\Cand_Res'
    pattern = "*.doc"
    for path, subdirs, files in os.walk(root):
        for name in files:
            if fnmatch(name, pattern):
                print os.path.join(path, name)

If you may suggest some smart solution either by using any Windows feature, or any MS-DOS command etc. or Python script.

  • 1
    If it works post it on codereview – Bhargav Rao May 01 '15 at 11:56
  • The first thing to do when something is "taking to much time" is to run a profiler to identify the bottleneck. Python has [the cProfile standard module for that](https://docs.python.org/2/library/profile.html). See http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script for some simple usage example and alternatives. – Sylvain Leroux May 01 '15 at 12:37
  • It's not likely to help you now, unless you want to try a alpha release, but [os.walk in 3.5](https://docs.python.org/3.5/library/os.html#os.walk) is substantially faster on Windows. – Eryk Sun May 01 '15 at 20:50

1 Answers1

0

Does this command help?

dir *.doc /b /s

This should list all files with .doc extension in the current directory and all subdirectories. /b makes the output display just directories and files, and /s specifies to search subdirectories.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Thanks but now if I have to move them to a destination folder? Sorry to bother you again. – SUBHABRATA BANERJEE May 01 '15 at 13:09
  • Thanks, but I wanted to move from MS-DOS, move/MOVE/REN commands not helping. – SUBHABRATA BANERJEE May 01 '15 at 13:29
  • That's another question :) ... but look at [`shutil.move()`](https://docs.python.org/2/library/shutil.html#shutil.move), or [`os.rename()`](http://docs.python.org/library/os.html#os.rename). You can run the dos command via `subprocess.check_output()`, collect the output and then move the files in Python. For a pure DOS solution see this answer: http://stackoverflow.com/a/246576/21945 and use `move` or `ren` instead of `copy`. – mhawke May 01 '15 at 13:31
  • DOS is a single-tasking OS written in 16-bit x86 assembly, with an API based on `INT 21H` software interrupts, a command-line shell named COMMAND.COM, and a GUI named Windows (2.x). The last release was MS-DOS 8.0 in Windows ME, but by then it's heavily augmented by 32-bit VxDs. Modern Windows doesn't run any DOS code. It's written in C/C++; uses the NT kernel and Win32 API; the GUI is the main shell (explorer.exe, shell32.dll) and the command-line shell is cmd.exe (attached to a conhost.exe window). cmd.exe came from OS/2; inherits a lot from COMMAND.COM; but it's not DOS. – Eryk Sun May 01 '15 at 20:41
  • @eryksum: That's all true, but what are you saying? Can't the OP run a command in a `cmd.exe` shell? (I retained DOS terminology mainly because the OP refers to it). – mhawke May 02 '15 at 01:56
  • I just want you to change the wording to something like "cmd shell command" instead of "DOS command". – Eryk Sun May 02 '15 at 02:41
  • Also, consider using `Popen` and looping over `stdout`, rather than aggregating the results in string via `check_output`. – Eryk Sun May 02 '15 at 02:45