Batch file variable in an environment variable - windows

I have a program named go.exe, and I pass it a file name example so that it can make a log file named example_golog.txt.
The problem here is that I want to only call the executable without explicitly giving a filename or making a user have to give a file name.
My solution to this was to name a system variable called GO whose value is go.exe %~n0. What's wrong with this is instead of the %~n0 part getting the file name of the batch file that called it, my go.exe file just makes a text file called %~n0_golog.txt.
Is there any way around this so that %~n0 will do it's magic in the batch files that call it in go.exe %~n0?
EDIT:
My system variable:
Name: GO
Value: go %~n0
My test.bat file:
#echo on
%GO%
pause
When I run test.bat, the %~n0 does not expand out.
So instead of test_golog.txt being made, %~n0_golog.txt is made

In your batch file, you can call it to dereference the variable.
echo %go% will report go %~n0.
call echo %go% will report go test (from a batch file named test).
You can use whatever other sets you need from there.

Related

Set environment variable in nested batch file

I am trying to factor out a piece of a batch file that sets an environment variable to another batch file. This piece is somewhat involved and I would rather not repeat it in multiple batch files. I won't over-complicate this post with the actual code I'm trying to run in the nested batch file, but will provide a simple example that shows what I'm trying to do and reproduces the problem.
Batch1.cmd
cmd.exe /c setvar.cmd abc def
set abc
setvar.cmd
set var=%1
set val=%2
set %var%=%val%
The error returned for "set abc" in Batch1.cmd is this:
Environment variable abc not defined
I imagine cmd.exe starts up a new environment, because on return to Batch1.cmd, the variable does not exist.
Is there a way I can nest a batch file and keep the environment it creates?
The environment block is not shared between processes. As you are starting a new cmd instance a separate environment block is created, changed and destroyed before returning the control to the current batch file, that will not see any change as it was done in a different process.
Use call setvar.cmd abc def to start the nested batch file in the current process.
Use the setx <var> <val> command to set an environment variable more persistently. For example:
setx WEBCAM_ADDR 192.168.0.101
(Note: There is NO equals sign (=) as with the regular set command)
I just went through this issue, trying to run batch files from windows. This is better than having to write, say, a configuration file somewhere...

Get the filename of a running batch file

I am trying to make a batch file that will write the name we names it.
For example if i names the file "test1" and start it it will change the title only to "test1" If i change the name to "bat file" it will display "bat file" in the title.
Is it possible to make a file that will write to a .txt file with the same name as the bat file? I am trying to make a batch file that will log sometings and write it too ".txt" where is the name of the bat file without extension
In batch files %0 stores a reference to the current batch file. The exact format of the value depends on how the file is invoked, but there are a serie of modifiers that can be used to retrieve the needed information (You can see the documentation or execute for /?, as the help for this command also includes the full list of modifiers)
#echo off
title %~n0
echo I am %~n0
> "%~dpn0.txt" echo This goes to the file
Where
%~n0 = name of the current batch file (without extension)
%~dpn0 = drive, path and file name (without extension) of the current batch file
echo %~f0 for writing your batch file name. Use ">" to write anything to a file. For eg use echo "write this" > file.txt

How to reset path vars after each batch call?

I'm performing a mass build (1000+) projects. I call a batch file that calls another batch file that contains a long list of calls to project builds. The problems is, after a few projects, the process stops and says "The input line is too long". I did some research and found that the path environment vars are probably changing and therefore becoming too long. How can I reset the path variable between each of the build calls? Or is there another way I can solve this problem?
A simple solution is that in the batch file which modifies PATH or in one of the parent batch files you insert the line
set InitialPath=%PATH%
And later when the project is built another a line is inserted
PATH=%InitialPath%
A small batch file demonstrating this simple solution:
#echo off
rem Remember initial value of environment variable PATH.
set InitialPath=%PATH%
rem Environment variable PATH is modified to include a compiler directory.
PATH=C:\Program Files (x86)\MyCompiler\bin;%PATH%
rem Do here whatever must be done with modified PATH.
echo %PATH%
rem Restore the initial value of environment variable PATH.
PATH=%InitialPath%
echo %PATH%
rem Optionally the environment variable InitialPath is removed finally.
set InitialPath=

Windows batch file, save contents of file dowloaded by wget to a variable

Currently, I have a batch file that uses wget to read a file from the server. Is there any way for wget to save the contents of that file to a variable and then for the batch file to take a certain action based on the value of the variable?
The peseduo-code would probably look something like this. I am very new to batch files and am still learning the semantics:
SAVE RESULT OF wget http://www.theserver.com/instruction TO VARIABLE: the_variable
IF %the_variable% == 'restart' <DO SOME ACTION HERE>
I will base this answer on the assumption that your downloaded file contains text strings.
If this is the case then it is possibile to use the FOR command in this way:
for /F %I IN (instruction.txt) DO if %I==restart #echo RESTART FOUND
This command opens the file "instruction.txt" and parse it assigning each word to the variable %I
Then for each value of variable %I executes the command specified after the keyword DO.
In this case I have compared the variabile %I to the string "restart" and if the result is true the batch execs the command #echo RESTART FOUND
You could use the GOTO: function in your batch file. So that if the variable is equal to a certain value/string it jumps to a particular section in the batch file and carries out the code in that section.
Almost like using methids in Object Orientated Programming.
CHeck this link out :-
http://www.robvanderwoude.com/goto.php

can't call .bat file from within another - "not recognized as an internal or external command" error

i tried looking at the other questions regarding this, but no go. i've tried a straight call to the other bat file ("otherBat.bat," for instance), a "call" command, and even a "start" command. all of these are failing though, and i'm at a loss as to why. both .bat files are within the same folder, and i'm not changing directories, so i don't know what the problem is...
any help on this would be much appreciated ^_^
edit: sorry, here's the code :)
primary.bat:
echo Test run...enter variable1
set /p var1=:
echo Test run...enter variable2
set /p var2=:
call other.bat %var1% %var2%
pause
other.bat:
echo Working!
pause
You should either cd to the current directory in your first batch file or call the second batch file by full path.
Is the second .bat file in your path? What happens if you change your first .bat file to call it using an absolute path?
use the absolute path :
::prototype
CALL [drive:][path]filename [parameters]
::example
call C:\Users\theUserName\path-to-your-file\the-file-name.bat %your-variables-to-pass%
cf the call documentation from ss64.com
Absolute path may be replaced with %~dp0\, which means use current script's path instead of runtime context; in your case call %~dp0\other.bat %var1% %var2%.
Had same issue when running as administrator from context menu – call tried to execute batch file in System32 instead of parent BAT folder.

Resources