When I run this in a batch file:
for /d %%i in ("%SystemDrive%\Users\*") do (
set myvar=%%i\apple
set myvar=%myvar%\orange
)
My output looks like this:
set myvar=C:\Users\Joe\apple
set myvar=\orange
set myvar=C:\Users\Bob\apple
set myvar=\orange
...
I'm expecting to see set myvar=C:\Users\Joe\apple\orange. Why does myvar appear to be have an empty value even though you can see it being set with one?
Per the comments, this generates the expected result:
setlocal ENABLEDELAYEDEXPANSION
for /d %%i in ("%SystemDrive%\Users\*") do (
set myvar=%%i\apple
set myvar=!myvar!\orange
)
Another solution is to use call. For example:
for /d %%i in ("%SystemDrive%\Users\*") do call :MyFunction
exit
:MyFunction
set myvar=%%i\apple
set myvar=%myvar%\orange
Related
I have some lines of text. Then I have a list with test-words. I like to look up each line of the text and check if one of the test-words appears in it.
Beforehand this works well with a commands like these:
IF not "!stringToTest:%searchstring%=!"=="!stringToTest!"
However, now this seems to be more complicated as I have nested Loops?
I try to create a little MWE for my problem:
#echo off
setlocal enabledelayedexpansion
set /a counterPC=0
set "listPC=Win10,Motherboard,USB-Port,Core"
FOR %%G in (%listPC%) do (
set PCsearchVal[!counterPC!]=%%G
set /a counterPC+=1
)
set /a counterPC-=1
set "dummyline=Environment,1234,ZUIOP,Core"
FOR %%G in (%dummyline%) do (
set "stringToTest=%%G"
echo String to Test: !stringToTest!
FOR /l %%I in (0,1,%counterPC%) do (
set "searchstring=!PCsearchVal[%%I]!"
echo Test for this String: !searchstring!
IF not "!stringToTest:%searchstring%=!"=="!stringToTest!" echo Searchstring is in String to Test
)
)
endlocal
pause
In this he alway enter the IF-Condition. I know that this can may be solved with FINDSTR however in all my other code I used the search-strategy liek above. There may be just a little mistake I oversee? Many thanks in advance-
using this code for example :
setlocal enabledelayedexpansion
set arrayline[0]=############
set arrayline[1]=#..........#
set arrayline[2]=#..........#
set arrayline[3]=#..........#
set arrayline[4]=#..........#
set arrayline[5]=#..........#
set arrayline[6]=#..........#
set arrayline[7]=#..........#
set arrayline[8]=#..........#
set arrayline[9]=#..........#
set arrayline[10]=#..........#
set arrayline[11]=#..........#
set arrayline[12]=############
::read it using a FOR /L statement
for /l %%n in (0,1,12) do (
echo !arrayline[%%n]!
)
pause
I want to use "normal" variable syntax instead of using setlocal enabledelayedexpansion (line 1).
I mean that instead of !arrayline[%%n]! I want to have something like that : %arrayline[n]% in order to put the information in it, in a variable (this way:)
setlocal enabledelayedexpansion
set arrayline[0]=############
set arrayline[1]=#..........#
set arrayline[2]=#..........#
set arrayline[3]=#..........#
set arrayline[4]=#..........#
set arrayline[5]=#..........#
set arrayline[6]=#..........#
set arrayline[7]=#..........#
set arrayline[8]=#..........#
set arrayline[9]=#..........#
set arrayline[10]=#..........#
set arrayline[11]=#..........#
set arrayline[12]=############
::read it using a FOR /L statement
for /l %%n in (0,1,12) do (
set buffer=%arrayline[n]%
echo %buffer%
)
pause
I NEED to put the information in a variable (which is buffer here).
If possible, can somebody replace the necessary code in mine ?
I'm a begginer so if it is not clear enough just ask ;)
Thank you in advance.
CALL echo %%arrayline[%%n]%%
is the classic solution here. This executes echo %arrayline[n]% in a subprocedure.
CALL SET "myvar=%%arrayline[%%n]%%"
CALL ECHO %%myvar%%
I'm trying to simply get the filenames from a directory into a loop and then process (with the eventual aim of then doing a substring on the filename), but can't get it to work.
I've googled plenty and it seems to me that what I'm doing should work, but I'm obviously missing something.
Here's my code:-
setlocal ENABLEDELAYEDEXPANSION
set files=*.*
FOR %%A IN ("%files%") DO (
echo %%A
Set MYVAR=%%A
echo MYVAR=%MYVAR%
)
pause
The echo of the %%A works fine, but MYVAR is always empty. I had this working
on 1 line earlier, before I added the brackets.
Please help. I've spent far too long on this already and am pulling my hair out.
Try using one of the two methods below.
Either:
#Echo Off
SetLocal EnableDelayedExpansion
Set "files=*.*"
For %%A In ("%files%") Do (
Echo=%%A
Set "MYVAR=%%A"
Echo=MYVAR=!MYVAR!
)
Timeout -1
Or:
#Echo Off
Set "files=*.*"
For %%A In ("%files%") Do (
Echo=%%A
Set "MYVAR=%%A"
Call Echo=MYVAR=%%MYVAR%%
)
Timeout -1
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%
I am having an bit of trouble in replacing an string insde an .txt file with Batch
What i have now is this
:SetHostName
set /p HostName=Please Enter your hostname:
setlocal enabledelayedexpansion
set oldstring=%FTP_HOST%
set newstring=%HostName%
for /f "tokens=*" %%i in (FTPConfig.ini) do (
set str=%%i
set str=!str: %oldstring% = "hi"!
echo !str!
)
But when i test it it wont replace the FTP_HOST instead it shows this
FTP_HOST=test
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource
inside my .txt file i have this
FTP_HOST=test
FTP_USER=hi
FTP_PASS=testing
FTP_SERVER_FILE_LOCATION=program
FTP_SERVER_RESOURCE_LOCATION=resource
Any help would be appriciated
I think you have this quite backwards. This code appears to be trying to replace the actual text FTP_HOST, not the value that appears after it. If you fixed the obvious errors, you'd have:
set oldstring=FTP_HOST
set newstring=%HostName%
for /f "tokens=*" %%i in (FTPConfig.ini) do (
set str=%%i
set str=!str:%oldstring%=%newstring%!
echo !str!
)
Assuming I enter my.new.host, that would give the output:
my.new.host=test
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource
I really don't think that's what you intended. What you probably wanted is something like this:
set oldstring=FTP_HOST
set newstring=%HostName%
for /f "tokens=1,2* delims==" %%i in (FTPConfig.ini) do (
set str=%%j
if "%%i"=="%oldstring%" set str=%newstring%
echo %%i=!str!
)
Which yields:
FTP_HOST=my.new.host
FTP_USER=hi
FTP_PASS=testing
FTP_PROGRAM_FOLDER=program
FTP_RESOURCE_FOLDER=resource