Link to home
Start Free TrialLog in
Avatar of valleytech
valleytechFlag for United States of America

asked on

Dos Script

 hi all,
 I need to write a DOS Script that can compare 2 files. The output will be Yes if they are same, or No if they are different.
 I have the idea of doing comparison by reading lines from each file. If the first line a same, move to next line. If 1 line different, just terminate.
 
  I need to verify that algorithm. Please help me how to start Dos Script since I never work on it. Thanks a lot.
Avatar of cryptosid
cryptosid

Hi,

You should post this question in DOS area, however I wonder if there is a scripting lang in DOS as powerful as unix to read and compare two files line by line.

I doubt it, but would love to know if such an option exists in DOS.

Further, if you are looking for C code, then you should try writing the code yourself using FILE operations and use

fgets() function to read a line each from both files and use strcmp() function to compare both strings...

there are multiple ways to do the same thing.. however i guess you got some hang of it now :-)


Regards,
Siddhesh
Avatar of valleytech

ASKER

I have no idea what is the DOS script. So i don't even know the procedure to program it ( just do'nt care about algorithm)
i guess DOS Script = DOS Batch?
 Anyway i dont know both of them hic hic
Why not use the COMP or FC DOS commands.
This question doesnt make much sense.

There are already commands in DOS to compare files.  "DIFF" and "FC" and "COMP" come to mind.

So there's not much point in writing a program to do this, unless you need some special option, like  ignoring blank lines, or somesuch.

And you can't really write a DOS script (a "batch" file) top do this, as the DOS scripting language doesnt have an easy way to read a line from a fuile, much less two lines from two files.

You*could* write a program in some other programming language.  Most every language except DOS scripting can do this.

Pls advise.

ASKER CERTIFIED SOLUTION
Avatar of GuruGary
GuruGary
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> And you can't really write a DOS script (a "batch" file) top do this, as the DOS scripting language doesnt have an easy way to read a line from a fuile, much less two lines from two files.

This isn't true anymore... in Win2k/xp you can now use switches on the FOR command so that it processes the lines in a file... just type FOR /? (enter) for the help page.  (I'm not expert with it though to offer suggestions, and I can't imagine a way to be reading 2 files at once.)

There is, of course, no reason to try to reimplement FC or DIFF or COMP in a .BAT file, though.
I am using windows XP
Here is the result

C:\BATCH>batchfile file1.txt file2.txt
Same
The system cannot find the batch label specified - END

C:\BATCH>batchfile file1.txt file2.txt
Different


The only minor problem is "the system cannot find the batch label specified-END" Gurugray. Thanks.
The last line of the batch file sould be a colon followed by the word "END" like:

:END
Hi GuruGary,
 could you please help me to extend the batch?
 My purpose is to copy a file from current working directory to another one ( provide by parameter). If there is a file already in destinary  directory -> overwrite it. Otherwise just copy.
 Thanks.
I am sorry to ask you much on this because i can do it in C level or MFC. I don't know about DOS , but i need this kind of tool that code in DOS batch. Thanks for patience.
Do you want to copy instead of file compare?  Or copy only if they are different or the destination does not exist?
i want the batch ask user Yes or No to overwrite the same file in destinay folder. ( so compare is still useful). If the destination doesn't not exist -> show error message. Thanks  lot GuruGary.
A brand new batch file for WinXP to be passed 2 parameters.  The first param should be the file name, and should exist in the current directory (enclosed in quotes if there are spaces).  The second param should be the destination directory (enclosed in quotes if there are spaces).  This batch file will:

Check for 2 parameters (1=file, 2=directory), and exit if there are less than 2
Check to see if the file specified exists in the destination directory - if not, show error and exit
Compare the specified file in the current directory with the same filename in the destination directory
   If files are the same, no need to copy so end
   If files are different, prompt to overwrite using built-in prompt from COPY command

@echo off
if {%2}=={} echo Pass file to copy then the directory to be compared&goto :EOF
if not exist %2%1 echo ERROR: The file %2%1 does not exist!&goto :EOF
fc.exe %1 %2%1 >NUL
if errorlevel 1 (echo Files are different&copy %1 %2 /-y) else echo Files are the same

Is that what you want?
oh sorry Gurugary. I make you confused. Here is the idea
  This batch file will:

Check for 2 parameters (1=file, 2=directory), and exit if there are less than 2.
Check to see if the file will be copied in the current directory ( in which is also has the batch file) are the same with any file in the destination directory.

   If there are 2 same files, aske user to enter Yes to Overwrite the file in destinay directory or No to cancel the copy job
   If there is not any same file, just copy.

 
 it should work exactly the way when we to copy a file by ctr+c and paste in a new place by ctrl-v by using mouse in GUI.

   Thanks a lot.
So you are copying a file from a directory into the current directory?  Also, you said "If the destination doesn't not exist -> show error message", so does that mean if the file in the current direcory does not exist, or if the file in the passed directory does not exist it should show the error?  Also, just out of curiousity ... why would you want to overwrite a file with an identical copy of that same file?
oh i need to copy from current working directory ( where batch file is stored) to another directory( provide as parameter).
  If the file doesn't exist in current working directory -> show error message
  If there is a file has same name in the destination directory -> ask user to overwrite it or cancel copy job
 oh , i just want the batch can do exactly like the mouse and shortcut Ctrl + C, Ctrl +v do. For example, in Windows, I can click on the file first. I can use Ctrl +C to copy the file. Next I go to the destination folder and use Ctrl +V to paste it.

   So I assume the file to be copied is at same directory as the batch file store.

  Thanks a lot.
That is quite a bit different than the original question.

Try this:

@echo off
if {%2}=={} echo Pass file to copy then the directory to be copied to&goto :EOF
if exist %1 (copy %1 %2 /-y) else echo ERROR: The file "%1" does not exist.
Dear GuruGary,
  could you please explain for me your code?  Because I don't understand &goto. THanks.
just wonder where you live. Are you watch world cup now? I am looking a link that I can watch online. Thanks
PS: you're master in DOS
Here is the code broken up:

Line 1
@echo off => don't display any commands including this command

Line 2
if {%2}=={} => if there is no second parameter passed
... echo Pass ... => Output text saying this batch file needs 2 params
... &goto :EOF => after the previous (echo command) also goto :EOF which is the EndOfFile (meaning exit in this case)

Line 3
if exist %1 => %1 is the first parameter which should be a file name, so check to see if that file exists
... (copy %1 %2 /-y) => copy the %1 (file) to %2 (directory) and /-y means make sure to prompt for overwrite if it exists
... else echo ERROR ... => if the IF condition at the beginning of the line is false (meaning the file does not exist) then output text saying so