How to assign a match in FOR loop to a variable - Batch - batch-file

I am trying to assign the value of %%~nxK to variable t but it just seems impossible to do
FOR /D %%K in ("%APPDATA%\Mozilla\Firefox\Profiles\*") DO (
set "t=%%~nxK"
#echo t
)
pause
(note : There is only one folder present in \Profiles*)
I have also tried
set t="%%~nxK"
and
set t=%%~nxK
when trying to #echo t from within the loop or outside the loop I only see the letter "T" and no assigned values

setlocal enableDelayedExpansion
FOR /D %%K in ("%APPDATA%\Mozilla\Firefox\Profiles\*") DO (
set "t=%%~nxK"
#echo !t!
)
pause
in this case you need delayed epxansion

The syntax for print a variable in DOS is echo %t%

Related

Need to copy text from one file to another in batch file

Need to copy text from one file to another.
file1 as below:
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
file2 will be
locked/agent.jms.remote.host=mkdfvsh_a-2341
locked/agent.jms.remote.port=1234
& need to replace when creating the second file
command i used:
SET newfile=file2
SEt filetoCOpy=file1
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:<AGENTHOST>=%AGENTHOST%!
SET line=!line:<AGENTPORT>=%AGENTPORT%!
echo !line!>>%newfile%
)
Result i got
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
variable value not changing.
Can someone help here what's wrong?
You should be getting an error with the code you posted. The < and > are treated as redirection unless they are escaped or quoted:
SET line=!line:^<AGENTHOST^>=%AGENTHOST%!
SET line=!line:^<AGENTPORT^>=%AGENTPORT%!
or
SET "line=!line:<AGENTHOST>=%AGENTHOST%!"
SET "line=!line:<AGENTPORT>=%AGENTPORT%!"
But you still have at least one problem - If you enable delayed expansion within a loop, then you must ENDLOCAL at the end of the loop, otherwise you can run out of SETLOCAL stack space.
Also, I don't see where AGENTHOST or AGENTPORT is defined.
You can try something like that :
#echo off
SET newfile=file2.txt
SEt filetoCOpy=file1.txt
set "ReplaceString1=mkdfvsh_a-2341"
set "ReplaceString2=1234"
If exist %newfile% Del %newfile%
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:^<AGENTHOST^>=%ReplaceString1%!
SET line=!line:^<AGENTPORT^>=%ReplaceString2%!
echo !line!>>%newfile%
)
EndLocal
start "" %newfile%

Clear variables values in batch

I am reading two text files to set the values of two variables (u,l). Now I want to write script to run multiple files. When it is reading first file it will set the variables from the respective files but when it is reading second file it will set the same values of those variables.
#echo on
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
I tried SETLOCAL/ENDLOCAL option but in that case it is not reading variables values and getting error that ECHO is off. Even I wrote set u= and set l= at the initial of the script but not working in my case.
Your code, as given, works fine. However, I'm guessing it is code from inside an if statement, or for loop. If that is the case, you should use delayed expansion. You can use delayded expansion like this:
This is an example, not the exact code you need:
#echo on
setlocal EnableDelayedExpansion
if 1 equ 1 (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
FOR /L %%G IN (1,1,1) DO (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
pause
Note that inside the if statement and for loop, you replace % signs, when they are around variable names, with !. So %someVar% becomes !someVar!, but %%F stays %%F.
Outside of if statements and for loops, so outside of (), you use the normal %someVar%

Assigning a string variable in a dos FOR loop

I have this file structure where each subdirectory contains particular file types, lets say pdfs.
ParentDIR
-->SUBDIR1
-->SUBDIR2
I am trying to run a batch file from the parent to recursively return paths so I can parse them and perform some action
#echo off
for /R %%v in (*.pdf) do (
set pathname=%%~pv
echo %pathname%
)
I would expect the output of the path variable here to read
\PARENTDIR\SUBDIR1
\PARENTDIR\SUBDIR2
but it reads
\PARENTDIR\SUBDIR2
\PARENTDIR\SUBDIR2
If I echo the value of %%~pv without assigning it to a variable, it is correct.
How can I get the value of this variable to be assigned correctly at each iteration through the loop?
#echo off >Nul
setlocal EnableDelayedExpansion
for /R %%v in (*.pdf) do (
set pathname=%%~pv >Nul
echo !pathname!
)
endlocal
Here mind EnableDelayedExpansion keyword of setlocal command = Expand variables at execution time rather than at parse time, if used !pathname! syntax instead of %pathname% one.

Batch map and lookup with expanded variables

I'm trying to do this, but my "v" variable is !expanded!. Augh. I've tried flipping things around but I'm not very good with expansion. How could I adjust the map and lookup to work with an expanded variable?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set map=mon;Monday;tue;Tuesday;wed;Wednesday;thu;Thursday;fri;Friday;sat;Saturday;sun;Sunday
FOR /f "delims=" %%i IN (q20764599.txt) DO (
SET "v=%%i"
CALL :setv
ECHO ==!v!==
)
ECHO +%v%+
GOTO :EOF
:setv
CALL SET v=%%map:*%v%;=%%
SET v=%v:;=&rem.%
GOTO :eof
This should simulate your wanting to work with !v!. The file q20764599.txt could contain say a single line reading tue which gets assigned to v and the magic proceeds from there...

dos batch extract characters from string

I am reading, in a file, the first column which contains 0002C1, 0002C2, 0003C1, 0004C1
Extracting only the first 4 digits and put them in a variable.
FOR /F "tokens=1" %i IN (export.txt) DO (
echo %i
set s=%i:~0,4%
echo %s%
)
in output, the result of echo %i is correct, extracting the digits seems to be working fine also (when I try it for one entry, the result is correct) but the value of s seems to not change!
Can somebody see what the problem is?
Here is the output that I receive:
0002C1
%s%
0002C2
%s%
0003C1
%s%
0004C1
%s%
First: You need yo expand the variable using the SETLOCAL ENABLEDELAYEDEXPANSION command...
Second: you are trying to "cut" a special var (%i:~0,1%), you can't.
My solution:
#Echo OFF
:: By Elektro H#cker
FOR /F %%# IN (export.txt) DO (
Call Set "Token=%%#"
Call Set "Token=%%Token:~0,4%%"
Call Echo %%Token%%
)
Pause&Exit
There are a few issues with your script, only a few adjustments needed and it works great.
You need to use delayed expansion to access a variable that you create in a for loop.
setlocal enabledelayedexpansion
FOR /F "tokens=1" %%i IN (export.txt) DO (
echo %%i
set x=%%i
set s=!x:~0,4!
echo !s!
)
The only difference being you replace the %'s with !'s to tell cmd to use delayed expansion instead to read the variables.
When you are using a batch file you need to use double %'s for the for variables.
You also need to assign %%i to something so you can use a variable sign either side of it, in this case I have used x.

Resources