CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 45
  1. #1
    Join Date
    Aug 2019
    Posts
    56

    How to convert const std::filesystem::directory_entry to tchar?

    I'm trying to convert const std::filesystem:irectory_entry (dirent) to tchar but I don't understand how it an be done. I tried a lot of ways. Can you help me?

    Code:
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    
    namespace fs = std::filesystem;
    
    int main() try
    {
        std::for_each(fs::recursive_directory_iterator("./foo/"), {},
            [](fs::directory_entry const& dirent)
        {
            if (fs::is_regular_file(dirent) &&
                dirent.path().filename() == "black.txt")
                TCHAR path[_MAX_PATH];
                const std::filesystem::directory_entry &entry = *path;
            _tcscpy(path, dirent);
    
        });
    }
    catch (fs::filesystem_error const& e)
    {
        std::cerr << "error: " << e.what() << '\n';
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to convert const std::filesystem::directory_entry to tchar?

    .filename() returns a type path. To access this as a 'c-style' string, use .c_str(). So something like this (not tried):

    Code:
    _tcscpy(path, dirent.path().filename().c_str());
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Quote Originally Posted by 2kaud View Post
    .filename() returns a type path. To access this as a 'c-style' string, use .c_str(). So something like this (not tried):

    Code:
    _tcscpy(path, dirent.path().filename().c_str());
    Unfortunately it shows errors: https://imgur.com/a/wOhD5ln and "path" is undeclared identifier

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to convert const std::filesystem::directory_entry to tchar?

    This compiles as Multi-byte character set:

    Code:
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    
    namespace fs = std::filesystem;
    
    int main()
    {
    	try {
    		std::for_each(fs::recursive_directory_iterator("./foo/"), {},
    			[](fs::directory_entry const& dirent)
    		{
    			if (fs::is_regular_file(dirent) &&
    				dirent.path().filename() == "black.txt") {
    				TCHAR path[_MAX_PATH];
    				_tcscpy(path, dirent.path().filename().string().c_str());
    
    			}
    
    		});
    	}
    	catch (fs::filesystem_error const& e)
    	{
    		std::cerr << "error: " << e.what() << '\n';
    	}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Quote Originally Posted by prako2 View Post
    Unfortunately it shows errors: https://imgur.com/a/wOhD5ln and "path" is undeclared identifier
    Have a look at the Format observers section in https://en.cppreference.com/w/cpp/filesystem/path
    Victor Nijegorodov

  7. #7
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Quote Originally Posted by 2kaud View Post
    This compiles as Multi-byte character set:

    Code:
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    
    namespace fs = std::filesystem;
    
    int main()
    {
    	try {
    		std::for_each(fs::recursive_directory_iterator("./foo/"), {},
    			[](fs::directory_entry const& dirent)
    		{
    			if (fs::is_regular_file(dirent) &&
    				dirent.path().filename() == "black.txt") {
    				TCHAR path[_MAX_PATH];
    				_tcscpy(path, dirent.path().filename().string().c_str());
    
    			}
    
    		});
    	}
    	catch (fs::filesystem_error const& e)
    	{
    		std::cerr << "error: " << e.what() << '\n';
    	}
    }
    Thank you, it compiles now. But I encountered more global problem. I'm using v141_xp in vs2017 and I'm getting >c:\program files (x86)\microsoft sdks\windows\v7.1a\include\objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier' if I add <windows.h>. If using v141 then windows.h is not found at all. And I can't find any solution for this.

  8. #8
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Quote Originally Posted by VictorN View Post
    Have a look at the Format observers section in https://en.cppreference.com/w/cpp/filesystem/path
    Thanks, I was looking to it before.

  9. #9
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    I solved windows.h problem by setting up conformance mode to no. I'm trying a code with copy function of windows.h but it doesn't work (path variable).
    Code:
    #include "pch.h"
    #include <Windows.h>
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    
    namespace fs = std::filesystem;
    
    int main()
    {
    	try {
    		std::for_each(fs::recursive_directory_iterator("d:/folder/"), {},
    			[](fs::directory_entry const& dirent)
    		{
    			if (fs::is_regular_file(dirent) &&
    				dirent.path().filename() == "black.txt") 
    				fs::copy(dirent, "d:/folder/");
    				TCHAR path[_MAX_PATH];
    				_tcscpy(path, dirent.path().filename().string().c_str());
    				CopyFile(
    					path,
    					"d://folder//red//white.txt", // Hardwire the path\filename
    					TRUE // Do not overwrite it if it already exists.
    				);
    			}
    
    		);
    	}
    	
    	catch (fs::filesystem_error const& e)
    	{
    		std::cerr << "error: " << e.what() << '\n';
    	}
    }
    I want to copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt but it doesn't work.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to convert const std::filesystem::directory_entry to tchar?

    Quote Originally Posted by prako2 View Post
    Code:
    ...
    		CopyFile(
    					path,
    					"d://folder//red//white.txt", // Hardwire the path\filename
    					TRUE // Do not overwrite it if it already exists.,
    ...
    I want to copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt but it doesn't work.
    Did you probably meant "d:/folder/red/white.tx"?
    Or "d:\\folder\\red\\white.tx"?
    Victor Nijegorodov

  11. #11
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    For me // works everywhere, fs::copy(dirent, "d://folder//"); works well but not in
    CopyFile(
    path,
    "d://folder//red//white.txt", // Hardwire the path\filename
    TRUE // Do not overwrite it if it already exists.,
    but if not "path" variable CopyFile also works. For clearence I changed everything to // and I get the same results
    Code:
    #include "pch.h"
    #include <Windows.h>
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    
    namespace fs = std::filesystem;
    
    int main()
    {
    	try {
    		std::for_each(fs::recursive_directory_iterator("d://folder//"), {},
    			[](fs::directory_entry const& dirent)
    		{
    			if (fs::is_regular_file(dirent) &&
    				dirent.path().filename() == "black.txt")
    				fs::copy(dirent, "d://folder//");
    				TCHAR path[_MAX_PATH];
    				_tcscpy(path, dirent.path().filename().string().c_str());
    				CopyFile(
    					path,
    					"d://folder//red//white.txt", // Hardwire the path\filename
    					TRUE // Do not overwrite it if it already exists.
    				);
    			}
    
    		);
    	}
    	
    	catch (fs::filesystem_error const& e)
    	{
    		std::cerr << "error: " << e.what() << '\n';
    	}
    }
    / Also works for me in fs::copy(dirent, "d:/folder/");
    Last edited by prako2; August 10th, 2019 at 02:26 AM.

  12. #12
    Join Date
    Nov 2018
    Posts
    120

    Re: How to convert const std::filesystem::directory_entry to tchar?


  13. #13
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    I just debugged and it says that imgur.com/a/Ul4f4ua and I can't understand why

  14. #14
    Join Date
    Aug 2019
    Posts
    56

    Re: How to convert const std::filesystem::directory_entry to tchar?

    a.txt was in my "folder". Now I deleted it and now it shows that "path" is equal to some weird symbols

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to convert const std::filesystem::directory_entry to tchar?

    using / in a path name may work in some places, but \ is the path separator in windows. And in a non-raw text string, then \\ is used. So

    "d://folder//" becomes "d:\\folder\" and "d://folder//red//white.txt" becomes ""d:\\folder\\red\\white.txt". If / is used for a path, then you don't use //.

    I want to copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt if white.txt does not exist
    Consider:

    Code:
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    const fs::path initpath {"c:\\myprogs\\folder\\"};
    const fs::path destname {"c:\\myprogs\\folder\\red\\white.txt"};
    const fs::path req_fn {"black.txt"};
    
    int main()
    {
    	for (const auto& fn : fs::recursive_directory_iterator(initpath))
    		if (fs::is_regular_file(fn) && fn.path().filename() == req_fn) {
    			fs::copy_file(fn.path(), destname, fs::copy_options::skip_existing);
    			break;
    		}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured