tags:

views:

160

answers:

2

I am having a windows application interacting with DB2 database. In my application i have code to execute some DB2 commands through command line interface. I have used windowsAPI "ShellExecuteEx()" to execute those DB2 commands through command line.

Following is the code written to execute DB2 command through command line.

  string command = "/c /w /i DB2 UNCATALOG NODE DB_DATABASE ">>" test.log | echo 
  %date% %time% >> test.log";
  SHELLEXECUTEINFO shellInfo;
  ZeroMemory(&shellInfo, sizeof(shellInfo));
  shellInfo.cbSize = sizeof(shellInfo);
  shellInfo.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
  shellInfo.lpFile = "db2cmd";
  shellInfo.lpParameters = command.c_str();
  if (ShellExecuteEx(&shellInfo)) 
    { 
      WaitForSingleObject(shellInfo.hProcess, INFINITE); 
      CloseHandle(shellInfo.hProcess); 
      bStatus = true; 
    } 
    else 
    { 
           bStatus = false; 
    } 

The code is executed successfully , however if test.log is observered i only get result of DB2 command and not date and time. If you see the above command there is "|" pipe operator and echo command to log date and time in test.log

Please note that if I execute above DB2 command through separately command line i.e. not through code. I am able to view date and time log along with DB2 command result in test.log. Following is the full command which i executed through command line. DB2CMD /c /i /w DB2 UNCATALOG NODE DB_DATABASE ">>" test.log | echo %date% %time% >> test.log

According to me since DB2 command is executed successfully through code, there is problem with only usage of "|" pipe operator or echo command.

A: 

The pipe operator is executed by a command shell. The easiest way to get that functionality would be to use the system() library function.

Otherwise, you could programatically append the timestamp to the file after the command is done (revised):

system ("db2cmd DB2 UNCATALOG NODE DB_DATABASE >>test.log");
system ("echo  %date% %time% >> test.log");
wallyk
Thanks for your comments and suggesting a new approach. However if you see the code carefully. Command fired is "db2cmd" and argument passed to it is "/c /w /i DB2 UNCATALOG NODE DB_DATABASE ">>" test.log | echo %date% %time% >> test.log"; So please also suggest how we can possibly do the same using system library and it would be great if you can suggest what is the problem i m getting while using "|" pipe operator and echo.Thanks
decisive
I expected to find something like db2cmd, but didn't see it. I've corrected the example.
wallyk
Thanks for suggesting this approach. However, it would be nice if you can really tell what i am doing wrong in using "|" operator in code that i am not able to get date time in log file
decisive
@decisive: giving both `|` and `>>` to *db2cmd* makes no sense. Should the output of the program go to the end of `test.log`, or should it be input to the `echo` command? Since the latter makes no sense, I changed it from a pipeline to separate commands.
wallyk
+2  A: 

The pipe mechanism is used to pass output of one program as input to the next program. I guess in your case the && separator would be more appropriate.

Try and let cmd handle the execution of multiple statements:

cmd /c "db2cmd ... && echo ..."
devio
decisive
David Rodríguez - dribeas