1

I have a problem where I have a text file containing 30k filenames which I need to extract from a windows folder containing 100k other files.

It's a probably a pretty simple problem, but, I have extensively searched the web and cannot find a solution, maybe I am using the wrong terms.

Fine using Windows batch file, c#, Java, Python, whatever...

might be an idea to rename the files to something like xxfilename so I can order by filename?

Thanks in advance.

Fruitful
  • 597
  • 2
  • 9
  • 16
  • If the pattern of 30K filenames is similar, then you may use FileFilter to pick up the files from the folder in Java. – Sudhanshu Umalkar Mar 15 '13 at 02:57
  • Thanks Sudhanshu but they are totally random. – Fruitful Mar 15 '13 at 03:00
  • So the file contains the name of the files? Or you need to find the file, that contains the names, among thousands of other files? – Heinrich Mar 15 '13 at 03:00
  • Maybe an idea to rename 30k files based on their filename. – Fruitful Mar 15 '13 at 03:01
  • http://stackoverflow.com/questions/1009107/what-net-collection-provides-the-fastest-search – rism Mar 15 '13 at 03:02
  • If you just want to copy (extract) them to some other folder, then just read the filenames from the file, check if they exist in the windows folder, and copy them. Just a simple for loop should work. – Sudhanshu Umalkar Mar 15 '13 at 03:03
  • if you want to rename files in windows you just Ctrl-A, right mouse click and rename in Windows Explorer and it will auto generate suffix according to item order. – rism Mar 15 '13 at 03:04
  • Yeah Heinrich the file contains a list of filenames, that I need to somehow extract from a folder containing many more filenames than the ones on the list. – Fruitful Mar 15 '13 at 03:05
  • I'm not a very good programmer, so was hoping to maybe find a link to some code or an article with a similar walk through. – Fruitful Mar 15 '13 at 03:06
  • So in c# you just want a List, from System.Collections.Generics to store the data, File.Read from system.io to read the file names into the list then just iterate the list / or string[] doing a File.move. The folder containing a 100k files is irrelevant in terms of the program. – rism Mar 15 '13 at 03:07
  • Thanks Rism, I'll have a look through that and try to figure it out. – Fruitful Mar 15 '13 at 03:09

4 Answers4

2

Using c#:

foreach (var filePath in File.ReadAllLines(indexFile))
{
    if (File.Exists(filePath))
    {
        var destinationPath = Path.Combine(destinationRoot, Path.GetFileName(filePath));
        File.Copy(filePath, destinationPath);
    }
}
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
2

Using a batch file:

setlocal
set destination=c:\Temp\Destination

for /f %%f in (index.txt) do (
    copy "%%f" "%destination%"
)

endlocal
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
1

If the file contains the list of filenames you can just read them out and copy the files to your destination.

Something like:

string destination = "D:\\";
foreach (var filename in File.ReadAllLines("fileWithFilenames").Where(f => File.Exists(f)))
{
    File.Copy(filename, Path.Combine(destination, Path.GetFileName(filename)));
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
1

Something on these lines may be done ...

    BufferedReader reader = new BufferedReader(new FileReader("somefile"));
    String line;
    File file;

    File folder = new File("somefolder");
    List<String> files = Arrays.asList(folder.list());

    while((line = reader.readLine()) != null) {
        if(files.contains(line)) {
            file = new File(line);
            // do something with the file
        }
    }
Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33