0

I'm comparing two folders of pdf files. First I check which files are new and save all new files in an array anew:

setlocal enabledelayedexpansion
FOR /F "delims=. tokens=1" %%I IN ('dir "%Rev2Dir%\*.pdf" /b') DO (
    if not exist "%Rev1Dir%\%%I.pdf" (
        IF NOT EXIST new.txt (
            ECHO %%I new > new.txt
        ) ELSE (
            ECHO %%I new >> new.txt
        )
        ECHO %%I.pdf ^(new^)
        copy "%Rev2Dir%\%%I.pdf" "%Rev1Dir%\%%I.pdf"
        set /A i=i+1
        set anew[!i!]=%%I
    )
)

This works fine. I'm able to display all those files with:

set k=%i%
for /L %%i in (1,1,%k%) do echo file number %%i: "!anew[%%i]!"

Now it's getting tricky. Within an other loop, I'm checking whole first folder and need to know if the file is new or not:

FOR /F "delims=. tokens=1" %%I IN ('dir "%Rev1Dir%\*.pdf" /b') DO (
    IF EXIST "%Rev2Dir%\%%I.pdf" (  
        echo file is %%I
        echo j is !j!
        if "%%I" == "!anew[%j%]!" (
            set /A j=j+1
            ECHO %%I.pdf ^(new^)            
        ) Else (
            echo do someting
        )

)

The issue is, that !anew[%j%]! is still anew[1]. The variable j will not update. For my echo it works fine. So I guess I need something like !anew[!j!]! (which does not work) Can someone assist.

  • Array management is explained with detail at [this post](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Nov 28 '14 at 22:03

3 Answers3

2

I suspect your entire script is way more convoluted than need be. But I don't fully understand your end goal, so I don't have any suggestions for a better way.

But there is a very simple way to fix your immediate problem in your existing code.

You are already using delayed expansion because regular expansion does not show the current value within your loop. But you need two levels of expansion when you attempt !anew[%j%]!. But %j% suffers from the problem that led to use of delayed expansion in the first place. The trick is to transfer the j value to a FOR variable.

Replace this line:

        if "%%I" == "!anew[%j%]!" (

with this:

        for %%j in (!j!) do if "%%I" == "!anew[%%j]!" (
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I'd agree we're trying to debug a solution to an unstated problem. I see is a bug here with the underlying methodology. The match is made to the next-sequential name only. If the names selected in the second phase match entries [1],[3],[4] then that method will "stick" on [2]; hence I chose to match against the entire match-me array. Also there's a fail-to-fail scenario: If the directories are on an NTFS drive, then the names should arrive in alphabetical order, but not if the drives are FAT. OP has also not used `if /i` to guard against case-mismatches...but good approach for nested-expansion – Magoo Nov 28 '14 at 18:25
  • @Magoo - yeah, I wanted to acknowledge that the overall OP code is less than ideal, but I didn't have the patience to figure out what it is supposed to do, or determine whether my fix would actually make the overall code work. – dbenham Nov 28 '14 at 18:57
0
FOR /F "delims=. tokens=1" %%I IN ('dir "%Rev1Dir%\*.pdf" /b') DO (
 IF EXIST "%Rev2Dir%\%%I.pdf" (  
  echo file is %%I
  set "found="
  for /f "tokens=1*delims==" %%m in ('set anew[ 2^>nul') do if "%%I" == "%%m" set found=Y
  if defined found ECHO %%I.pdf ^(new^)            
  ) Else (
   echo do someting
  )
 )
)

should work for you. It checks any envvar starting anew[ for a match against %%I and if i's found sets found to non-empty. if defined operates on the run-time value of the target variable.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

However I see exhaustive Magoo's answer... Indirect variable addresing:

Instead of all failed attempts like !anew[%j%]! or !anew[!j!]! (or all other combination of % and !) apply next approach:

set anewref=anew[!j!]
call :indirect "anewval=%%!anewref!%%"
echo j !j!, anewref !anewref!, anewval !anewval!


goto :eof
:indirect
  set %~1
exit /B
JosefZ
  • 28,460
  • 5
  • 44
  • 83