Problem with copying all files from one folder into an other folder in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sailordi
    New Member
    • Mar 2014
    • 3

    #1

    Problem with copying all files from one folder into an other folder in c++

    I’m trying to make a program that will copy all files from one folder in to another folder.
    Ex path:
    From: G:\\Ex\\ExF1
    To: C:\\EXAll
    I’m using Visual studio express C++ as the IDE.
    I have tried using this code:

    bool copy_functions: :CopyAll(string InPath,string OutPath)
    {
    ifstream In(InPath.c_str (),ios::in|ios: :binary);
    ofstream Out(OutPath.c_s tr(),ios::out|i os::binary);

    if(In.is_open() == false) { cout<<InPath<<" could not be opened for copying\n\n"; }

    if(Out.is_open( ) == false) { cout<<OutPath<< " could not be opened for copying\n\n"; }

    if(In.is_open() == false || Out.is_open() == false)
    {
    getchar();
    return false();
    }

    In.seekg(0,ios: :end);
    long FileSize = In.tellg();

    short* Buffer = new short(FileSize/2);

    In.seekg(0,ios: :beg);

    In.read((char*) Buffer,FileSize );
    Out.write((char *)Buffer,FileSi ze);
    delete[] Buffer;

    In.close();
    Out.close();

    return true;
    }
    But I always get booth could not be opened for copying messages even thug I use the absolute paths for the folders.
    Please help;
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Are G:\\Ex\\ExF1 and C:\\EXAll individual files? The stream objects only work for one file. If ExF1 and EXAll are folder names, then there is nothing to open.

    Also, you will need to make OS calls to discover the names of the files in the folder. What the calls are is dependent on the operating system you are using.

    Comment

    • sailordi
      New Member
      • Mar 2014
      • 3

      #3
      G:\\Ex\\ExF1 and C:\\EXAll are the both folders.
      Im programing in Windows 7

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Here you go: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

        Comment

        • sailordi
          New Member
          • Mar 2014
          • 3

          #5
          I found this code to get a list of files in a folder:

          bool copy_functions: :CopyAll(string InPath,string OutPath)
          {
          char Path[500];
          WIN32_FIND_DATA Data;
          HANDLE Hd;

          cout<<"InPath "<<InPath<<"\n\ n";
          sprintf(Path,"% s*.*",InPath.c_ str());
          cout<<"Path "<<Path<<"\n\n" ;
          Hd = FindFirstFile(P ath,&Data);

          if(Hd == INVALID_HANDLE_ VALUE)
          {
          cout<<"Faild to find "<<InPath<<"\n\ n";
          return false;
          }

          do
          {
          if(!(Data.dwFil eAttributes & FILE_ATTRIBUTE_ DIRECTORY))
          {
          cout<<"Filename : "<<Data.cFileNa me<<"\n";
          }

          }while(FindNext File(Hd,&Data)) ;
          FindClose(Hd);
          return true;
          }
          It woorks if i use it in Dev c++, but if i try to use the same code i Visual studio c++ i get this error message:

          Error 1 error C2664: 'HANDLE FindFirstFileW( LPCWSTR,LPWIN32 _FIND_DATAW)' : cannot convert argument 1 from 'char [500]' to 'LPCWSTR'

          Why do get this error message?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            This is the prototype of FindFirstFile:

            Code:
            HANDLE WINAPI FindFirstFile(
              _In_   LPCTSTR lpFileName,
              _Out_  LPWIN32_FIND_DATA lpFindFileData
            );
            Notice he first argument is a LPCTSTR. That is a long pointer to a TCHAR string. TCHAR?

            You will discover that FindFirstFile is not a function. It is a macro that expands to FindFirstFileA if your project is ANSI or to FindFirstFileW if your project supports Unicode.

            Your error is for FindFirstFileW so I know you have a Unicode project. The LPCTSTR has resolved to an LPCWSTR (long pointer to a const wchar_t string a.k.a a wide string.

            You pass in a char* and not an LPCWSTR. Hence your error.

            You can convert a C-style string to a Unicode string by calling MultiByteToWide Char. This is how you get the LPCWSTR:
            http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

            This can help you with TCHAR: http://msdn.microsoft.com/en-us/library/cc842072.aspx

            The TCHAR is to allow you to support both ANSI and Unicode from the same source code. If you intend to only use Unicode, then I would code FindFirstFileW which will skip all the TCHAR stuff.

            You will need to do some study here.

            Comment

            Working...