how to use batch in c++ - c++

I have a batch program that I would like to add to one of my c++ programs. The batch program tests if a file exists and exits the script if it doesn't. I do not want to add any more libraries to my code. The problem that I have is that I am not sure how I am able to use batch in c++. I am able to figure everything else out on my own I just need to know this one thing.

You can use the system(" ") command to use batch.

Assuming you are on Windows,
you have two options available to run batch files on Windows from C/C++.
You can use system (or _wsystem for wide characters).
"The system function passes command to the command interpreter,
which executes the string as an operating-system command. system
refers to the COMSPEC and PATH environment variables that locate the
command-interpreter file (the file named CMD.EXE in Windows 2000 and
later)."
Or
You can use CreateProcess directly.
Note that for batch files:
"To run a batch file, you must start the command interpreter; set
lpApplicationName to cmd.exe and set lpCommandLine to the following
arguments: /c plus the name of the batch file."

Related

(VS2010 C++) Execute a command every time the program is run?

The IDE I'm using is VS2010 for writing C++
I want to execute the command cmd C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt every time the program I'm coding is run from within the IDE. (This command should open a console to track changes made to the file Log.txt)
There are ways to make a command run every time the program is built, but I can't find a way to make a command run whenever the program itself is run, even if it's already built. Where or how might I be able to set that kind of thing up?
I've tried putting $(TargetPath) & C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt into the project's Properties->Debugging->Command (TargetPath is the full name of the debug executable) but VS reads the entire thing as a filename and gets confused.
You can create a file run.cmd for example next to the vcxproj file, which would contain:
%1
C:\utilities\unix\tail.exe -f -n15 %2Log.txt
And then in Properties->Debugging->Command you write:
$(ProjectDir)\run.cmd
and in Command Arguments you write:
"$(TargetPath)" "$(ProjectDir)"
I may have misspelled the macros, but you get the idea: it executes first your program and then whatever you want.
Edit: Unfortunately it works only if you start without debugging (Ctrl+F5), because otherwise the debugger tries to attach to run.cmd and complains that the format is unsupported.

Compiled exe won't run through my custom schemed cmd?

I have a custom color/font/size/etc. scheme for my cmd.
When I run my program through VS (with or without debugging) it runs my program in cmd using my scheme. (good)
But, when I run the exe file directly, it runs in the default cmd (without my scheme). (bad)
If I make a .bat file which executes my exe, then it runs in the schemed cmd. (good)
But if I make a shortcut of that .bat file, it runs in the default cmd! (bad)
How can I make it so that the exe itself runs through the schemed cmd?
If not possible, how can I make it so the shortcut to the exe/bat runs it through the schemed cmd?
If you want the software to run in schemed cmd, you should execute the program over command line, other way you can edit your program's font etc. in your program.
Compilers run programs by cmd.
And if you want it so much, you can check your program on task manager at beginning your code and if there isn't, you can run with system function and hide first one.
I don't know how you changed your cmd scheme, so, it is possible that this will not help you. Anyway...
The standard way of cmd customization is to include in HKCU\Console\ a subkey with the title of the console that will be customized, and inside this key, the values that define the console properties (more here)
So, let's say you have now a console scheme in the registry. When you start a console program via a shortcut, the window title of the started console is the text indicated as the shortcut name. Match shortcut name to console name defined in registry (or the reverse).

Using tree Command in C++ on C://

I want to use the Windows CMD tree command in my C++ console application. My code:
system("cd c:/");
system("tree");
The problem is that the command tree will execute on the folder path where the program is running and not on C://. Is there a way to fix this?
Why not :
system("tree c:\");
?
TREE [drive:][path] [/F] [/A]
/F Display the names of the files in each folder.
/A Use ASCII instead of extended characters.
You can use SetCurrentDirectory from windows.h. This page has a demonstration:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806%28v=vs.85%29.aspx
Your problem is that the system("cd c:/") is executed in a shell, and then the shell exits. [It's also wrong, because you use the wrong kind of slash, it should be "cd c:\\" - the double backslash is needed to make one backslash in the output, assuming we're talking about a Windows system].
There are a couple of different ways to do this:
Use chdir() (or SetCurrentDirectory) function call to change the main processes current working directory, and then call system("..."). This is the simplest solution.
Generate all your commands into a batch file, then pass the batch file to system.
Open a command shell with something like _popen() and pass commands into the pipe that you get from that.
Manually create pipes and connect them to the standard in and standard out of a process that runs the command prompt.
Just for programs in Windows, include "windows.h", then
SetCurrentDirectory("c:/");
system("pwd");
While I'm still curious why would you want to do this, you can try to run all commands in one system() call:
system("cd c: && c: && tree");
Second c: is needed to change drive letter, in case if you're not currently on drive c: (because cd doesn't do it).

Running Shell Script from CPP , while Script bundled inside the exe

I wanna create a exe which has the Shell Script and Simple CPP file which calls the Shell Script using system() function. Lets say exe name 'myInstaller' which has files myintsaller.cpp and myShell.sh. When i run the exe myInstaller , it must execute shell script. I want to do like this so i can protect my Shell Script code ,which has over 3000 lines of Code.
How do i do this ... I m in real need of this.
As far as i've searched, there is no way to bundle the script inside the exe. Either it would be Shell Compiler as shelter suggested or plain shell scripts. No work around for this.
I guess it's Windows platform since you speak about exe. In this case you can add your script as a resource to your exe, extract it in run-time and execute.
You could keep the script as a string in your C++ code and call system("sh -c '" + code + "'"). Quoting may be an issue though. Otherwise, write it to a temp location, execute it and then unlink it.

How to move to a folder and execute a Windows command line call

This is simple to do in languages like Python, but I'm not sure how to do it in C++. I want to move to a specific folder, say "C:\tests" and run some command line call from that folder. Thanks
You can start a process in a specific directory using the CreateProcess() call. In particular, look at the lpCurrentDirectory argument:
lpCurrentDirectory [in, optional]
The full path to the current directory for the process. The string can also specify
a UNC path. If this parameter is NULL, the new process will have the same current drive
and directory as the calling process. (This feature is provided primarily for shells that
need to start an application and specify its initial drive and working directory.)
This function is used internally by Python's subprocess.Popen's constructor.
You want SetCurrentDirectory for changing directories and system for executing a command asynchronously. system is the simple way to do it. You can use CreateProcess if you need flexibility.
Create a bat file and put cd command there to set required path and then write your command to execute. Ex in your bat file write
line1 "cd c:/tests/"
line2 "your command to be executed"
After that you could use system("*.bat") to call your bat file. See more info related to that here http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044654269&id=1043284392 Hope this helps