1

I have two .txt files. One contains numbers, and the other one contains filepaths. I want to combine these two files to a .csv. The combination is based on wether the number (from nrs.txt) is in the string of the filepath (nodups.txt).

Now I have the following code for this:

@setlocal enableextensions enabledelayedexpansion

for /F %a IN (Output\nrs.txt) DO (
    SET "nrs=%a"

    for /F %b IN (Output\nodups.txt) DO (
        SET "pathstring=%b"
        SET csvdelim=,
        IF NOT x!pathstring:%nrs%=""!==x%pathstring% %nrs%,%pathstring%>>new2017.txt
    )
)

@endlocal

However, I keep having the following issues with the code:

  1. The pathstring never seems to get set. (when I run the code without the if statement, The nrs variable gets set but the pathstring is set to %b). I've seen a lot of possible solutions on here already but none seem to work for me (setting variables like !var! and using usebackq).
  2. The IF statement in the second for loop gets the following error message =""!==x%pathstring% was unexpected at this time. The ="" should remove the nr. from the path (if its there). When I replace "" with something else it still does not work.

The file contents are:

File nrs.txt:

12345
12245
16532

nodubs.txt:

C:\tmp\PDF_16532_20170405.pdf
C:\tmp\PDF_1234AB_20170405.pdf
C:\tmp\PDF_12345_20170506.pdf

Desired output:

12345, C:\tmp\PDF_12345_20170506.pdf
16532, C:\tmp\PDF_16532_20170405.pdf

I really hope someone can help me out with this !

Coen
  • 73
  • 1
  • 7

3 Answers3

1

This solution use a different approach, based on arrays:

@echo off
setlocal EnableDelayedExpansion

rem Load array from nodubs.txt file
pushd "Output"
for /F "tokens=1,2* delims=_" %%a in (nodubs.txt) do set "nodubs[%%b]=%%a_%%b_%%c"

rem Process nrs.txt file and show output
(for /F %%a in (nrs.txt) do (
   if defined nodubs[%%a] echo %%a, !nodubs[%%a]!
)) > new2017.txt
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I prefer this solution since it is more clear to me, as to how it works (and if anything ever changes I know where to make adjustments). However, if I run it like this I don't get any output. If I remove the `@echo off` i get the following: `C:\Path\Output>(if defined nodubs[12345] echo 12345, !nodubs[12345]! ) C:\Path\Output>(if defined nodubs[12245] echo 12245, !nodubs[12245]! ) C:\Path\Output>(if defined nodubs[16532] echo 16532, !nodubs[16532]! ) ` – Coen May 17 '17 at 17:44
  • You may remove the parentheses that surround the for and the redirection, that is, change `(for ... )) > new2017.txt` by `for ... )` and then insert the `>>new2017.txt` redirection at end of `echo %%a, ...` line. – Aacini May 17 '17 at 18:04
  • Thank you so much ! – Coen May 18 '17 at 07:08
0
  • In a batch file for variables need two percent signs.
  • There is no need to put %%A into a variable, use it directly.

@setlocal enableextensions enabledelayedexpansion
for /F %%a IN (Output\nrs.txt) DO (
    findstr /i "_%%a_" Output\nodups.txt >NUL 2>&1 || >>new2017.txt Echo %%a
)
@endlocal
  • Instead of a second for, I'd use findstr to search for the entry of nrs.txt enclosed in underscores.
  • if no find use condiotonal execution on failure || to write to the new file.
  • I'm sorry, I think I was unclear. I need to append both the path and the number in the new file. Is that archievable with your way? Also for some reason the %%a gives me an '%%a was unexpected at this time' error. That is why I used %a (which did seem to work) – Coen May 17 '17 at 12:51
  • Please edit your question to include a sample of your desired output based on sample nrs.txt and nodubstxt –  May 17 '17 at 12:59
  • `%a` and `%b` as used in your examples doesn't work in a batch file and since you're post is tagged batch-file, @LotPings usage is correct. – Compo May 17 '17 at 12:59
  • @LotPings added a desired output. @Compo I ran tests with the windows CMD, I just learned that you can't use `%%A` there. My apologies – Coen May 17 '17 at 13:17
0

According to changed preliminaries another answer.

@Echo on
Pushd Output
for /F "tokens=1-3 delims=_" %%A IN (
  ' findstr /G:nrs.txt nodubs.txt'
) DO >>"..\new2017.txt" Echo %%B, %%A_%%B_%%C
Popd

sample output:

> type ..\new2017.txt
16532, C:\tmp\PDF_16532_20170405.pdf
12345, C:\tmp\PDF_12345_20170506.pdf