Almost there… Need to find and replace. Using for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Almost there… Need to find and replace. Using for loop

#1 Post by pditty8811 » 20 Mar 2013 23:43

0down votefavorite





I am trying to find and replace. I am using a for loop. I am learning but I am almost there. I have tried to find the answer but the sources have been a bit too confusing for me.

The delim is a blank space and as you can tell I am skipping 4 lines and doing the 2nd token.

I need what is found there at that spot to be replaced by var5a. I have it backwards, as I need %%F to equal var5a, not the other way around (as I have it written now). But don't know how to write it. Please explain how one can do this. I've tried using <<= but with no luck.

Code: Select all

for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
set var5a=!var5a!%%F
)


I'm learning so please be kind.

I thought about writing:

Code: Select all

set var5a<<=!var5a!


or

Code: Select all

set %%F=!var5a!


but those don't work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Almost there… Need to find and replace. Using for loop

#2 Post by foxidrive » 21 Mar 2013 00:15

Code: Select all

@echo off
setlocal enabledelayedexpansion
 >script.vbs echo one two
>>script.vbs echo a b
>>script.vbs echo c d
>>script.vbs echo e f
>>script.vbs echo three four
>>script.vbs echo g h

set var5a=test
for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
set %%F=!var5a!
set %%F
goto :gotit
)
:gotit
pause


Output is this:

four=test
Press any key to continue . . .

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#3 Post by pditty8811 » 21 Mar 2013 00:29

Those aren't the contents of script.vbs though.

The contents of script.vbs are different all the time. How to get it to work. If I use your code it ends up looking like you placed with the one two, a b, cd ....

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#4 Post by pditty8811 » 21 Mar 2013 00:30

script.vbs is a file. And I'd like to replace %%F with variable var5a

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Almost there… Need to find and replace. Using for loop

#5 Post by foxidrive » 21 Mar 2013 02:01

It's an example. It shows how to set the variable which is what you asked to do.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#6 Post by pditty8811 » 21 Mar 2013 02:06

I'm trying to replace %%F inside script.vbs with variable var5a. What am I doing wrong?

I need to do a find and replace, not finding any specific word, but finding a word in a particular location which I have pointed to already (skip=4 tokens=2 delims= )

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#7 Post by pditty8811 » 21 Mar 2013 02:07

foxidrive wrote:It's an example. It shows how to set the variable which is what you asked to do.


I stated I'm trying to find and replace. Setting the variable is only part of it. What am I missing?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Almost there… Need to find and replace. Using for loop

#8 Post by foxidrive » 21 Mar 2013 02:14

Explain it again.

You used code and a script file but didn't show what was in the script, nor did you explain what you want in the final outcome.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#9 Post by pditty8811 » 21 Mar 2013 02:22

Sorry about that. Here goes:


Let's say var5a = 19401211

I'd like it to replace %%F in script.vbs as where I said it was located. %%F will change all the time. In this example %%F is 19401212

Here is an example of script.vbs:

Code: Select all

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
WshShell.SendKeys "s"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "19401212"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "19421212"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "9000"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "8000"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "850"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "800"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "y"
WshShell.SendKeys "{ENTER}"



I'd like to change what is in bold. I'd like the number to be within quotes too. So I'd like it to look like this:

Code: Select all

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
WshShell.SendKeys "s"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "19401211"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "19421212"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "9000"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "8000"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "850"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "800"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "y"
WshShell.SendKeys "{ENTER}"



var5a is never constant and %%F is never constant.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Almost there… Need to find and replace. Using for loop

#10 Post by foxidrive » 21 Mar 2013 02:44

If you want to change part of that script then this works here.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set var5a="19401211"
set c=0
del script.tmp 2>nul
for /f "delims=" %%a in (script.vbs) do (
set /a c=c+1
if !c! EQU 5 (
>>script.tmp echo WshShell.SendKeys %var5a%
) else (
>>script.tmp echo %%a
)
)
move /y script.tmp script.vbs >nul


pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#11 Post by pditty8811 » 21 Mar 2013 03:46

Works great.

I need help with a script you wrote before. I am trying to grab var6 which is one line above var4 in "PosTime_Campaign_SCR.mis". var4 is found after "Class=". And var6 is found on the line above which includes and starts the line with "Name="

Here is my code:

Code: Select all

set /a maxlines4=1
set /a linecount4=0

findstr /b "Class=!var4!" "PosTime_Campaign_SCR.mis"
if not errorlevel 1 (
for /f "skip=-1 tokens=* " %%L in ('findstr "Class=!var4!" PosTime_Campaign_SCR.mis') do (
if not errorlevel 1 (
set var6=!var6!%%L

set /a linecount4+=1
if !linecount4! GEQ %maxlines4% GOTO exitloop5
)
)
)

if  errorlevel 1 (
goto exitloop4
)


skip=-1 doesn't move one line above the found string. How do I do that in a for loop? I'd like to move one line above the found string in order to grab the variable on. I'd like to make the whole line above var6

This is what it will look like in "PosTime_Campaign_SCR.mis"

Name=Bismarck #13 --------var6 (whole line)
Class=Bismarck -------------(Bismarck is var4)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Almost there… Need to find and replace. Using for loop

#12 Post by foxidrive » 21 Mar 2013 08:07

Code: Select all

@echo off
setlocal enabledelayedexpansion

set var4=Bismarck

set /a maxlines4=1
set /a linecount4=1

findstr /b "Class=!var4!" "PosTime_Campaign_SCR.mis" >nul

if not errorlevel 1 (
for /f "delims=[]" %%a in ('find /n /i "Class=!var4!" ^<"PosTime_Campaign_SCR.mis"') do (
for /f "delims=" %%b in (' type "PosTime_Campaign_SCR.mis" ') do (
set /a linecount4+=1
if !linecount4! EQU %%a set var6=%%b
)
)
)
echo %var6%
pause

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#13 Post by pditty8811 » 21 Mar 2013 12:37

That isn't working for some reason.

Let's try it simpler.

Code: Select all

findstr /b "Class=!var4!" "PosTime_Campaign_SCR.mis"
if not errorlevel 1 (
for /f "skip=-1 tokens=* " %%L in ('findstr "Class=!var4!" PosTime_Campaign_SCR.mis') do (
if not errorlevel 1 (
set var6=!var6!%%L
)
)
)


Thank you.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Almost there… Need to find and replace. Using for loop

#14 Post by pditty8811 » 21 Mar 2013 12:58

I'm sorry. It's not working and its taking a long time to tell me that. It is grabbing the first thing that it finds in [].

I only want it to find the first instance of var4 and then grab var 6, ignoring everything else below it. That may make it faster.
Last edited by pditty8811 on 21 Mar 2013 13:15, edited 1 time in total.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Almost there… Need to find and replace. Using for loop

#15 Post by mfm4aa » 21 Mar 2013 13:01

"skip=x" ---> x must be greater than zero!

Post Reply