How to expand "*.*" into a list of files? (Windows console C) - console-application

I've got a Visual C++ console application which parses one or more files. To parse all N .txt files I'd like to call it with program.exe *.txt and have that expanded into a list in argv[1] to argv[N]. Instead, I see *.txt in argv[1].
Thanks,
Borge

Related

Terminal Command Output to array in CPP

I have a folder and subfolder which contain many HTML files. I want to store all the html file paths to an array. I am using C++ and ubuntu.
I know a terminal command - find . -name *.html which gives me all the html file paths.
I want to use these paths to create PDF of these HTML files using WKHTMLTOPDF and threading. How to store these paths and use it?
You could brute-force it by using std::system
http://en.cppreference.com/w/cpp/utility/program/system
to execute your find command and use the output in a C++ program. Or read up on file system traversal in Steven's APUE ('Advanced Programming in the Unix Environment) and do it yourself. Begin with man 3 stat.

How to write the contents of the console to a file in C++ or C, multiple process/exe may write to console

I have multiple exes(one exe call other exe, another exe may call another....) which are writing something to console. I would like to copy the displayed contents of console to a text file. In C++ or C. not manually by right click and mark paste. If same happens through code it will be a great help.
C or C++
Write the name of the exe file in the console/command prompt along with greater than symbol and the file name in which you want the output.
Ex. Considering "abc.exe" the executable file.
write the below statements in your cmd or console.
abc.exe > output.txt
This will create a file "output.txt" with the output, in the same folder where the exe file exists.

How to run a file in a specified program through c++

I am trying to use the ShellExecute command for launching a program and open a file through a c++ code. I have included windows.h. The code I am writing is as follows:
ShellExecute(GetDesktopWindow(), "open", "C:\\Program Files (x86)\\EMSO\\bin\\emso.exe","C:\\Program Files (x86)\\EMSO\\bin\\MultiCSTR_Reaction.mso", NULL, SW_SHOWNORMAL);
The code opens the executable file but is not able to open the .mso file. It generates dialog boxes saying- 'File 'C:\Program' not found', 'File 'Files' not found' and 'File '(x86)\EMSO\bin\MultiCSTR_Reaction.mso' not found.' I can open the file through the command line. But how to open through a c++ code.
Try the path to a file (4th argument to ShellExecute) as follows:
"\"C:\\Program Files (x86)\\EMSO\\bin\\MultiCSTR_Reaction.mso\""
Programs can take multiple command line arguments separated by spaces. So, if you try to pass a string that contain spaces as an argument, you'll find it split into as many arguments as many spaces there are. To avoid this, the string needs to be surrounded by quotes.

How do I open a file in c++ (other than notepad)

I was wondering how to open a file other than notepad... Our prof gave us an example:
s = "notepad.exe test.txt";
system(s.c_str());
That will open a file type of "notepad.exe" and the file name of "test.txt"
Main Question:
Now, I was wondering if there was a way to open other type of files, such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
My attempt opened something in a new cmd.exe (because of the START keyword):
fileNeededtoBeOpened = "START \"New Microsoft Office Excel Worksheet.xlsx\"";
system(fileNeededtoBeOpened.c_str());
(This code is slightly different from my original, where I'm trying to open a file from a vector...) but all I really need to know is instead of "notepad.exe" or "START" is there a different command to open different file types that aren't .txt
Also, a side note, I was reading on the internet that it wasn't safe to use system() to open files, is this correct?
I found the answer by myself... for those who are curious, here an the answers:
To open a text file: system(notepad)
To open an excel file: system(start excel)
To open a word doc file: system(start winword)
To open a 7z file: system(start 7zFM)
To open a visual studio file: system(start devenv)
I think you're confused.
System executes a command as you would on the command line (type cmd into the run prompt under start menu to get that).
So, when you type notepad.exe test.txt it's saying:
Open the program notepad.exe which is on the system path (so the
command line can find it to execute that program), and pass the
parameter test.txt to it.
Notepad itself decides what to do with test.txt, in this case it opens it.
So, you can tell it to run any command (program/executable) and pass any parameters to it in reality. If excel's on your system path, you can probably just type excel.exe to open it from your system command. Otherwise, find the location excel is installed in, and refer to it with the whole path to excel.exe and it will work fine.
For example, on my computer, executing "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" would open excel from the command line. I can pass further parameters to it by having more information (like filenames) after the Excel.exe" portion, just as you did in your notepad example. Using your system command should have equivilent behavior when that line is executed.
If you are only targeting Windows systems you can use the ShellExecuteEx function (part of the Win32 API). You can just pass a filename to it and it will launch what ever program is registered to handle that file type (just as if you opened the file from windows explorer). Documentation is available on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx
There is some examples on Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) MSDN article and lots more elsewhere around the internet.
AS the other guys mentioned , the System function only executes a cmd command, .. notepad.exe is in the system's path by default so it works directly
but for example for me if I want to open a zip file on my desktop , I'd type something like
"C:\Program Files\7-Zip\7zFM.exe" Desktop\zipfile.zip
that's when I'm currently at the my user's directory [by default] , or
"C:\Program Files\7-Zip\7zFM.exe" C:\Users\JiMMaR\Desktop\zipfile.zip
[where JiMMaR is my user name on windows 7]
note that this certain command works only on windows , if you are using another OS this won't work as it is
try doing a
fileNeededtoBeOpened = "\"C:\Program Files\7-Zip\7zFM.exe\" C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
and see if that executes or not
edit:
if you cannot escape the space , then try this one
fileNeededtoBeOpened = "C:\Program~1\7-Zip\7zFM.exe C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
Ok, firstly - system - is a function that starts a separate process to your program. Much the same as in a command window when you type the command. The command lines you provide will be dependent on the applications you want to launch.
Now, I was wondering if there was a way to open other type of files,
such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
Yes I would be pretty shocked if there wasn't a command line parameter you could specify to load a document in these apps at start up. (Ok not shocked, but it is pretty standard)
Does this have anything to do with c++ - not really - you need to look at references for the applications you mention and see what the command lines parameters are for them. Then craft a string and system(...) to your hearts content.

Open file with another application

Hy!
I want to open an .xml file from c++ with a specified application (knowing the path of the file and the path of the application). Same way as you would right-click the file and click "Open with..." and choose the application to start with.
Thanks in advance!
Usually applications allow command line arguments for file opening. For example, to open a file with notepad you can use command line :
notepad.exe a.txt
That's why you can use this feature with CreateProcess in Windows (which allows specifying arguments) or analogous function in other OS . HTH
how about simply:
system("application.exe file.xml");