About the system in vc - c++

I run below code in vc.
system("#echo off");
system("set a=3");
system("echo %a%");
system("pause");
but it display as '%a%', but I require display as '3'.
How Can I make it?
Thanks very much for your help.

Create the "tmp.bat" file:
#echo off
set a=3
echo %a%
pause
and then call the
system("cmd /c tmp.bat")
The thing is that system() call creates a "clean" environment where the "a" variable has not been set (in the "echo %a%" call).
To convert the sequence of commands into something "executable" one might use some special tool, not the "VC".
Look for http://www.autoitscript.com/site/autoit/ and similar tools.

For a start, every single one of those commands runs independently of the other. By the time you try to echo %a%, the set for it (from the previous call to system) has been long forgotten. So, it's effectively the same as if you had opened up a brand new cmd window and typed:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Pax> echo %a%
%a%
C:\Documents and Settings\Pax> _
You will probably need to place all those instructions into a single cmd file then run:
system ("cmd /c c:\\full\\path\\to\my\\script.cmd");

Related

Visual Studio cross platform makefile project, command not found

I'm trying to build a cross platform project for Ubuntu. In my makefile I have the line
"PSPSDK=$(shell psp-config --pspsdk-path)"
which gives the error "psp-config: Command not found."
psp-config is in my path and running make from the Ubuntu system on the files that get copied over from Visual Studio works fine. It also works if I manually ssh into the Ubuntu system from windows and run the command from there.
Why can't it find the command when run through Visual Studio?
You should update PATH at the beginning of "~/.bashrc" file (and not at the end) because it starts with somethign like:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Also, if you add code before these lines, it will be invoked for every subshell execution, so it is better to add a guard for it as well so it is invoked only once per session:
if [ -z $HOME_OPT_PATH_SET ]; then
export PATH=$PATH:$HOME/opt
export HOME_OPT_PATH_SET=1
fi
# If not running interactively, don't do anything
...

How to close the drive

I have a code which will un-hide all files present in the removable drive
//buffer is the removable drive letter
PWSTR show = L"/k attrib -s -r -h *.* /s /d /l ";
ShellExecute(NULL,L"open",L"cmd.exe",show,buffer,SW_HIDE);
This code works successfully but after performing this operation I cannot eject my removable drive.
It shows the following message
Even when I close my program it shows this message. When seeing the task manager I found that my drive I:\ is running in the background what should I do now to solve this
The switch /k makes cmd.exe run a command then sit there waiting for further instructions.
Your shell window is still there, in the background, hidden, sitting on I:.
Replace /k with /c.
/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
(http://ss64.com/nt/cmd.html)

Console prompt window appear on system("start dir") but not on system("start ipconfig")

I try to create a simple UI which runs a command prompt in the background (but the windows console must not disappear) while clicking on each button, resp.
But before, I try something like system("start dir"); to see if the button works.
Here is the problem: when I click on the left button the windows console appear and don't exit unit I close it. But this only work with system("start dir");. If I change dir to ipconfig (or another call-function) the windows console will appear for a second and the exit. I tried something like system("PAUSE"); or getch(); etc, but it doesn't work.
Why does this command work with dir but not with another command?
There is a fundamental difference between DIR and IPCONFIG, the DIR command is built into the command processor (aka shell), IPCONFIG is a separate program stored in c:\windows\system32.
When you type START /? at the command line then you can see why it treats them differently:
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
The alternative is to ask the command processor to execute the command and exit afterwards. You do with the /c option:
system("cmd.exe /c dir");
Or simpler yet, since system() automatically passes off the job to the command processor:
system("dir");
Just stop using start :)

batch file to check existance of directory if not exists then use alternate directory for file copy

I am trying to write a batch file that copies an exe file from a network location to a local location. It currently works but depending on windows version (xp or win7) the user has to select the correct .bat file due to different local paths needed for the copy. (they are going to the startup folder to be ran every time user starts machine). This is the first time i've ever worked with writing batch files and am completely lost when looking at the syntax for if statements. If i could get some help figuring this out it would be great.
Here is what I currently have that works for XP:
REM #ECHO OFF
ECHO STARTING MOVEFILES
SET EXITRC=0
SET EXITMSG=EXITRC INITIALIZED
ECHO %EXITRC% -- %EXITMS
COPY "\\networkDrive\install\Individual\program\MOVEFILES.EXE" "C:\DOCUMENTS AND SETTINGS\ALL USERS\START MENU\PROGRAMS\STARTUP\"
ECHO COPIED FILES TO YOUR PC
SET EXITRC=%ERRORLEVEL%
IF NOT %EXITRC% == 0 GOTO :EXIT
SET EXITMSG=PROCESSING COMPLETE
:EXIT
ECHO STEP: %EXITMSG% RC: %EXITRC%
ECHO FINISHING MOVEFILES
PAUSE
EXIT %EXITRC%
Here is what I have for Windows 7:
#ECHO OFF
ECHO STARTING MOVEFILESWIN7
SET EXITRC=0
SET EXITMSG=EXITRC INITIALIZED
ECHO %EXITRC% -- %EXITMS
COPY "\\networkDrive\install\Individual\program\MOVEFILES.exe" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
ECHO COPIED MOVEFILESWIN7 TO YOUR PC - All Users / Public Startup folder
SET EXITRC=%ERRORLEVEL%
IF NOT %EXITRC% == 0 GOTO :EXIT
SET EXITMSG=PROCESSING COMPLETE
:EXIT
ECHO STEP: %EXITMSG% RC: %EXITRC%
ECHO FINISHING MOVEFILESWIN7
PAUSE
EXIT %EXITRC%
I would like to have only one batch file that will cover both scenarios so there is no confusion to the user on which batch file to run.
You can utilise the environment variable %ALLUSERSPROFILE%.
On WinXP the default is C:\Documents and Settings\All Users
On Win7/2008 the default is C:\ProgramData
There is a table available here: http://ss64.com/nt/syntax-variables.html
I see you also copy a different file. Not sure why you do that. Maybe you could detect using a method here: https://stackoverflow.com/a/2788764/1553090 -- Otherwise perhaps you should take advantage of the %ProgramFiles% and %ProgramFiles(x86)% variables.
Just to elaborate on how you might choose to use these variables... You can test the Win7 startup folder for existence, and if it's not there just fallback to the XP (regardless of whether it exists).
set STARTUP_WIN7=%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Startup
set STARTUP_WINXP=%ALLUSERSPROFILE%\Start Menu\Programs\Startup
if EXIST "%STARTUP_WIN7%" (
set STARTUP=%STARTUP_WIN7%
) else (
set STARTUP=%STARTUP_WINXP%
)

Execute shell command in c++

I have a question regarding executing shell commands in c++. I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. I can decode files by first going to the right directory (cd Test_Copy2) and then execute a command in the command prompt (java -jar tool.jar -b x.fit x.csv). I tried a lot of different stuff but unfortunately got none to work!
I tried using:
system, _popen, ShellExecute(NULL, L"open", L"C:\\WINDOWS\\system32\\cmd.exe ", L"java -jar Tool.jar -b x.fit x.csv", L"C:\\Test_Copy2", SW_SHOWNORMAL)
Can anyone please provide me with an example on how to do that? I dont know where I'm going wrong, most of the time the command prompt opens but no command is executed!
If you really want to run the jar in a cmd.exe instance, you need to add one of the correct command line switches to cmd.exe for it to work the way you want it to:
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
For instance, your command string should be:
C:\\WINDOWS\\system32\\cmd.exe /c java -jar Tool.jar -b x.fit x.csv
You can use the system() function to execute shell commands.
For example:
system("DIR") executes the DIR command in the CMD shell. The default directory at the start is the directory you're .exe file is located.
'system("PAUSE")` executes the PAUSE command.
The command/s you wannt to execute should be passed as a constant string to the function.
Edit:
For you paritcular program the syntax (IMO) would be:
system("java -jar Tool.jar -b x.fit x.csv")