for command to set a variable does not work - variables

I'm running this command and I don't see why it won't work
setlocal EnableDelayedExpansion
for %%a in (harry-boy) do set %%a:-==
echo %harry%
pause
And this is the result I get -
e:\6\1>setlocal EnableDelayedExpansion
e:\6\1>for %a in (harry-boy) do set %a:-==
e:\6\1>set harry-boy:-==
e:\6\1>echo
ECHO is on.
e:\6\1>pause
Press any key to continue . . .
I'm changing the hyphen sign to a equals sign then running the set command on that. I expect to see that the variable "harry" = "boy"??
Here is a simple test -
set file=play=here.mkv
set %file:==-%
echo %file%
pause
and I get this -
set file=play=here.mkv
==-%
was unexpected at this time.
set %file:==-%
I thought I would get the new contents of file = play-here.mkv. Ok, I see that this makes the syntax wrong and the set command stops. So how do I change the = to a hyphen?

The string replacement format:
%var:old-string=new-string%
does NOT work on for replaceable parameters, just in Batch variables. The equivalent way for your example, using a variable instead, would be:
set a=harry-boy
set %a:-==%
echo %harry%
pause
Output:
C:>set a=harry-boy
C:>set harry=boy
C:>echo boy
boy
C:>pause
Press any key to continue . . .

Easy soloution:
for /f "tokens=1,2 delims=-" %%a in ("harry-boy") do set %%a=%%b
Echo %harry%
And that should do your job for you. But it will only work with one - in the quote.

SET will assign the value on the right of the first = to an environment variable named on the left.
Hence you would be assigning a value of = to a variable named harry-boy:- in BOTH cases.
You can verify this by executing
set harr
which will display any variable starting harr

Related

storing current path in variable and reusing it

I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code.
I'm making a .bat file with the following code
echo off
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause
OUTPUT is :
C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .
So what I dont get is why the value of variable curr_directory is not being printed.
What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory
Thanks
I don't know where to start. EVERYTHING about your code is wrong. This should work:
#echo off
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause
In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.
EDIT: As SomethingDark stated, the first line should be #echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123
use %curr_directory% instead of $curr_directory
Avoid spaces inbetween like the below one
"set curr_directory = %cd%"
below should work
echo off
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause

batch file difference between %variable% and %variable % or variable call with spaces

I've just started with batch file programming and testing variable usage momentary.
Does anyone know what is the difference between the 2 variable calls, the space before the last %
#echo off
set pathOS1="\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
ECHO We're working with %pathOS1%
ECHO We're working with %pathOS1 %
since the echo is different:
We're working with "\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
We're working with \\o1511\Pcs7ProjectO1511\OS1511\GraCS\
Delayed expansion is not enabled.
Spaces are allowed in the variable name.
set "data=100"
set "data =101"
echo %data%
echo %data %
set data
You have two variables with two similar values
So I summarize your answers:
This can happen if there's no setlocal at the start of the routine, any values set in the environment remain set, so if "pathos1 " was set previously, its value will be retained until it's cleared by a set "pathos1 =" statement or cmd is closed.
So, since I didn't close the cmd and didn't set setlocal, I called the previously set variable "pathos1 =".
It is good example for setlocal :)

Variable set in batch file won't show

When I do this in Notepad, the command prompt doesn't show ping %ip% -t -l %package%, but it shows ping %ip% -t -l and doesn't show the package variable.
#echo off
set data=0
set package = -600
IF %data% == 0 (
set /a package= %package% + 1600
#echo ping %ip% -t -l %package%
)
echo %package%
pause
What am I doing wrong?
Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.
Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).
Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.
Note therefore the use of CALL ECHO %%var%% which displays the changed value of var.
So:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /A data=0
set /A package=-600
IF %data% == 0 (
set /a package=!package!+1600
echo ping %ip% -t -l !package!
)
echo %package%
pause
Noting: The setlocal statement should normally be placed at the start of the code. Your posted code is evidently a snip, since you do not appear to be setting ip.
Spaces are irrelevant in a set /a but even so, removing them fosters a habit
Set /a uses the run-time value, not the parse-time value of var when the syntax set /a var=var+1 is used within a loop, so set /a var=var+1 and set /a var=!var!+1 are equivalent but set /a var=%var%+1 uses the value of var at the time the loop is parsed.
Since echoing is set to off by the initial statement, the leading # on the second echo is redundant.

How to store variables within variables in Batch?

I am trying to store a concatenated set of variables into a newly SET variable. When I add a variable into another variable it doesn't seem to actually get set correctly. I'm curious if BATCH can store variables within variables, or if I have to do formatting beyond what I currently have:
Example: The 'oldDirectory' variable should display the same thing as "%progdata%\%datetime%"
#echo off
For /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do (
SET YYYY=%%c
SET MM=%%a
SET DD=%%b
)
For /f "tokens=1-3 delims=/:." %%a in ("%TIME%") do (
SET HH24=%%a
SET MI=%%b
SET SS=%%c
)
SET datetime=%YYYY%%MM%%DD%_%HH24%%MI%%SS%
SET progdata=C:\ProgramData
#echo on
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="%progdata%\%datetime%"
echo %oldDirectory%
)
pause
try with :
CALL SET oldDirectory="%progdata%\%datetime%"
CALL ECHO %oldDirectory%
First method:
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="%%progdata%%\%%datetime%%"
call echo %oldDirectory%
)
Second method:
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="!progdata!\!datetime!"
setlocal EnableDelayedExpansion
echo %oldDirectory%
)
An interesting point is that echo %oldDirectory% command display the current value of progdata and datetime variables with the same value of oldDirectory!
EDIT: Example added
#echo off
set progdata=C:\ProgramData
echo First method:
SET oldDirectory="%%progdata%%\%%date:/=%%_%%time::=%%"
call echo %oldDirectory%
echo Second method:
SET oldDirectory="!progdata!\!date:/=!_!time::=!"
setlocal EnableDelayedExpansion
echo %oldDirectory%
Output:
First method:
"C:\ProgramData\14082013_211303.20"
Second method:
"C:\ProgramData\14082013_211303.21"
You have a standard DELAYED EXPANSION problem, discussed endlessly on SO.
When batch encounters a "Block statement" - that is typically a parenthesised statement spread over many lines such as your IF EXIST then the entire statement is parsed through to the closing parenthesis AND at this time, ANY %var% is replaced by the value of that variable as it stands WHEN THE STATEMENT IS PARSED
Consequently, your ECHO %olddirectory% is replaced by ECHO since olddirectory has no value AT PARSE TIME and executing ECHO will report ECHO is On/Off progdata on the other hand IS set at parse-time and hence echo Found %progdata% is replaced by echo Found C:\ProgramData
The very simplest cure is to move the ECHO statement outside of the block
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="%progdata%\%datetime%"
)
echo Olddirectory=%oldDirectory%
(I added the olddirectory= so that the echo statement finds something to echo if olddirectory is not set)
The second easiest way to display the value is
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="%progdata%\%datetime%"
CALL echo %%oldDirectory%%
)
Here, the ECHO command is not expanded in the context of the IF, but in the context of the CALL which acquires its environment from the run-time value of the IF context.
The third easiest way to display the value is by using the delayedexpansion option of a setlocal command. An NT batch command traditionally starts
#echo off
setlocal
which suppresses echoing and establishes a local environment. Any changes to the local environment are backed out when an endlocal or end-of-file is reached in the setlocal's context. If this mantra is consistently followed, we don't get the situation where a variable is established by one batch and the environment is 'dirty' for the next. Consider running your original twice within the same cmd session. progdata, and all of the other variables you are establishing would remain set for the second coming - and hence olddirectory may be set by your first invocation, and retain that stale data if for some reason it's not EXPLICITLY set in the second. setlocal backs all those changes out for you.
setlocal enabledelayedexpansion adds an extra facility to the mix. Whereas %var% is resolved to the PARSE-TIME value of var, if delayedexpansion has been invoked then !var! is resolved to the RUN-TIME value - as it changes in a FOR loop...
Hence, adding
SETLOCAL ENABLEDELAYEDEXPANSION
at a strategic point (after the #echo off until you're off your training wheels...) would allow you to make a simple change to the display of olddirectory
IF EXIST "%progdata%" (
echo Found %progdata%
SET oldDirectory="%progdata%\%datetime%"
echo !oldDirectory!
)

Removing spaces from a variable in batch

I am writing a file to remove spaces from filenames in a folder and then put the result in a .txt file. I just get a result of "Echo is on." over and over.
This is what I have so far:
#echo ON
SET LOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=%var%
set MyVar=%MyVar: =%
echo %MyVar%>>text.txt
)
Can someone tell me whats wrong?
Removing all spaces (not just leading and trailing) can be done without using setlocal enabledelayedexpansionwith the following line:
set var=%var: =%
This works by replacing all spaces in the string with the empty string.
Source: DOS - String Manipulation
The reason why you are getting ECHO is on. is because delayed expansion was not used, which caused the value of %var% and %MyVar% to be inserted before the for command is run, and since they were not defined at the start, empty variables were inserted in. When the echo %MyVar%>>text.txt was run, it was interpreted as echo >>text.txt. When echo is run without any arguments, it outputs whether echo is on or off, which is what you get in text.txt.
To fix the problem, you have to do two things:
First, there is something wrong with your second line. There is no space between set and local in setlocal. The second line should be SETLOCAL EnableDelayedExpansion.
Second, to use delayed expansion, you have to replace all %s in each variable with !, like !var! instead of %var%.
End result:
#echo ON
SETLOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=!var!
set MyVar=!MyVar: =!
echo !MyVar!>>text.txt
)
You actually do not need to use a temporary variable in this case, you can just do SET MyVar=%%~n# and skip to set MyVar=!MyVar: =!.
The wrong thing is you've enabled the variable expansion (you wroted it bad...) and also you are not using it, when you use enabledelayedexpansion you need to write the variable names as this: !Variable! instead of this else: %Variable%
But you don't need to use it with this code:
#echo ON
For %%# in (*) do (
SET "var=%%~n#"
Call Set "MyVar=%%var: =%%"
Call echo %%MyVar%%>>text.txt
)
Run the following batch in the folder holding the files to be renamed
#echo off
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename=.=_!
set filename=!filename= =_!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
you just need to add the print to txt
The set var=%var: =% did not work for me.
So I tried with success for a number the following code:
set /a var-=1 & set /a var+=1