Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: search in log for file names in folder  (Read 3499 times)

0 Members and 1 Guest are viewing this topic.

schumway

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 7
    search in log for file names in folder
    « on: May 04, 2019, 11:36:19 PM »
     Not sure what I am doing wrong here...
    Windows 7 but running it as a dos batch file

    have a bunch of PDFs in a folder (c:\files\*.pdf) and I want to see which of these files are not in a log file.

    in the log file the messages are (once per file):
    >>>log.txt
    Login name is [email protected]
    Attached binary file: MAY2019_1978727_E.pdf
    File saved.
    >>>

    What I thought I would need was...

    @echo off
    for %%f in (c:\files\*.pdf) DO (
    echo %%~nxf
    findstr /m %%~nxf log.txt
    if %errorlevel% NEQ 0 (
    echo %%~nxf was not found XXXXXXXXX
    )
    )


    pseudo code...
    for all the PDFs in the "files" folder
    find the file name (no path header) in the log file log.txt
    If the file is not in the folder
    then echo the file name was not found XXXXXX

    basically the log file confirms all the files that should be in the folder but looking for any PDFs that are not in the log file for some reason

    I am sure it is something simple but brain stopped working after version 15 and a couple of hours googling lol

    thanks


    Salmon Trout

    • Guest
    Re: search in log for file names in folder
    « Reply #1 on: May 05, 2019, 10:29:46 AM »
    You can't set + read a %...% type variable in a loop; you have to use delayed expansion and use ! instead of % around the variable name.

    @echo off
    setlocal enabledelayedexpansion
    for %%f in (c:\files\*.pdf) DO (
        echo %%~nxf
        findstr /m %%~nxf log.txt
        if !errorlevel! NEQ 0 (
            echo %%~nxf was not found XXXXXXXXX
        )
    )


    Salmon Trout

    • Guest
    Re: search in log for file names in folder
    « Reply #2 on: May 06, 2019, 05:59:51 AM »
    If you just want a console message saying whether or not a PDF file was found in the log, you can simplify the batch a lot, to one line in fact:

    @for %%f in (c:\files\*.pdf) do @findstr %%~nxf log.txt >nul && (echo %%~nxf found: YES) || (echo %%~nxf found: NO XXXXXXX)

    Code: [Select]
    FAQ.pdf found: YES
    faq2.pdf found: NO XXXXXXX


       

    schumway

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Windows 7
      Re: search in log for file names in folder
      « Reply #3 on: May 06, 2019, 08:21:40 AM »
      Many thanks... this worked great. I just created a small batch file to call the one below and piped it to another log file and then just did a search for the XXXX... interesting tidbit... was expecting 15 and found 27 (out of 5500) so it has also found another potential issue we need to look into. Thank-you for that as well!

      You can't set + read a %...% type variable in a loop; you have to use delayed expansion and use ! instead of % around the variable name.

      @echo off
      setlocal enabledelayedexpansion
      for %%f in (c:\files\*.pdf) DO (
          echo %%~nxf
          findstr /m %%~nxf log.txt
          if !errorlevel! NEQ 0 (
              echo %%~nxf was not found XXXXXXXXX
          )
      )