Setting system variables using bat file - batch-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!!

Related

Determine end of path with a wildcard in .bat

I'm trying to copy a folder with a variable path to a fixed path using a .bat file. Basically trying to retrieve a folder from a shared server to use it locally. The path to the variable location can mostly be solved by retrieving an user input. However, the user input will only cover a large part of the to retrieve path. At the end there is still a variable component. That's where I'm stuck. Is there any way to use a wildcard in .bat to retrieve a folder? Searching stackoverflow teaches me that it can only be used for files and not folders, is there a workaround?
Code:
#echo off
set /p enddirectory=enter your folder:
set fixedpath=C:\xxx\xxx\folders\
set endpath=%fixedpath%%enddirectory%\ (*wildcard to be added here)
Xcopy /e %endpath% "C:\xxx\localfolder"
For example:
Server path : C/xx/ABC-1234567
User input: ABC
Defining the first path of the server path works, how can I get the full path? (1234567 is variable)

Script to Replace Text in %PATH% Environment Variable

As of right now when we update Java on a PC which users a certain program we use I have had to manually update the system path environment variables with the latest Java version number in the path (C:\Program Files (x86)\Java\jre1.8.0_141\bin\client).
My goal is to automate this process via a DOS script if possible. I am far from a coder but would like this script to search for the "jre1.8.0_141" then replace it with whatever build number the latest update is in the %PATH% variable. This way I can leave each individual PC's environment variables in tact and only edit the Java string.
I have looked at merging entries with the registry but would like to avoid that as all systems are not identical.
This is my script so far:
This is what I have so far:
REM Exports the Systems Environmental Variables
reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "C:\Software\%COMPUTERNAME%_ENV_VAR_BACKUP.reg" /y
REM Searchs for Java path and changes if necessary
IF "%PATH%" = "*jre1.8.0_141*" (
THEN **NOT SURE WHERE TO GO FROM HERE**
)
I know what I want to accomplish but I have no idea how to translate that into a working batch file so I apologize I have no more information for you.
Thank you for any help.

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.

Why does changing working directory to desktop subfolder using environment variable UserProfile not work?

I am making this code in batch, in which the directory needs to be changed in order for the commands to complete successfully. Right now, this is what I've discerned to use, from other posts asking the same question:
cd C:\Users\%UserProfile%\Desktop\NewFolder
From what I read, this should work, but it isn't. And I don't know why.
Can anyone please help me on this?
Read HELP SET and then try SET user and you will realize that USERPROFILE contains the full path of the current user home directory.
So, to set the current working directory to the user desktop, you need to either
CD /D %USERPROFILE%\Desktop
or
PUSHD %USERPROFILE%\Desktop
which is better, because you may later POPD to restore the current directory to its initial value.
Try cd %USERPROFILE%\Desktop\NewFolder
Use cd /D "%USERPROFILE%\Desktop\NewFolder"
Environment variable USERPROFILE contains full path to the profile directory of the current user. The user name stored in environment variable USERNAME which is included also in USERPROFILE can contain also 1 or more spaces and therefore it is better to use double quotes around complete path.
And the user's profile directory can be on a different drive as the current drive. So it is better to use also parameter /D on changing the current directory.
By the way: Executing in a new command prompt window the command set shows all standard environment variables with their current values.

Get Server name of batch file from within batch file

MachineX is calling a batch file through UNC path to either MachineA or MachineB, depending on server failover status:
If everything is good, the batch is called via path \\MachineA\files\Batch1.bat.
If there is a failover, MachineX knows to call the batch via path \\MachineB\files\Batch1.bat.
Without using an additional parameter, or tapping into how MachineX knows which machine to call, I need to know within the batch which machine it's running on, so it can access other files on the machine. To do this, I want to store the machine name in a variable.
Because the executing machine is MachineX, I can't use a hostname variable to pull the batch, but since the batch is always called with a fully-qualified UNC path, argument %0 will have the machine name in the path. Since the system is part of a mirroring setup, the batch files are always kept identical, so hard-coding is not possible.
What is the simplest method of getting the machine name from within the batch file, without having to go external? Thanks in advance.
EDIT:
Alright, I figured it out:
for /f "tokens=1,2,3,4,5 delims=\" %%a in ('echo %~p0') do set svrpth=%%a
This works fine so long as there are no more than 5 folders in the path trail, which for this purpose is acceptable.
Thanks for all your help.
if path is the same on every machine it looks as you can simply remove that from %0 variable (path to called script) - what is left will be machine name

Resources