0

I would like to know if there is a way where I can use a for loop on an expanded variable.

Is it something recommended or which can be done?

I have a script which reads a text file which has many dimensions such as State%%20Country, City%%20Country, etc. I save them into a variable (lets call it !P!). So !P1! now has:

!P1!
City%%20Country
State%%20Country
...

The script runs a for loop and reads each row and based on that the value of !P1! changes.

I would like to know or appreciate if some one can let me know I can loop over the variable as I would like to replace the %%20 which is part of the string contained in !P1! with Space.

So what I am trying is :

for /F "delims==" %%A in ('!P1!') do ...

I know this wont work, but can someone let me know if there is a way to do this or if this is the right approach?

Anks
  • 17
  • 1
  • 5

1 Answers1

0
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION

SET "p1=something%%%%20or%%%%20other"
SET "repl=%%%%20"
SET "p1=!p1:%repl%= !"
FOR %%a IN (%p1%) DO ECHO %%a

Not sure whether your problem has to do with a variable named !p1! or you are using delayed expansion on variable p1. The above may assist.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • You think the environment variables have the name `P1`, `P2`, ... but you are wrong. Anks named the environment variables `!P1!`, `!P2!`, ... Yes, really, the environment variable names contain at begin and end an exclamation mark. You might not believe that, but it's true. This question by Anks is a duplicate of [Replace %%20 in string using windows batch script](https://stackoverflow.com/questions/46416238/) where the working code is already posted (and accepted). The problem here is that Anks has no idea what he/she is doing. – Mofi Oct 11 '17 at 06:14
  • @Mofi : Even though I've been unemployed now effectively for 16 years, I still can't find the time to research prior answers - never mind remember them. I suppose I just assume that since the automatic reaction these days is to google it, and that would be my first port of call if I had a problem, then OP would have done so. I really don't have the time or inclination to do the basic research that should have been done before posting a question. Congratulations on your power of recall. – Magoo Oct 11 '17 at 07:19
  • @Mofi I have the answer from the previous question, want to try/learn this approach. – Anks Oct 11 '17 at 19:51