Using tree Command in C++ on C:// - 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).

Related

how to use batch in 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."

How to have a C++ program run a cmd command [duplicate]

I'm trying to use the system() function in a C program.
For example, I tried to create a directory on my desktop, using the system() function.My code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("cd c:\\Users\\USER\\Desktop");
system("mkdir test");
return 0;
}
When I run this code, a directory is created, but not on my desktop. It is created in my project directory.
Why is this happens?
Can I use the cd command in the system() function? If not, is there an replacement to the cd command that will work with system()?
I'm using Windows OS. I'm trying to use system() from a C program as I use cmd program.
I know that I can create the directory using WinAPI without any problem. I don't want to use WinAPI, my question is how can I make it work using system().
The changed directory only lasts for the duration of the system command. The command starts a separate program, which inherits its current directory from your program, but when that program exits its current directory dies with it.
You can use && to join the commands together, and it will work:
system("cd /D C:\\Users\\USER\\Desktop && mkdir test");
I also added the /D switch, or the CD command would not change drive letter if it were called from a different drive.
However, mkdir is perfectly capable of accepting a full path, so you could simply do:
system("mkdir C:\\Users\\USER\\Desktop\\test");
When you say system("some shell command");, the program spawns a shell to run the command. The shell has its own idea of the current directory, separate from your program's. The shell cds to the directory just as you asked it to, and then dies, leaving your process's CWD unaffected.
You could simply say _chdir("c:\\Users\\User\\Desktop"); to set the current directory before running the "mkdir" command. The shell that spawns to run it will then inherit your program's current directory and make the folder in the right place.
(For that matter, you could say _mkdir("test") as well, and stop using system unnecessarily. You should only reach for system when you're trying to do something that's worth running an external program / shell for.)
You have to perform both the commands on a single line like this,
system("cd c:\\Users\\USER\\Desktop && mkdir test");

How do I create/access ~/.lein/profiles.clj?

I am very new to Clojure and am following Clojure for the Brave and True. One of the steps is to create ~/.lein/profiles.clj . I cannot find how I am supposed to do this so any help would be much appreciated.
Thanks in advance
From your question, I take it that you are a) on a Linux system and b) do not yet know your way around Linux. Is that correct?
If so, there are a few things, you should know:
Filenames beginning with a dot are hidden. You can not see them in normal file listings. All graphical filemanagers have a switch somewhere to show hidden files. If you are typing in a terminal, you can use the -a option of the command ls to show them. Compare the output of ls ~ and ls -a ~on the command line. You can usually get a command line if you start a "terminal" or "console" from menu.
You can create directories on the command line with mkdir. In this case you would call it like this: mkdir ~/.lein on the command line.
You can then use one of the many, many text editors to create and edit the profiles.clj file. For example, on the command line call gedit ~/.lein/profiles.clj to open a graphical editor. It should be installed on most systems. If you do not have a graphical user-interface, you could try the editor nano instead of gedit
If you are on a Windows box, all these instructions make no sense. In that case, I cannot help you much as I have never run Clojure on Windows.
If you are already an experienced Linux user and I just misread your question, I beg your pardon for stating the obvious.

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.

With system() in C++ on Windows, why are two quotes required to invoke a program in another directory?

I have the find.exe program in my utils folder. This does not work:
system("\"utils/find.exe\"");
All I get is
'utils' is not recognized as an internal or external command,
operable program or batch file.
However, for some reason this works:
system("\"\"utils/find.exe\"\"");
Echoing the single quoted string
system("echo \"utils/find.exe\"");
outputs
"utils/find.exe"
... so why do I need two quotes?
I assume you're on windows because you're trying to execute an .exe file. So, instead of writting "utils/find.exe", try to write "utils\find.exe". The delimiting character on windows is '\', so it probably sees "utils" as a command since '/' is ignored.
Perhaps system() is passing your command line to the shell, e.g. cmd.exe, which also needs quoting?
Even though you can use both / and \ as directory separators in Windows, the command processor will try to interpret anything starting with a / as a switch. Try this:
system("\"utils\\find.exe\"");