-2

I have two folders in my system, one is "Image folder (contains images)" and 2nd is "Text folder (contains text files)". In these folder, few images and text files have the same names like: abc.jpg and abc.txt.

Actually, I want to find out the text file from the 2nd folder using input image name (or I want to match the text files name with the image names).

Thereafter, I wants to copy the matched text file into the "Image folder".

I am working on WINDOWS operating system.

Miki
  • 40,887
  • 13
  • 123
  • 202
  • 3
    That's what you want. The important thing to address here is **what you've tried**. This is a C++ question. Where's your C++ code? – tadman Oct 18 '17 at 05:21
  • Search for `how to get files name in a directory` then search for `compare string file names` then search for `how to copy files` then your done. – Farhad Oct 18 '17 at 05:22
  • 1
    It is hard to provide any hint if even the operational system platform was not provided. Please improve your question with the required information. By the way, see this link to get familiar with StackOverflow mechanics: https://stackoverflow.com/tour – Duloren Oct 18 '17 at 05:53
  • You should add a Windows tag – Basile Starynkevitch Oct 18 '17 at 06:17
  • 2
    What sort of role do you envisage for OpenCV in processing filenames? It is an image processor. – Mark Setchell Oct 18 '17 at 06:20
  • Maybe this [link](https://stackoverflow.com/a/612112/4603670) will help – Barmak Shemirani Oct 18 '17 at 06:24
  • BTW, switching to Linux could be profitable to you: it is mostly free software and you have many free software whose source code can be a useful example – Basile Starynkevitch Oct 18 '17 at 06:31

1 Answers1

2

If your issue is to find two different file names (but with similar basenames), notice that:

  • directories and folders are unknown to the C++11 or C++14 standard. Future C++17 standard might provide a filesystem library (but you won't find a mature implementation easily today)

  • POSIX and Windows has directories (not folders). You could use (notably on Linux or MacOSX) POSIX functions like opendir(3), readdir(3), closedir(3) combined with stat(2) to explore them, or use some higher level library functions like nftw(3).

  • basename(3) could be useful, but you can use string functions once you know that / is used as a directory separator.

  • some framework libraries, notably Qt, POCO, Boost, .... provide useful functions on directories and may give a common abstraction of them usable on several operating systems. Actually I recommend using a framework library, because it is easier and more portable.

The notion of file, of file systems, and of directory is very operating system specific (and some academic OSes don't have them and provide a different notion of persistence). Read Operating Systems: Three Easy Pieces (freely downloadable) for an overview. On Linux and POSIX systems, a file is really some i-node, a directory is a kind of file having entries mapping names to i-nodes, and a file could have several names in various directories (e.g. using link(2)). The C++ standard knows about standard streams, e.g. thru its input/output library.

Copying a file generally means to copy its content (byte by byte) so is not an elementary operation. In practice better copy large blocks of at least 16 kilobytes. Some libraries provide functions to copy files.

On Windows (which I don't know) the notion of file and "folder" is different, and the directory separator is \. You need to dive into Microsoft documentation. Even Microsoft documentation speaks of directories. But using a framework library would be simpler (and more portable).

BTW, the terminology of folder is generally wrong. You see some folders (not all of them) on your GUI or desktop environment, but the OS (and your program) knows about directories and files.

Sometimes, using some higher-level abstraction than files is useful. For example the SQLite library provides you with some database abstraction, GDBM gives indexed files, and you might consider using some database system like PostGreSQL or MongoDB, etc etc.... YMMV.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547