PDA

View Full Version : Creating and executing cmd files



Momergil
21st August 2011, 17:28
Hello!

I have in mind the following "program" to do:

a guy click a button in, let us say, a MainWindow. A QDialog opens with an empty space to write text and a OK button comes together. In the white text field, the guy writes a windows or linux cmd code, and clicks "OK". When he does, the QDialog is closed, a function is called and creates a cmd file, put the text-code he wrote in it and its closed.

Later, the guy clicks a different pushbutton in the MainWindow and a list of various cmd files he created before is shown in a QListWidget. He selects one, clicks a "OK" and the cmd file in questions is called and executed.


My question is: how do I do that?

Essentially I don't think there will be a problem with the window and list part. Essentially I create a class responsible for each "cmd object" and in the MainWindow a vector of that class. That each time the OK button is pressed, an object of the "cmd class" is created and the vector executes a pushback() of that object. When the second PushButton is pressed and a QListWidget appears, it uses a for(;;) to take all the "cmd objects" created till than and present them as an Item of the list. When the "OK" button is pressed, a connect() with the MainWindow is "called" and a function is executed, namely a function that opens and executes the cmd file in question.


The problem is that I don't know how to do the create/execute part of the cmd file. I have a bit of experience in writing and reading .txt files with QFile and QTextStream, but I don't know how to do something similar with cmd files. Nor I know if the text-code put by the user in the white text place should be copied in a big QString and than put in the cmd, or if a should use another method.


Could somebody help me, please?

Thanks!


Momergil

wysota
21st August 2011, 17:30
If by "cmd files" you mean script files for command interpreters (such as cmd on Windows and Bash on Unix) then those are regular text files so you can use the same approach. As for executing them, either use the system() call or QProcess.

Momergil
23rd August 2011, 12:25
If by "cmd files" you mean script files for command interpreters (such as cmd on Windows and Bash on Unix) then those are regular text files so you can use the same approach. As for executing them, either use the system() call or QProcess.

So, in other words, I do exactly the same, only in the part where I define the file instead I write something as "./test/test.txt" I write "./test/test.cmd" and everything else is the same?

Thanks!

yeye_olive
23rd August 2011, 16:01
I do not understand your question. Please clarify. There is no such thing as a .cmd file anyway. Perhaps you should first learn how to write scripts for the command interpreter. Under Windows, you will use the .bat extension. Under GNU/Linux, you will need to make the file executable and have it start with the proper shebang.

wysota
23rd August 2011, 16:03
Yes, it's the same. At least regarding the part where you define scripts.

Momergil
24th August 2011, 19:07
I do not understand your question. Please clarify. There is no such thing as a .cmd file anyway. Perhaps you should first learn how to write scripts for the command interpreter. Under Windows, you will use the .bat extension. Under GNU/Linux, you will need to make the file executable and have it start with the proper shebang.

yeye, now I know that there is not such thing as a .cmd file ^^ I though there was, but I saw a video about this .bat in YouTube and now I know. Thanks! Now, about Linux, what exactly should I do?


Yes, it's the same. At least regarding the part where you define scripts.

O.K., so I just change the file type from txt to .bat. Thanks!

yeye_olive
24th August 2011, 20:49
Now, about Linux, what exactly should I do?
You need to do two things:
1. Add a line (the so-called "shebang") at the beginning of the script specifying which interpreter should run it; here is an example of a Hello world script for sh:


#!/bin/sh
echo "Hello world"

Depending on what you want to do you might leave that to the user of your application.
2. Make the script executable by its owner. See QFile::setPermissions().

Momergil
13th September 2011, 15:55
You need to do two things:
1. Add a line (the so-called "shebang") at the beginning of the script specifying which interpreter should run it; here is an example of a Hello world script for sh:


#!/bin/sh
echo "Hello world"

Depending on what you want to do you might leave that to the user of your application.
2. Make the script executable by its owner. See QFile::setPermissions().

yeye,

thanks for the help, and thanks for all the others.


God bless,

Momergil.