Batch stops after setting variables - file

I'm new to batch scripting and just tried to do some simple stuff and trigger some other applications. At first my batch set some variables and then it should save the results from a command to a file. Both parts alone work fine, but if I put them together it stops after setting the variables. Can someone help me?
Code:
set path=%cd%
set filename=\MAC.txt
set filepath=%path%%filename%
arp -a |find "192.168.1.1">>%filepath%
pause

Since path is the variable used by cmd for finding executables, changing it to something where arp isn't will mean that you won't be able to locate and run arp.
Use a different variable name, such as:
set filepath=%cd%
set filename=\MAC.txt
set fullname=%filepath%%filename%
arp -a |find "192.168.1.1">>%fullname%
pause
Note that this still has another problem present in your original script, the fact that spaces in your current directory may cause issues (unless you quote them correctly).
Since you're using the current directory exclusively, you probably don't need to worry about the path:
set fullname=.\MAC.txt
arp -a |find "192.168.1.1">>%fullname%
pause

Related

Setting system variables using bat file

I wanna write a bat file to set system variables on any windows system. My software which I made needs to set a path instead of asking the user to process the method I heard we can do this task using bat files so I tried multiple ways to set a path nut most of them are duplicates of all existing paths instead of adding new ones and some times it's removing all paths and keeping only the new path
setx Path "%Path%;c:\ffmpeg"
which is duplicating all the existing path variable
I did read multiple Stackoverflow queries on this but none helped
if ur testing on your system to check
please save/make a copy of all ur paths first
Thank you
Eswar
i wouldn't recommend you to mess with system variables but if you want to store path for your program here's a script that will do the thing but before doing so i would encourage you to make make a backup for files and all the paths stored in system variable %Path% and if something happen read this but anyway here is a script that worked for me:
#echo off
echo validating Path
ping localhost -n 2 >nul
set "store_path=Path to your program "
rem /M to set the variable in system wide environment
setx Path "%path%;%store_path%"
setx Path "%path%;%store_path%" /M
simply i am storing the new path with the old one.
I don't suggest you mess with system variables but if you want to store path from cmd use
setx Path "%Path%;C:\your\path\here\" C:\ffmpeg\bin
I tested on windows 10 it worked for me
Hopefully it works for u too!!

Redirect and apply color from text file

I just started coding with Batch a couple weeks ago, a couple hours a day while at work.
I'm writing a script to auto-launch and auto-create dated files. I thought it would be nice to try something "fun".
What I'm trying to do is have my main script save a configuration file with the Batch color code to call upon later/first start and change itself to the previously saved color code. Preferably with a text file.
I've tried using the global variable thing. I think our IT has that disabled as I cannot share variable states between scripts. I haven't tried to enable it. I should also mention we are on Windows 7.
I have since gone the route of creating a colors.bat with the below example code within. I have applied the redirection to my main script to save the file after asking the user for a color code that gets applied to a variable. Right now this Batch file is called upon at a first start in the main script, later I will add a statement to check if the file exists or not.
color 5e
Instead I would prefer a text file that can be called upon or perhaps even a file without a container to prevent backend editing.
However I can't seem to get my script to apply the code inside the text file properly. Below are examples that have failed to apply the color change. Inside the "colors.txt" file is just the Batch compatible color code of "5e" and nothing else.
type colors.txt | color
echo Test
color <colors.txt
echo Test
The above code I have tried with varying placements of the spaces as well.
I've been at this for a few hours now reading up on Batch and Redirection and really anything else of interest. I tried at one point putting the color code as the file name and then trying the Parameter stuff like " %~n1 ". I couldn't figure that out at all.
I hope none of that was confusing. But I'll reiterate, the script as it is right now works. I want to use a .txt container instead of .bat or better yet no container at all. Or even get the global variables enabled, whichever is the best route. I want to call upon this file at script start to apply the color code.
If you have any other questions let me know.
Thanks.
It is fairly straight forward
#echo off
setlocal enabledelayedexpansion
if exist colors.txt (
goto setcolor
) else (
set /p color="Choose color: "
echo !color! > colors.txt
attrib colors.txt +r
endlocal
goto setcolor
)
:setcolor
for /F %%i in (colors.txt) do color %%i
echo Test
pause
The above code check if colors.txt exists, if not it will prompt you for a preferred colour and background colour. Save the code to file and then set it, next time the file will be found and it will just set the colour without re-prompting. We also set attrib +r to make file read-only, if you want to prevent people from editing it, there are ways around it though.
EDIT:
enabledelayedexpansion do cmd /? and you will see
/V:ON
Enable delayed environment variable expansion using ! as the
delimiter. For example, /V:ON would allow !var! to expand the
variable var at execution time. The var syntax expands variables
at input time, which is quite a different thing when inside of a FOR
loop.

Batch File - Attrib +r %Var1%

I have a undoubtedly silly problem. I need to change the attribute of a file to read only. I know to use...
atrrib +r c:\somefile.txt
And it works. However in my program I want to use a variable in place of the path to be built up beforehand. Now if I write...
set File=c:\somefile.txt
attrib +r %File%
Then I get an error saying 'attrib' is not recognized as an internal or external command etc.
However if I echo %File% beforehand then I know the path to the file is correct and being read properly.
What is my error? Thanks a lot!!!
Edit:
set File=Main.xaml
set Folder=C:\Users\yef03111\Desktop\His0164\WINDOW\ALS026-01~EDF
set Path=%Folder%\%File%
echo %Path%
However if I change the echo to attrib +r and nothing else...
attrib +r %Path%
I get the 'attrib' not recognized error. This is the current example that is not working. Hope you can spot something from it!
The problem is that you are setting an environment variable called PATH. This is overwriting the system PATH variable which contains the location of the executable files such as attrib. The way it works is that in order to find a program to run, the OS looks up the PATH variable and searches in the folders listed there for executable files with the name of the program you are trying to run. When you change the PATH variable, the OS can no longer find the attrib command.
Change the name of your variable from path to filepath and it will work.
Path is a system variable that tells CMD, Explorer, and CreateProcess where to look for commands.
As you are trashing the system variable CMD no longer looks in system32 for commands like attrib.
set Path=%Folder%\%File%
As a general rule avoid likely names used by the system in naming your own things. A lot of people will do MyFile or ProgPath.
Also commands like attrib will always be found if the current directory is System32. The current directory is always searched first for commands (programs). I suspect RunAs is setting the current directory to System32.
I have tested your script using Cmd on windows 7. Works a treat, so I cant recreate what you are seeing.
I did quite a lot of batch scripting at one point, and I used to get random errors using standard windows notepad. Tell tail signs of issues in the script were whitespace characters. If you are using notepad, switch to using notepad++ to write this and see if you still get errors.

Kicking off a script and moving on to start another

I'm trying to remotely deploy some software and I have a few batch scripts to help this process along. The basics of my process is to have a text list that scriptA goes through, scriptA copies the necessary files to the target PCs (retrieved from the list) and launches ScriptB. The theory would then be that it would move on to the next PC in the list as soon as ScriptB launches and doesn't wait for ScriptB to finish before moving on. The basics of what I have are below:
ScriptA:
Set /P List= Please enter the list name
Set /P Name= Please enter your username
FOR /F %%A IN (%LIST%) DO (
MKDIR \\%%A\C$\Temp\Install
XCOPY "\\share\necessary files" "\\%%A\C$\Temp\Install"
PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install\scriptB.bat"
)
ScriptB just runs the installer and cleans up after itself.
Like I said, the goal would be that once scriptA has finished copying over files and gets things kicked off, it would move on to the next PC on the list. I've confirmed that everything works as is right now (so please ignore any random things that wouldn't work in the pseudocode above) it just takes forever because it waits for the installation to finish before moving on.
EDIT:
I realized I had the wrong files shown as being copied. What was originally listed as 'install.bat' should have been scriptB. I apologize, it has been very hard to try to get my question across as I'm having a very hard time describing the problem.
Per Request, here is how I was using start:
ScriptA:
START PSECEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install\ScriptB.bat"
It worked to start ScriptB in a new window, but what I'm really trying to have as the final product is:
scriptA gets prompts for the list of PC names
scriptA pulls the first name from the list and copies over the install files from a network share, including scriptB and puts them in \temp\install
scriptA kicks off scriptB on the PC it just copied files to.
While scriptB is running, scriptA moves on and copies the files to the next PC on the list and kicks of the next copy of scriptB
repeat until the whole list of PCs is completed.
I hope this helps to clarify things, and again, I apologize for the confusion. I have all of the steps listed above working as I would hope, the only thing that isn't working is that I can't get scriptA to kick off scriptB without pausing scriptA to wait for scriptB to finish and what I would like to have happen is that it moves on without waiting (so that I could start this script at the end of the day and it would continue installing at night until it finished the list of PCs).
Thanks again for any help!
You can use the START command to launch a command without waiting for it to end before moving on. Note that this will execute the specified command in a newly created command prompt, not in the same command prompt process.
Be aware though that if the path to your executable/batch file contains spaces, you must specify an empty string argument as the first argument. Example:
START "" "C:\Path with spaces\My other script.bat"
This is due to the fact that if the first arguments is surrounded by quotes, it is considered as being the title of the new command prompt that is launched to execute your new process. Specifying an empty argument (or the title you'd like to use for that matter) will work around this situation.
The further we are away from the actual problem, the less able we are to help. The more general the question, the more vague the response.
We're pointlessly mapping from "ScriptB" to "Install.bat" and you've claimed to have added the start command but not shown us where you've used it.
Using your original script, changing
PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install.bat"
to
start "installing on %%A" PSEXEC \\%%A -u DOMAIN\%NAME% -e "\\%%A\C$\Temp\Install.bat"
should solve the problem - but no guarantees.

trying to pass variables adds extra characters

Ive seen others address this several years back but they didnt seem to give a working answer.
When I try to pass these two commands in a batch file (they work when done by hand) :
nltest /dsgetsite>c:\windows\temp\site.txt
set /p CurrentADSite<c:\windows\temp\site.txt
but when I attempt to issue the commands via a batch file I get this:
C:\working>nltest /dsgetsite 1>c:\windows\temp\site.txt
C:\working>set /p CurrentADSite 0<c:\windows\temp\site.txt
The syntax of the command is incorrect.
How on earth do I actually get this to work? Is there an easier way to pass the dsgetsite results straight into a variable?
You can use for /f to avoid the temp file (also you [usually] can't write to the Windows directory so that will blow up anyway):
for /f %%x in ('nltest /dsgetsite') do if not defined CurrentADSite set CurrentADSite=%%x
According to http://ss64.com/nt/set.html you may have missed out an =
"To place the first line of a file into a variable:
Set /P _MyVar=<MyFilename.txt "
If all else fails you could always fall back on:
:: start the temp batch with start of set command
echo set CurrentADSite=>c:\windows\temp\site.bat
:: add to the temp bat : the variable assignment
nltest /dsgetsite>>c:\windows\temp\site.bat
:: run the temp batch and return here
call c:\windows\temp\site.bat

Resources