从目录中的每个文件夹读取文件 [英] Read files from every folder in a directory

查看:86
本文介绍了从目录中的每个文件夹读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,其中一部分涉及读取某些文件夹中的文件,以便将它们组合为单个文件.顶级文件夹是C:\\ PROGRAMS \\.

因此,例如,该目录中的一个文件夹通常看起来像:
C:\\ PROGRAMS \\ AAA-999a
其中包含文件...
\\ AAA-999a_Test1.txt
\\ AAA-999a_Test2.txt

这两个.txt文件在同一文件夹AAA-999a.txt中合并为一个.txt文件.

如果用户提供与文件夹名称匹配的字符串,我已经有了执行此操作的代码;在这种情况下为AAA-999a.

但是,PROGRAMS \\目录包含许多这样的文件夹,并且我希望能够通过目录中以AAA,BBB或CCC开头的目录中的每个文件夹进行调用,并使用我在上面描述的代码(文件夹可以是随时添加或删除,并且名称中可以包含-999a的不同名称,但始终以AAA,BBB或CCC开头.

我想我的问题是我真的不知道从哪里开始.任何代码,伪代码,或者只是一个如何入门的想法都很好.

谢谢!


我应该补充一点,这些文件与顶层目录Common.txt中的文件合并在一起. Common.txt在其代码中包含用于确定何时开始读取测试文件的标志.因此,将读取Common.txt的第一部分,直到找到//-Test1 Start-//,然后读取Test1文件.然后它将继续从Common.txt中读取,直到找到//-Test2 Start-//.然后读取Common.txt的其余部分.

I''m writing a program and part of it involves reading from files in certain folders in order to combine them into single files. The top level folder is C:\\PROGRAMS\\.

So for example one folder in this directory would generally look something like:
C:\\PROGRAMS\\AAA-999a
which contains the files...
\\AAA-999a_Test1.txt
\\AAA-999a_Test2.txt

These two .txt files are combined into a single .txt file in the same folder, AAA-999a.txt.

I already have the code that does this if the user gives the string that matches the folder name; in this case AAA-999a.

However, the PROGRAMS\\ directory contains many of these folders and I want to be able to make a call that runs through every folder in the directory beginning with AAA, BBB or CCC and uses the code I described above on them (folders can be added or removed any time, and can have a different -999a part of their name, but will always begin with AAA, BBB or CCC).

I guess my problem is I don''t really know where to start. Any code, pseudocode, or just an idea of how to get started on this would be great.

Thanks!


I should add that these files are combined with a file in the top level directory named Common.txt. Common.txt has flags in its code that are used to determine when to start reading the test files. So the first part of Common.txt is read until it finds //--Test1 Start--// then reads in the Test1 file. It will then continue reading from Common.txt until //--Test2 Start--// is found. Then the rest of Common.txt is read in.

推荐答案

如果文件并置在一起的顺序很重要,则可能需要创建某种类型的映射在对相关文件夹进行预扫描时.该映射可以按照您希望将它们缝合在一起的顺序包含文件路径的列表.此时,您只需要依次遍历路径列表并执行对输出文件的串联即可.
If the order that the files are concatenated together matters, you will likely need to create a map of some sort in a pre-scan of the folders in question. That map could contain a list of the file paths in the order that you wish to stitch them together. At that point you simply need to iterate through the list of paths in order and perform the concatenation to your output file.


在此处查看示例.这一步在VC ++中对目录进行递归搜索.您只需更改很少的代码即可使其适用于您的应用程序逻辑

如何使用Visual C ++递归搜索文件夹 [
Take a look at the sample here. This one does recursive search of the directory in VC++. You will have to change the code very little to get it working for your app logic

How to recursively search folders by using Visual C++[^]


您可以先查看 FindFirstFileEx [ ^ ]函数.两者都允许您按掩码查找文件/子文件夹,但是第二个允许您指定其他属性(在您的情况下,仅查找目录)
You can start by looking at FindFirstFile[^] or FindFirstFileEx[^] functions. Both allow you to find a file/subfolder by mask, but the second allows you to specify additional attributes (in your case, look for directories only)


这篇关于从目录中的每个文件夹读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆