I have a txt file naemd as (settings.txt )which has content
SET BACKUP_DRIVE=E:\
SET BACKUP_DIRECTORY=BACKUP\
SET HOURLY_DIRECTORY=HOURLY\
SET INPUT_DIRECTORY=D:\MySQL\Data\CDR\.
I have a bat file where I want to use these variables for prepare backup path
SET BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\
But I am not getting prepared path.
I have tried
type settings.txt in the bat file
It is printing content of setting file but not implementing it..showing echo is off..
if I do echo on then some prob also.
Please tell me how to use these variables
Thanks
Try this:
#echo off&setlocal
:: set CURRENT_HOUR for testing
set "CURRENT_HOUR=03"
for /f "delims=" %%i in (settings.txt) do %%i
SET "BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\"
echo %BACKUP_PATH%
Output is:
E:\BACKUP\HOURLY\03\
Related
So I know how to save strings to a text file as a list from batch by using
set /p Myvar1="Enter your variable name/value: "
set /p Myvar2="Enter your variable name/value: "
set /p Myvar3="Enter your variable name/value: "
And then append to a text file
echo %Myvar1% %Myvar2% %Myvar3% >> Mylist.txt
So when I open the text file through the batch file, it can be displayed
as a list:
SET "variables="
ECHO =================================================
ECHO My list of stuff
ECHO =================================================
< Mylist.txt (
set /p MyVar1=
set /p MyVar2=
set /p MyVar3=
)
::set line
ECHO - [0] - %Myvar1%
ECHO - [1] - %Myvar1%
ECHO - [2] - %Myvar1%
Now the problem is that:
For each new line on the Mylist.txt text file I have to manually add lines on the batch file. On the provided example the batch is setup so it displays 3 lines of text from the text file. If the text file has 10 lines, it will only show the first 3 lines because that is what is specified. So I would like to accomplish the opposite of this script.
The batch file should be able to:
Batch file reads Mylist.txt.
For each line in Mylist.txt file the batch file creates a "numerated variable".
Each "numerated variable" can be addressable so the user can be prompted to select one of the options on the list 1, 2, 3, 4, etc.
The easiest method to save variable names and their values to a file is redirecting the output of command SET to a file.
set My >"%APPDATA%\MyVariables.txt"
This simple line saves all environment variables starting with My in name with name and value into file MyVariables.txt in application data directory of current user.
And following command line can be used in a batch file to reload those environment variables from file:
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I"
Same command line for usage directly in a command prompt window:
for /F "usebackq delims=" %I in ("%APPDATA%\MyVariables.txt") do set "%I"
A little bit more secure would be checking if each line in the file MyVariables.txt really starts with My as expected and so was not manipulated by someone who wants to override for example PATH by adding manually to file MyVariables.txt a line starting with PATH=.
It might be useful to know how many variables were loaded from file. This can be achieved with a simple extension:
set "VariablesCount=0"
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I" & set /A VariablesCount+=1
Of course if there is an environment variable MyVariablesCount starting with My then this variable with its value would be also saved into the file and reloaded from file which would make it unnecessary to count the number of variables set from file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
set /?
Read also the Microsoft article about Using Command Redirection Operators.
simple thing with a for loop plus a counter:
setlocal enabledelayedexpansion
set i=0
for /f "delims=" %%a in (mylist.txt) do (
set /a i+=1
set var[!i!]=%%a
)
echo number of variables: %i%:
set var[
echo ---------
for /l %%a in (1,1,%i%) do echo variable %%a = %var[%%a]%
How do I get a Batch file to store data? I've tried using Text files and Dat files but no luck. Could anyone help me out?
I tried this for storing a name:
echo %name% >name.txt
but I'm finding it hard making it extract the name data and printing it in a batch file.
Another way is to save the data like this :
#echo off
set "name=Jaden"
>name.txt echo name=%name%
And to get the value again. Just evaluate the line(s) in name.txt
like this :
#echo off
for /f "delims=" %%a in (name.txt) do set %%a
echo name --^> %name%
First set the value and then if you echo to a file redirect, it will send the content value to a file.
set name=Jaden
echo %name% > name.txt
then it will work.
Just use set /p name=<name.txt
I have a script that I can run in the command line to get me the version of software. It works perfectly in the command line. I type this in getversion "<full path>" and it gets me exactly what I need.
Now the catch is that I have to have the getversion.bat and a vbscript file both in the directory that I'm in for the command line. This is probably a dumb question but if I want to add this into a batch script where the version is set as a variable how would I do that?
right now I had it looking like this
#echo off
set version=getversion "<full path>"
echo %version%
pause>nul
The problem seems to be that the batch file doesn't know where to find getversion.bat or the vbscript referenced in that script. How can I tell the batch file where they are?
One way to accomplish this is to direct the output of your function to a file and then read this output back into a local variable using a SET /P command. This should do it:
SET TempFile="%Temp%\%RANDOM%.txt"
REM Direct output to a temp file.
CALL getversion "<full path>">%TempFile%
REM Read the output written to the temp file into a local variable.
SET /P Version=<%TempFile%
ECHO %Version%
REM Cleanup.
IF EXIST %TempFile% DEL %TempFile%
Alternately (and this way much "cleaner"), you can use a FOR command to run your function and store the output into a local variable:
FOR /F "usebackq tokens=* delims=" %%A IN (`CALL getversion "<full path>"`) DO SET Version=%%A
ECHO %Version%
I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:
#echo off
SET drive=
SET /P drive=Please enter the drive:
SET folder=
SET /P folder=Enter name of folder desired:
SET server=
SET /P server=Enter name of new server:
#echo off > newfile.txt
setLocal EnableDelayedExpansion
if exist newfile.txt del newfile.txt
for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
pause
Every time I run this, the cmd prompt shows:
The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.
It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well
I have searched on stackoverflow, but cannot figure this out with any assistance.
You're pushing your luck using the for loop for that.
A tool like sed would work well.
If you look at this post they have a vbscript implementation that you could use
Is there any sed like utility for cmd.exe
set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984
cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%
How can I store a directory name in a variable inside batch file??
set dirname=c:/some/folder
echo %dirname%
REM retrieve the last letter of the folder
REM and save it in another variable
set lastLetter=%dirname:~-1%
echo %lastLetter%
To return the directory where the batch file is stored use
%~dp0
%0 is the full path of the batch file,
set src= %~dp0
echo %src%
I've used this one when I expect certain files relative to my batch file.
Without more precisions it's difficult to help you...
Maybe you want the current directory?
set name=%CD%
echo %name%
Use the set command.
Example:
rem Assign the folder "C:\Folder1\Folder2" to the variable "MySavedFolder"
set MySavedFolder=C:\Folder1\Folder2
rem Run the program "MyProgram.exe" that is located in Folder2
%MySavedFolder%\MyProgram.exe
rem Run the program "My Second Program.exe" that is located in Folder 2 (note spaces in filename)
"%MySavedFolder%\My Second Program.exe"
rem Quotes are required to stop the spaces from being command-line delimiters, but the command interpreter (cmd.exe) will still substitute the value of the variable.
To remove the variable, assign nothing to the name:
set MySavedFolder=
Since you are inside a batch file, I suggest surrounding your variable usage with setlocal and endlocal at the end of the file; this makes your environment variables local to the batch file and doesn't pollute the global environment namespace.
To allow a script to determine its parent folder directory name:
#echo off
::setlocal enableextensions,enabledelayedexpansion
SET CDIR=%~p0
SET CDIR=%CDIR:\= %
FOR %%a IN (%CDIR%) DO (
SET CNAME=%%a
)
echo %CDIR%
echo %CNAME%
pause
Or, better yet:
#echo off
::setlocal enableextensions,enabledelayedexpansion
SET CDIR=%~p0
SET CDIR=%CDIR:~1,-1%
SET CDIR=%CDIR:\=,%
SET CDIR=%CDIR: =_%
FOR %%a IN (%CDIR%) DO SET "CNAME=%%a"
echo %CDIR%
SET CNAME=%CNAME:_= %
echo %CNAME%
pause