1

Ok, so my end goal is to iterate through a text file, and for each line in the text file, use that line as a key in an array to store a value of 1.

Then, loop through files in a directory and see if there is a value in the aforementioned array for the current filename in this iteration.

My code is as follows

@echo off
setlocal enabledelayedexpansion

set filesRan[0]=1
set i=0
for /f "tokens=*" %%a in (input.txt) do (
  set /A i+=1
  set filesRan[%%a]=1
)

set patchesRan[0]=1

for %%f in ("C:\Users\kkennedy\Source\Repos\GeneSeek-DataHub\dbScripts\DDL\Patches\*") do (

    echo %%f

    echo %%~nf

    set thisFileName=%%~nf

    echo triple x !thisFileName!

    echo triple x !filesRan[%%thisFileName]!

    IF "!filesRan[%%thisFileName]!" EQU "1" (
        ECHO already ran it
    ) ELSE (
        ECHO did not run it
    )

    set patchesRan[!%%~nf!]=1

)

The exact line that is not producing the results I'm expecting is:

echo triple x !filesRan[%%thisFileName]!

I'm coding a bit blindly here with batch, so I'm guessing it is something fairly simple. For instance, is my syntax correct for referencing the array item?

Kamron K.
  • 594
  • 1
  • 10
  • 18
  • 5
    Don't assign the FOR variable to an environmental variable. Just use the FOR variable directly. You don't reference environmental variables with two leading percent symbols. They are referenced by using a percent symbol on each side of the variable name. – Squashman Aug 31 '17 at 17:07
  • Batch files don't support arrays, using [] does not change anything, you might as well just use a underscore. – Anders Aug 31 '17 at 18:43
  • 1
    Though using the brackets (which is apparently legal -- learn something new every day) does help convey the intent to someone else reading the code, at the cost of an extra character in the environment variable name. – Steven K. Mariner Aug 31 '17 at 21:29
  • All array management details are fully described at [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Sep 01 '17 at 02:39

0 Answers0