0

I have a somewhat complex search and replace that I need to do. I am using a batch file that loops over and executes fart.exe and I am trying to input strings from two different arrays into the "search" field and the "replace" field.

Everything is working EXCEPT that setting the final variables is not working, they are inserted into the commands as empty strings. How do I get the value of my arrays into those variables?

Note that the reason I am searching for the ID and not replacing it is because that is part of the verification that I am replacing the correct link.

My code:

setlocal EnableDelayedExpansion

set i=0
for /F %%a in (merchants.txt) do (
   set /A i+=1
   set merArray[!i!]=%%a
)

for /F %%b in (merchant-ids.txt) do (
   set /A i+=1
   set idArray[!i!]=%%b
)

set n=%i%

for /L %%i in (0,1,%n%) do (
    set merDom=!merArray[%%i]!
    set merID=!idArray[%%i]!
    echo !merArray[%%i]!
    fart.exe -i -r "C:\css_js_test\*.css" http://%merDom%/merchant/%merId%/ http://www.example.com/merchant/%merId%/
)
pause
ChipnCharge
  • 508
  • 5
  • 7
  • The for /l starts at zero while the 1st array is filled from 1. As `i` isn't reset to zero before the 2nd array is filled the arrays aren't congruent. Would IMO make more sense to get a csv with two fields for merchants, IDs. –  Oct 24 '18 at 23:24
  • So you decided to use delayed expansion for some of your variables but not the others? Any reason why you thought that would work? Why are you even bothering making the extra environmental variables? Just use the array variable directly with your fart command. – Squashman Oct 25 '18 at 00:47
  • @Squashman I'm not fluent in batch. I didn't realize I need to set it in different places. Where else do I need to set it? The reason I am using the environmental variables is because using the array variables directly wasn't working either. – ChipnCharge Oct 25 '18 at 01:41
  • @Squashman When I add the array variables directly to the fart.exe command, the output contains !merArray[1,2,3,etc.]! – ChipnCharge Oct 25 '18 at 02:08
  • @LotPings Thanks for the input but resetting i to zero didn't solve my problem. – ChipnCharge Oct 25 '18 at 02:16
  • @ChipnCharge, show me what you did. Also you are not understanding what LotPings is trying to convey to you. You need to reset the variable `%i%` back to zero before the second `FOR /F` command. – Squashman Oct 25 '18 at 02:27

1 Answers1

1

Three issues with your code. 1) You need to reset the counter variable back to zero before the second FOR /F command. 2) The FOR /l command needs to start at 1. 3) You need to use delayed expansion for all your variables inside a parentheses code block. Also note that you can use the array variables directly with the fart command instead of assigning them to another environmental variable.

@echo off
setlocal EnableDelayedExpansion

set i=0
for /F "delims=" %%a in (merchants.txt) do (
   set /A i+=1
   set merArray[!i!]=%%a
)

set i=0
for /F "delims=" %%b in (merchant-ids.txt) do (
   set /A i+=1
   set idArray[!i!]=%%b
)

set n=%i%

for /L %%i in (1,1,%n%) do (
    fart.exe -i -r "C:\css_js_test\*.css" http://!merArray[%%i]!/merchant/!idArray[%%i]!/ http://www.example.com/merchant/!idArray[%%i]!/
)
pause

For the sake of showing you that this worked with my testing, I am just using a single file named input.txt as input. I am running everything from the command prompt so that I can show you the contents of all the files before running the batch file. Then I display the contents of the changed input file at the end.

C:\BatchFiles\FART\SO>type merchant.bat
@echo off
setlocal EnableDelayedExpansion

set i=0
for /F "delims=" %%a in (merchants.txt) do (
   set /A i+=1
   set merArray[!i!]=%%a
)

set i=0
for /F "delims=" %%b in (merchant-ids.txt) do (
   set /A i+=1
   set idArray[!i!]=%%b
)

set n=%i%

for /L %%i in (1,1,%n%) do (
    fart.exe -i "input.txt" http://!merArray[%%i]!/merchant/!idArray[%%i]!/ http://www.example.com/merchant/!idArray[%%i]!/
)

C:\BatchFiles\FART\SO>type merchants.txt
www.ibm.com
www.target.com

C:\BatchFiles\FART\SO>type merchant-ids.txt
101010
202020

C:\BatchFiles\FART\SO>type input.txt
http://www.ibm.com/merchant/101010/
http://www.target.com/merchant/202020/

C:\BatchFiles\FART\SO>merchant.bat
input.txt
Replaced 1 occurence(s) in 1 file(s).
input.txt
Replaced 1 occurence(s) in 1 file(s).

C:\BatchFiles\FART\SO>type input.txt
http://www.example.com/merchant/101010/
http://www.example.com/merchant/202020/
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Thanks for the code rewrite! Unfortunately, the fart.exe command that is being run is still incorrect: fart.exe -i -r "C:\css_js_test\*.css" http://!merArray [769]!/merchant/!idArray[769]!/ http://www.example.com/merchant/!idArray[769]!/ Where 769 is i for each command. – ChipnCharge Oct 25 '18 at 14:30
  • @ChipnCharge, I can tell you the batch file code itself is pretty much rock solid. If `FART` is not working you will have to research that problem yourself. I have used it very minimally only to help others who seemed to refuse to read the help file and Google search for answers but since I don't understand the scope of what you are trying to do with it, I am afraid I can't provide much assistance. – Squashman Oct 25 '18 at 14:34
  • I appreciate your help but the array variable NAME is being sent to fart.exe, not the array variable VALUE. It is not a problem with fart itself. So far I've spent about 8 hours Googling for answers and reading up on fart. – ChipnCharge Oct 25 '18 at 14:57
  • @ChipnCharge, are you using the **EXACT** code I gave you. If not, it will not work. If you have somehow obfuscated your examples thinking you could change them to what you need then I would think you made a change that broke the code. I will update my answer to show you that it is working. – Squashman Oct 25 '18 at 15:18
  • I ran it EXACTLY as you gave it to me once and it didn't make any changes. All the results were "Replaced 0 occurences in 0 files." I removed the "@echo off" so I could see the command that was being given to fart.exe. That's where I got the values that were being given to fart. Does removing that disrupt what is actually going on? – ChipnCharge Oct 25 '18 at 15:48
  • @ChipnCharge, look at the verbose testing I provided in my answer. – Squashman Oct 25 '18 at 15:54
  • @ChipnCharge, you will not see values of delayed expansion variables with ECHO ON because those variables are expanded at time of execution. The verbose contents of the batch file executing happens before that phase. Feel free to read [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) to help you better understand that concept. – Squashman Oct 25 '18 at 16:08
  • I got it running. Permissions are kind of screwed up on the server I am working on and I had to run the command prompt as an Admin. Thanks for all the help!!! – ChipnCharge Oct 25 '18 at 16:20