Page 1 of 1

findstr errorlevel

Posted: 16 Dec 2014 15:20
by joecepy
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
      )

   )
)

Re: findstr errorlevel

Posted: 16 Dec 2014 15:39
by Squashman
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.

Re: findstr errorlevel

Posted: 16 Dec 2014 15:43
by joecepy
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

Re: findstr errorlevel

Posted: 16 Dec 2014 16:14
by Squashman
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

Re: findstr errorlevel

Posted: 16 Dec 2014 16:24
by dbenham
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

Re: findstr errorlevel

Posted: 16 Dec 2014 18:36
by Yury

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

Re: findstr errorlevel

Posted: 16 Dec 2014 21:18
by Squashman
Yury,good use of a temp file. Most of us shy away from temp files but in this case it is quite useful.

Re: findstr errorlevel

Posted: 16 Dec 2014 21:45
by Aacini

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

Re: findstr errorlevel

Posted: 17 Dec 2014 07:44
by joecepy
thanks everyone for all the help! you guys are batch gurus!!