Computing.Net > Forums > Programming > Compare file names in batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Compare file names in batch file

Reply to Message Icon

Name: pgilbert11
Date: June 19, 2008 at 07:13:18 Pacific
OS: XP SP2
CPU/Ram: Quad core p4
Product: HP
Comment:

HI

Trying to compare 2 file names and then react depending whether they are the same or not.

Code as follows:
for /f %%a IN ('dir "c:\test\1\*.ver" /b') do @echo %%a
for /f %%b IN ('dir "c:\test\2\*.ver" /b') do @echo %%b
if %%a equ %%b goto same else goto diff

:same
echo same file names
goto end

:diff
echo different filenames
goto end

:end

Have placed the @echo %% at the end of the For lines just so i can see that it is picking up the correct value.

When it gets to the If line however it does not seem to see that the variables are different values.
Please help!!

Thanks
Paul




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 19, 2008 at 08:26:47 Pacific
Reply:

Generally, a goto in a for is a mess.

Beyond that, I thik you are trying to find out which files exist in both; right?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: IVO
Date: June 19, 2008 at 08:47:25 Pacific
Reply:

The code posted is meaningless at all, i.e. absolutely wrong and horrible as it demonstrates the OP has no knowledge of the For /F statement and the scope of the related variables (that are defined *inside* the loop itself and can't be accessed outside the sequence block).

So I give up and maybe M2 is so patient to rewrite the code from scratch.


0

Response Number 3
Name: pgilbert11
Date: June 20, 2008 at 00:59:26 Pacific
Reply:

Cracked it!

For those who might be interested:
@echo off

for /f %%a IN ('dir "c:\test\1\*.ver" /b') do set file1=%%a
for /f %%b IN ('dir "c:\test\2\*.ver" /b') do set file2=%%b

if %file1%==%file2% goto same

echo different filenames
goto end

:same
echo same file names
goto end

:end


0

Response Number 4
Name: toast (by NoIdea)
Date: June 20, 2008 at 01:32:55 Pacific
Reply:

Well done I am interested. Glad you did it!


0

Response Number 5
Name: IVO
Date: June 20, 2008 at 03:46:02 Pacific
Reply:

@NoIdea,

are you sure that code performs properly?

Hmm...


0

Related Posts

See More



Response Number 6
Name: klint
Date: June 20, 2008 at 16:31:25 Pacific
Reply:

It may not work if there's more than one file in either directory.


0

Response Number 7
Name: Mechanix2Go
Date: June 21, 2008 at 04:59:09 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

set /p src=src ?
set /p dest=dest ?

pushd !src!

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
if exist !dest!\%%a echo %%a is in both
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Compare file names in batch file

%DATE% in batch files with win98 www.computing.net/answers/programming/date-in-batch-files-with-win98/4007.html

key-combination in batch file www.computing.net/answers/programming/keycombination-in-batch-file-/16782.html

Find then compare two #'s in batch www.computing.net/answers/programming/find-then-compare-two-s-in-batch/17217.html