findstr errorlevel

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

findstr errorlevel

#1 Post by joecepy » 16 Dec 2014 15:20

file structure: folder A contains: file1.xml, file2.xml, file3.xml
folder B contains: abc_file1.xml, ddd_file2.xml, ccc_file4.xml

objective:
I'm trying to go through folder A and take just the file name and see if that filename exists in the files listed in folder B.

so file1 would compare abc_file1, ddd_file2, ccc_file4 and it would return file exists only for abc_file1.xml. Then it would go through file2 and looks through folder B. etc.

I'm always returning errorlevel 0. I know it's the findstr but I'm not sure why?

any help would be appreciated.

thanks


Code: Select all

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
:: folder A
FOR /f "tokens=*" %%a IN ('dir /b *.xml') DO (
:: folder B
   FOR /f "tokens=*" %%b IN ('dir /b H:\*.xml') DO (
      SET "oldName=%%~na"

      SET "newName=%%~nb"
       ECHO OLD: !oldName!
       ECHO NEW: !newName!
      echo !oldName! |findstr /i "!newName!" > nul
      IF !ERRORLEVEL! == "1" (
         echo matched
      ) ELSE (
         echo no match
      )

   )
)
Last edited by joecepy on 16 Dec 2014 15:44, edited 1 time in total.

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: findstr errorlevel

#2 Post by Squashman » 16 Dec 2014 15:39

You talk about two folders but i only see you listing the files for one folder.
You should use an IF EXIST instead. It works with wildcards.

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: findstr errorlevel

#3 Post by joecepy » 16 Dec 2014 15:43

the second for loop loops through the files in folder B. I don't want to do a file exist I want do a file exist for a specific string in the list of fiels in folder B

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: findstr errorlevel

#4 Post by Squashman » 16 Dec 2014 16:14

If you are getting file1.xml from folder A you can do an if exist in folder B using a wildcard.
IF EXIST H:\*file1.xml echo match
IF EXIST H:\*%%~a echo match

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: findstr errorlevel

#5 Post by dbenham » 16 Dec 2014 16:24

You have got your search backwards. You are attempting to check if folder B name (new name) is contained within a folder A name (old name). But you want the other way around. There is at least one other issue: You need to specify a literal search, and perhaps others.

But there is a much better way. As Squashman said, you can use IF EXIST with wildcards.

Code: Select all

for %%F in (*.xml) do if not exist "H:\*_%%F" (
  echo   Match: %%F
) else (
  echo NoMatch: %%F
)

If you need to list the matching file(s), then you can use DIR /B

Code: Select all

for %%F in (*.xml) do (
  echo %%F
  dir /b "H:\%%F" 2>&1
  echo(
)
I redirected the "File Not Found" message to stdout so you can easily capture the output of both matches and no matches in a single file.

If you need both file names in variables, then:

Code: Select all

for %%F in (*.xmll) do (
  set "match="
  echo old: %%F
  for %%G in ("H:\*_%%F") do (
    REM do your match action here:
    echo new: %%G
    set match=1
  )
  if not defined match (
    REM do your no match action here:
    echo No Match
  )
  echo(
)


Dave Benham

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: findstr errorlevel

#6 Post by Yury » 16 Dec 2014 18:36

Code: Select all

@echo off

for /f "delims=" %%i in ('dir /a-d/b "A"') do echo _%%i>>.tmp

echo Match:
dir /a-d/b "B"| findstr /eilg:.tmp

echo.

echo Don't match:
dir /a-d/b "B"| findstr /eilvg:.tmp

pause>nul
del .tmp
exit /b

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: findstr errorlevel

#7 Post by Squashman » 16 Dec 2014 21:18

Yury,good use of a temp file. Most of us shy away from temp files but in this case it is quite useful.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: findstr errorlevel

#8 Post by Aacini » 16 Dec 2014 21:45

Code: Select all

@echo off

rem go through folder A and take just the file name
for %%A in (*.xml) do (

   rem and see if that filename exists in the files listed in folder B.
   set /P "=File %%~nA exists for:  "
   for %%B in (H:\*%%~nxA) do set /P "=%%~nB  "
   echo/

)

Antonio

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: findstr errorlevel

#9 Post by joecepy » 17 Dec 2014 07:44

thanks everyone for all the help! you guys are batch gurus!!

Post Reply