I have a bunch of batch files which I want to run sequentially. One of them runs an MSI which adds a folder to the PATH. How can I make sure that subsequent batch files will notice this change, without restarting CMD? I'm using Windows Server 2008 R2 x64.
I've tried call, cmd /c and start "", in the hope that starting a new process will work, but it doesn't.
in run-both-scripts.bat
call script1.bat <-- This runs an MSI which modifies the PATH
call script2.bat <-- This relies on the PATH changes which were made by the MSI in script1.bat
To clarify: this is fairly straightforward to reproduce.
Start CMD
Create an environment variable manually, not using setx, to mimic what the MSI does.
Right click on Computer -> Properties -> Advanced System Settings -> Environment variables -> New
Create an environment variable called, say, hello with the value hi there.
In your CMD window, type echo %hello%. You'll get %hello%.
Try cmd /c "echo %hello%. You'll get %hello%.
Try start "" to open a new CMD process; type echo %hello%. You'll get %hello%.
Try start "" echo %hello% to run the command in a new CMD process. You'll get %hello%.
Finally, try manually opening a new CMD window from the Start menu and type echo %hello% from there. You'll see hi there.
So you can see that the only way I've been able to make CMD see the change to the environment variable is by restarting CMD.
Ok, did some research, and found out why the soloutions we've been throwing at you don't work. When you start cmd.exe as an application, it looks at the current environment variables and copies it to memory. When you start cmd in a batch file, it will not look at the environment variables, instead it will look at the variables set in the current batch file, and utilise those. Thats the problem when your storing data on memory. The only way this is possible is if you copy the current environment variables into a text file as memory on a hard disk. Now, the question is how to go about doing this.
After heavy research, the only thing I could find that related to the topic was the use of start /i, however, when I tested this it didn't work. (start /? for more info).
In other words, other the setx, I don't think this is possible with batch.
Mona
Related
I am programming a DOS-like shell, but it starts in %UserProfile% instead of %UserProfile%\Desktop\Coding projects\CM-DOS.
The actual shell, kernel.bat's code is:
#echo off
type INFO.txt
pause
INFO.txt is in the same directory as kernel.bat (CM-DOS).
It says:
CM-DOS is a FREE, open-source Disk Operating System by Tristan
Carpenter. If anyone trys to sell you a paid copy of CM-DOS, send me
an e-mail at tristan4#mail.com. Please use this DOS shell kindly. This
shell is licensed under the MIT License. Do whatever you want with the
shell, create closed-source versions, fork it, I don't really care.
The script tries to type "%UserProfile%\INFO.txt", which doesn't exist.
How can I start the file in it's actual directory, instead of %UserProfile%.
If you run the script (clicking or navigating (cd <folder>) and .\kernel.bat) and the current folder isn't, well, the current folder, then something is off with either your console setup or there is an additional code executed in the background. Try to echo %CD% to see what the folder is and check the prompt string usually in the shape of:
C:\Users\Username>
which is the default path if you run cmd.exe as a user or:
C:\Windows\System32>
if you run as an Administrator / user with elevated privileges.
This can be changed though with regedit.exe.
However what you might be seeing is that you run your code (kernel.bat or however you name it) which might display the first path before execution or after the last command you might be dropped into a shell. Neither should be the case according to the code, yet it can happen if you started cmd.exe, then wrote cmd.exe (i.e. spawned another shell) and then ran your script.
What I suspect is that you're running the code like this:
C:\Users\You> .\Desktop\Coding projects\CM-DOS\kernel.bat
in which case the %CD% would be C:\Users\You and type command would be looking for C:\Users\You\INFO.txt. A better solution is simply creating a variable for the folder where your script is located - which is provided automatically, it's %~dp0 (echo %~dp0) and with that you can do:
echo off
type "%~dp0INFO.txt"
pause
to make it location independent, or better said, to access relatively according to the kernel.bat (the current script)'s location.
You can add cd %CD% and %CD% stands for the current direction. the final code would be
#echo off
cd %CD%
type INFO.txt
pause
The following command works just great as the only line in a batch file:
start "Google Sync" /belownormal "C:\Program Files\Google\Drive\googledrivesync.exe" /autostart
When I drop that line into msconfig startup tab, however, nothing happens.
So I thought maybe I needed to put in the full path to start.exe. Assuming it was an exe, that is.
So I tried both DIR and WHERE to locate start.* but nothing came up.
So, two 2uestions.
Is start a separate START.EXE executable in W7Pro and if so where is it?
How can I get this line to work inside msconfig?
Before you ask, my underlying purpose is to start Google Backup and Sync in the same way it usually does, but with /BELOWNORMAL priority. I already tried adding /BELOWNORMAL to the line Google was originally using in msconfig, without success. But START does what I want from a batch file and so I assume it would, or should, work via msconfig.
Thanks.
If you want to use commands implemented by Cmd.exe you must invoke Cmd.exe:
cmd /c start "Google Sync" /belownormal "C:\Program Files\Google\Drive\googledrivesync.exe" /autostart
I am a new programmer, i have created a batch file to open a website but i want that batch file to run when the computer starts.
Here's the code:
#echo off
:top
start iexplore.exe http://www.website.com
timeout 3
goto top
While i do think Arescet's answer will work, i am more in favor of using Windows' Task Scheduler.
Simply create a new task :
Assign it's trigger to be At Startup :
and add a new action to Start a program giving it the path to your batch file:
I believe this is a cleaner approach which also provides you with logging and history should you decide to do more things in your batch file later on
Type run into windows search, type shell:startup in the prompt, press enter, Then, create a shortcut of your program, and move the shortcut into the startUp folder.
On note of your code, the start command should have a blank title as "", and quotes around the application name and parameters passed, like:
start "" "iexplore.exe" "http://www.website.com"
I'm using cl in cmd and having to run vcvars32.bat every time I open a cmd window is really a pain in the axx. Can anyone offer a way of running it automatically?
From cmd /?:
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
You therefore could add vcvars32.bat to one of those AutoRun registry values to have it executed for every cmd.exe instance (except when /D is explicitly specified, of course).
However, be forewarned that doing this could result in other weird side-effects (for example, it could cause other .bat/.cmd scripts to be run in an environment that they aren't expecting).
A workaround that works for some people is to write a batch file and call it A.BAT and make a.bat launch vcvars32.bat. Put a.bat on the path and then it's a matter of opening the cmd prompt and typing a and enter and voila, you're set!
way old, but the easiest way to do this with, say, a shortcut created on your TaskBar is to modify your shortcut (in %appdata%\microsoft\internet explorer\quick launch\user pinned\taskbar, or thereabouts) so the target is:
%windir%\system32\cmd.exe /k vcvars32.cmd
that'll do exactly what you're looking for. The /k tells it to execute the string but keep the window open (string being your batch file). You can either put vcvars32 somewhere in your path, or specify the whole path to vcvars32.
You can use the script in http://www.alteridem.net/2010/09/02/visual-studio-2010-command-prompt-here to make it so when you right-click a folder in explorer the option shows up. After downloading and extracting the zip file you can modify the .inf to point to the correct path to your particular VS version (and change the displayed name if desired). Note the comment on the page about having to rename the file if you are running 64-bit Windows.
I have a batch file, this batch file will not start automatically, it will only run when i double click on it.
Can I run the batch file in the background when I double click on it.
Well, you can start it minimized with start, if that is enough. Really hiding it is difficult (although I can think of an option right now).
Basically you need to determine whether the batch has been started by double-clicking it. You can do this by defining a special variable and look for it:
#echo off
if not defined FOO (
set FOO=1
start /min "" %~0
exit /b
)
rem here whatever you wanted to do originally in the batch
As long as the FOO variable isn't defined (which is probably the default almost everywhere), this batch will launch itself minimized again, but with the variable defined first. Environments are passed to subprocesses, which is why this works.
you would generally need something else to run the script in that manor
i.e.
Create a shortcut, and set the “Run” field for the shortcut to “Minimized’.
Once you click or tab away from the cmd.exe window that the batch file is running it, it's "in the background" -- I'm not really sure what you want but it sounds like you might be asking how to run the batch file without displaying the cmd.exe window.
If so I can think of two ways: first, you can create a shortcut to the batch file, right click it, and in the properties there set the shortcut to run minimized (should be a drop down option next to Run).
You can also wrap invocation of the batch file in a VBScript file using Windows Script Host's shell object (calling the Run method) to run the batch file invisibly. Passing 0 as the intWindowStyle parameter will suppress display of a window or anything.
#Ghyath Serhal
I have used cmdow to do this on another program, it is an external application that can be used to modify the command prompt. To use it, you will need to either enter this code (see below) into it's own batch file, or into the command prompt, where it will run 'BatchFile.bat' with a hidden terminal window. I haven't found a way to use this in a single batch file, but I only found out about this today.
cmdow /run /hid 'BatchFile.bat'
Hope this helps.