Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI
Hello everybody,
 
I have from my console Qt application a .bat file which calls a Windows PowerShell script file. But th exit code is always 1 instead of 0.
 
Here is the code:
 
Console App:
QFileInfo fileInf("listFiles.bat");
QStringList arguments;
arguments<< "/c"<< fileInf.absoluteFilePath()<<sFolderName;
QProcess *process = new QProcess();
exitCode = process->execute("cmd.exe ",arguments);
 
where sFolderName is for example D:\Work
 
Script listFiles.bat:
 
cmd.exe /c powershell.exe -noprofile -ExecutionPolicy bypass -file get_filelist_only.ps1 %1
 
where %1 is supposed to take the value of sFolderName.
 
Script get_filelist_only.ps1:
 

Param
(
[string] $Dir
)
$currentLocation = $pwd
set-location $Dir
 
$items = gci $Dir -recurse
 
# retrieving the list of files
$file_items = foreach ($item in $items) {
if ($item.GetType().FullName.Equals("System.IO.FileInfo")) {
$item
}
}
 
if (test-path "contents.txt")
{
rm "contents.txt"
}
 

 
function GetRelativePath($fullName)
{
$len = $currentPath.Length
$len = $len + $lastVersionDir.Length
$relativePath = $fullName
$relativePath
}
 
# writing the files
foreach ($item in $file_items)
{
$item.Name
 
$relativePath = GetRelativePath($item.FullName)

Out-File -Append -FilePath "contents.txt" -InputObject $relativePath -encoding ASCII
}
 

set-location $currentLocation
echo "done!"
 

So, in fact I am passing the directory argument from script in order to list the files of a directory in the file contents.txt
 
I have tried from command line cmd.exe /c listFiles.bat D:\Work and it works fine. It calls the PowerShell script and it creates the file contents.txt in D:\Work.
 
In fact I am arriving in a Qt function:
void QProcessPrivate::findExitCode()
{
    DWORD theExitCode;
    if (GetExitCodeProcess(pid->hProcess, &theExitCode)) {
        exitCode = theExitCode;
        //### for now we assume a crash if exit code is less than -1 or the magic number
        crashed = (exitCode == 0xf291 || (int)exitCode < 0);
    }
}
 
and GetExitCodeProcess is a Windows API function.
 
which returns exitCode 1. I guess something is wrong with my parameters from my program because on my program console it appears the following:
D:\AutomateTest>cmd.exe /c powershell.ex
e -noprofile -ExecutionPolicy bypass -file get_filelist_only.ps1 D:/Work
'cmd.exe' is not recognized as an internal or external command,
operable program or batch file.
 
I don't understand what I am doing wrong. Also I have provided the full path for the cmd.exe but it does not work.
 
Also I have searched on google and on Qt forums but without success.
Thanks for your help.
Posted 6-Nov-12 5:14am
Edited 6-Nov-12 5:25am
v3
Comments
Richard MacCutchan at 6-Nov-12 11:17am
   
The environment for the batch process must be missing part, or all, of its PATH variable. Try a different batch script that lists all environment variables to see why.
Adriana Frunza at 7-Nov-12 2:14am
   
Thanks a lot for your help. Indeed it was a problem of path. I had to put the entire path to cmd.exe and powershell.exe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100