0

I have a text file ... 'datarcTemp'

0 data/srdata
1 data/my.dat
2 data/alp1.dat
3 data/ba.dat

I want to find 'alp1.dat' and replace it with 'alp2.dat' I am using for loop to find it, but it is not working. Any ideas ??

set x=2
set y= data/alp1.dat
set line=%x%%y%

for /f "tokens=*" %%a in (datarcTemp) do (
if %%a=="%line%" echo line found
set /a count+=1
rem counting all lines
)
alpha
  • 43
  • 9
  • 1
    As the doublequotes form part of the string, you should be using `if "%%a"=="%line%"`, not `if %%a=="%line%"`. – Compo Apr 03 '19 at 12:03
  • Can you help me to replace 'alp1.dat' with 'alp2.dat' ? – alpha Apr 03 '19 at 12:11
  • 2
    use [substring substitution](https://ss64.com/nt/syntax-replace.html) and don't forget [delayed expansion](https://stackoverflow.com/a/30284028/2152082) – Stephan Apr 03 '19 at 12:17
  • Where do you want to replace? Your code does not output anything except `line found`... – aschipfl Apr 03 '19 at 12:25
  • @Stephan - It is just to rename the variable, not to replace the string in file. – alpha Apr 03 '19 at 12:28
  • @aschipfl - Yes .. I trying to figure out. How can I replace it ..couldn't found yet – alpha Apr 03 '19 at 12:29
  • `set "string=!string:search=replace!"` – Stephan Apr 03 '19 at 12:29
  • @Stephan - It doesn't help. you are replacing the string alone. I have to edit the file – alpha Apr 03 '19 at 12:31
  • read the file line by line, replace the string and write it to a new file. When done, you can change the name back to the original file's. – Stephan Apr 03 '19 at 12:33
  • @alpha, if 'It is just to rename the variable, not to replace the string in file', then you could try `If /I "%%a"=="%line%" Set "line=%x%%y:1=2%"&Echo line found`. – Compo Apr 03 '19 at 12:42

0 Answers0