I have a username in a string.
Now I would like to add that string in an WinExec line:
How do I include that string in the WinExec line above?Code:WinExec("MKDIR C:\\Documents & settings\\ <UserName> \\New Dir");
I have a username in a string.
Now I would like to add that string in an WinExec line:
How do I include that string in the WinExec line above?Code:WinExec("MKDIR C:\\Documents & settings\\ <UserName> \\New Dir");
Well, you'll probably have to create a string with what you want and then pass that string to WinExec. With C-style strings and functions, you could use something like this:
With C++ strings, you could get away withCode:char str[100]; sprintf(str, "MKDIR C:\\Documents & settings\\%s\\New Dir", username); WinExec(str);
Or you could use stringstreams. See the FAQ.Code:std::string str = "MKDIR C:\\Documents & settings\\ " + username + " \\New Dir"; WinExec(str.c_str());
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Well, first of all, you should not be using WinExec:
http://msdn2.microsoft.com/en-us/library/ms687393.aspx
Anyway, there are a few ways of handling the string. One is to use std::string and the other is the C approach with chars.Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.
If you use the C approach you can allocate enough memory to hold the entire string, and then call sprintf() to write the string to the newly allocated block of memory. Then pass to the function.
If you use std::string, then it's a matter of adding the first part with the second part, etc. etc.. This is the preferred C++ approach. Something like this:
This is extremely simplified, however.Code:std::string path = "MKDIR C:\\Documents & settings\\"; std::string user = "John"; std::string full = path + user;
BTW, for WinExec, you need another argument. Something like SW_SHOW should be your second argument.
All in all, I would recommend you use CreateProcess() or some other appropriate function rather than WinExec().
Edit: Bah, beaten again. :'(
And the best option may be "ShellExecute" rather than "CreateProcess", as the former is a more "generic" function.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Ok,
I wrote this:
This should move the file.doc to Desktop, but it does not.Code:string com = "MOVE \"C:\\Documents and Settings\\"+UserName+"\\file.doc\" \"C:\\Documents and Settings\\"+UserName+"\\Desktop\""; const char *p; p=com.c_str(); WinExec(p,SW_HIDE);
Would ShellExecute or CreateProcess work better here? How would I write the command line then?
Probably because MOVE isn't a real program, but a shell command.
ShellExecute() may be what you want. Otherwise, some might argue the more "correct" approach would be to programatically move the file yourself by copying the file over, and then, upon verifying the copy worked successfully, you delete the original.
Ok, I rewrote the line, but I cannot get it work:
Shall I use"open" or "MOVE" as second parameter?Code:string com = "MOVE \"C:\\Documents and Settings\\"+UserName+"\\file.doc\" \"C:\\Documents and Settings\\"+UserName+"\\Desktop\""; const char *p; p=com.c_str(); ShellExecute( NULL, "MOVE", p, NULL, NULL, SW_SHOW );
Or, use the MoveFile Win32 function, perhaps?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I can not get MoveFile() working:
No compilation errors, but the files does not move. What´s wrong?Code:string src = "C:\\Documents and Settings\\"+UserName+"\\file.doc"; string dst = "C:\\Documents and Settings\\"+UserName+"\\Desktop"; const char *p; p=src.c_str(); const char *q; q=dst.c_str(); MoveFile(p, q);
Check the return value of MoveFile() and call GetLastError() if it returns 0.
Although, tbh, I think you need to specify the actual name of the new file, not just the folder you want to move it to.
What does UserName contain? What do src and dst look like when you print them?
BTW, thisCode:cout << "src: [" << src << "]\n"; cout << "dst: [" << dst << "]\n";
is equivalent to this:Code:const char *p; p=src.c_str(); const char *q; q=dst.c_str(); MoveFile(p, q);
Code:MoveFile(src.c_str(), dst.c_str());
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It works if I just set the src and dst variables to:
Not if I run this:Code:string src = "x.txt"; string dst = "y.txt";
So something might be wrong with the path. But what?Code:string src = "x.txt"; string dst = "C:\\Documents and Settings\\"+UserName+"\\Desktop";
Hmm, now it seems to work.
Thanks!