Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28459 Discussions

invisible execution of system commands? (ie, no console flashing)

forall
Beginner
883 Views
Is it possible to execute system commands (eg, 'rename "file1" "file2"') without having the system console window flash? I have a code that has to do such operations repeatedly while updating other graphics, and the flashing of the console essentially obliterates them..

I tried 'SYSTEM' and 'SYSTEMQQ' - they both execute the command correctly but flash the console.

I also tried 'createProcess' - it does not seem to execute the command at all (though, for running EXE files I succesfully use 'createProcess' to run them with the 'SW_HIDE' option)

Any ideas?

thanks in advance.
dmitri
0 Kudos
6 Replies
Steven_L_Intel1
Employee
883 Views
Use the Windows API MoveFileEx instead of trying to execute a command. In general, I find ShellExecute to be a better method of running commands than SYSTEMQQ, etc., as there is no console window created automatically.
0 Kudos
SergeyKostrov
Valued Contributor II
883 Views
Quoting forall
...
Is it possible to execute system commands (eg, 'rename "file1" "file2"') without having the system console
...
I also tried 'createProcess' - it does not seem to execute the command at all (though, for running EXE files I succesfully use 'createProcess' to run
them with the 'SW_HIDE' option)

Any ideas?

In a similarcase I use an additional Bat-file and a 'CreateProcess' Win32 API function successfully executes it.
The Bat-file looks like:

...

@echo off

attrib -R ..\Include\*.h > nul
copy Stdphf.h ..\Include\*.h > nul
attrib +R ..\Include\Stdphf.h > nul

attrib -R ..\..\Include\*.h > nul
attrib -R ..\..\Include\*.cfg > nul

copy Set.cfg ..\..\Include\*.cfg > nul
copy Test.cfg ..\..\Include\*.cfg > nul
copy Algorithm.cfg ..\..\Include\*.cfg > nul

copy AlgorithmSet.h ..\..\Include\*.h > nul
...

And in order to hide a console windowa 'STARTUPINFO' structureis used with the 'CreateProcess' Win32 API function.

A Bat-file based solution allows to isolate codes from some external operations and there is no need to re-compile
the source codes if something needs to be changed.

Best regards,
Sergey

0 Kudos
forall
Beginner
883 Views
thanks very much Steve and Sergey,

In my case, the names of the files (and the command to be executed) could be changing, so I guess instead of just constructing a different command on the fly in Fortran I would need to write some code that writes the bat-files?


Steve - is there an example of using shellExecute to run system commands? I quite routinely use it to for things like opening files in notepad etc (based on your great examples!), but I have never been able to use it for system commands.

Say, for the previous example of the 'rename file1.txt file2.txt': I tried passing this as the filename input into shellexecute but got return code = 2 from shellexecute. I also tried using 'file1.txt file2.txt' as the parameters, with no luck either .. Is there a general approach for system commands such as these?

** I also tried shellExecute(filename='path/cmd.exe',params='rename "file1" "file2"' etc) but this did not work either (Retcode indicated success but the rename command was not executed). Adding full paths to the files didnt work.

any hints?

thanks
dmitri
0 Kudos
SergeyKostrov
Valued Contributor II
883 Views
Hi Dmitri,

Quoting forall
...
Say, for the previous example of the 'rename file1.txt file2.txt': I tried passing this as the filename input into shellexecute but
got return code = 2 from shellexecute. I also tried using 'file1.txt file2.txt' as the

[SergeyK] Here isa descriptionof the error code 2:

The system cannot find the file specified (ERROR_FILE_NOT_FOUND ).

parameters, with no luck either .. Is there a general approach for system commands such as these?
...
any hints?

thanks
dmitri


I use relative paths inmyBat-file and itis executed froma strictly specifiedfolder. I remember that I had lots
of problems related to attempts to executecommands from a"wrong" folder.I think that absolute paths to some files or
commands could be used instead.

Best regards,
Sergey

0 Kudos
IanH
Honored Contributor II
883 Views
Quoting forall
** I also tried shellExecute(filename='path/cmd.exe',params='rename "file1" "file2"' etc) but this did not work either (Retcode indicated success but the rename command was not executed). Adding full paths to the files didnt work.

Have a close look at the command line syntax of cmd.exe. You probably need to have parameters of

/c ren file1 file2

Batch files are fragile. If you are just renaming a file then use the Windows API as already mentioned, or the RenameFileQQ IFPORT portability function.

0 Kudos
forall
Beginner
883 Views
excellent, using the '/c ren file1 file2' syntax werks!

thanks for the help. I understand that the API's are often preferable, and I already use them here and there within the limits of my understanding.

But sometimes I just want to quickly run a system command without working out API's etc - using which cannot be constructed 'on-the-fly' in the same way as system command strings such as above.

What I didnt realise - and paid for handsomely in confusion - was that calling CMD.EXE has different syntax for the same command than the DOS shell... slightly inconvenient I guess but at least its still a string :-)

thanks again!
0 Kudos
Reply