Solved How to use QProcess to start smbclient when the filename with space.
-
OS: Debian for raspberry pi 3
I inputted the command
smbclient //192.168.3.50/d -U username -W ereryone password -c “get \”\data\win\image\0125 6-2.PNG\” /home/pi/Temp/004.png"on the terminal and it goes well.
But, it doesn't work when I using the QProecess in the following code
QString order = “smbclient //192.168.3.50/d -U username -W ereryone password -c \“get \”\data\win\image\0125 6-2.PNG\” /home/pi/Temp/004.png\””
QProcess *process = new QProcess(this);
process->start(order);
process->waitForStarted(); //return true
process->waitForFinished(); //return truethe error message showed the path just include \data\win\image\0125 and the other are lost.
Someone can help me or give me some suggests.
Thanks in advance.
-
@LeonSu
Your code does not show how you pass yourorder
variable to yourQProcess
. Until we know what this, we cannot help. -
@LeonSu Definition of order is not correct
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c \"get \"\data\win\image\0125 6-2.PNG\" /home/pi/Temp/004.png\""
becomes
smbclient //192.168.3.50/d -U username -W ereryone password -c "get "\data\win\image\0125 6-2.PNG" /home/pi/Temp/004.png"
Quoted string in order are :
"get " " /home/pi/Temp/004.png"
This version should work better:
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c get \"\data\win\image\0125 6-2.PNG\" \"/home/pi/Temp/004.png\""
-
@Gojir4
How is that going to work correctly, given that you & he have a literal C++ string with (unprotected)\
s in it?Come to that,
the error message showed the path just include
\data\win\image\0125
Lucky if that happened. Perhaps you're just fortunate on
\d
,\w
,\i
&\0125
not happening to correspond to any C chars.... -
@JonB Right, I missed that, sry.
So, this way :QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c get \"/data/win/image/0125 6-2.PNG\" \"/home/pi/Temp/004.png\""
-
Although the above solves the "backslash" issue by using "forward slashes" instead (assuming that is acceptable to
smbclient
, I don't know), from what I can see elsewhere aboutsmbclient
command-line it may well not work.From the examples I have seen everywhere, including what @LeonSu wrote worked on the command line, I believe
smbclient -c
requires the following command to be a single argument, and hence be quoted. @Gojir4 's suggestion is passing two arguments, so I presume will likely not be acceptable.For the particular paths you have, I believe you should try:
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c \"get \\\"/data/win/image/0125 6-2.PNG\\\" /home/pi/Temp/004.png\""
or you could try one of
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c \"get '/data/win/image/0125 6-2.PNG' /home/pi/Temp/004.png\"" QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c 'get \"/data/win/image/0125 6-2.PNG\" /home/pi/Temp/004.png'"
Even then I am unsure about who does the unquoting, so that may not be right.
-
You really should pass the arguments with QProcess::setArguments() and not directly to start() - so you can be sure the separate arguments are treated as separate.
-
thanks everyone
I try to simplify the problem. I put file "0125 6-2.PNG" into folder d and didn't set any save path.
The following command is work okay on the terminal
smbclient //192.168.3.50/d -U username -W ereryone password -c "get \"0125 6-2.PNG\""
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c get \"0125 6-2.PNG\" "
the error message say: get <filename>[localname] <== it lost the file name
and I continue to try more like following
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c get '0125 6-2.PNG' "
I got the same result.
I try
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c \"get \\\"0125 6-2.PNG\\\"\"" QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c \"get '0125 6-2.PNG'\""
The error message is 「NT_STATUS_OBJECT_NAME_NOT_FOUND onening remote file \0125」 for the above two case
andQString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c 'get \"0125 6-2.PNG\"'"
the error message is 「get <filename>[localname]」
I base on your method to try following code
QString order = "smbclient //192.168.3.50/d -U username -W ereryone password -c 'get '0125 6-2.PNG''"
The error message is display 「'get: command not found」
QString prog = "smbclient";
QStringList arguments;arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << "\"get 0125 6-2.PNG\"";
process->start(prog, arguments);
the error message is 「get 0125 6-2.PNG: command not found」and I continue try the following code
arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << " \"get \"0125 6-2.PNG\"\"";
the error message is 「get 0125: command not found」
arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << "\"get '0125 6-2.PNG'\"";
the error message is 「get '0125 6-2.PNG': command not found」
and
arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << "\"get" << "\"0125 6-2.PNG\"" <<"\"";
the error message is 「get <filename>[localname]」
I guess the method (QProcess::setArguments()) is very close answer.
Is anyone have idea?
thanks again.
-
@LeonSu said in How to use QProcess to start smbclient when the filename with space.:
get \"0125 6-2.PNG\"
Since this is one argument, why don't you pass it to setArguments as one argument?
-
thanks Christian-Ehrlicher
thanks everyonethe final code is following
QProcess *process = new QProcess(this); QString prog = "smbclient"; QStringList arguments; arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << "get \"0125 6-2.PNG\""; process->start(prog, arguments); process->waitForStarted(); process->waitForFinished();
thanks again;
@Christian-Ehrlicher
I don't know what different between using setArguments and without using setArguments.
The description from Qt documents is
「Set the arguments to pass to the called program when starting the process. This function must be called before start().」In this case, I try to add the order(process->setArguments(arguments)), and the result is the same with above code.
QProcess *process = new QProcess(this); QString prog = "smbclient"; QStringList arguments; arguments << "//192.168.3.50/d" << "-U" << "username" << "-W" << "workgroup" << "password" << "-c" << "get \"0125 6-2.PNG\""; process->setArguments(arguments); process->start(prog, arguments); process->waitForStarted(); process->waitForFinished();
Could you please show me some example.
thank you. -
setArguments(arguments) + start(cmt) and start(cmd, arguments) is exactly the same. I refered to your first try with start(cmd_with_args). In this case Qt must guess what a single argument is (and fails in your case because there are spaces inbetween which must be in one argument), therefore it is much better to not rely on this logic here to avoid this kind of confusion.