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.
valleytechAsked:
Who is Participating?
 
GuruGaryCommented:
Do you want to do the actual compare to be done in the batch file, or would using a standard command that is built into DOS / Windows be good enough?  If the file is a text file, the whole thing could be done in the batch file, but the FC.EXE command has been built into DOS and Windows since DOS 5.00.

The following should work for DOS 5.00 and up, including under a command prompt in all versions of Windows:
@echo off
if not {%2}=={} goto GotParams
echo Pass the 2 files to be compared
goto :END

:GotParams
fc.exe %1 %2 >NUL
if errorlevel 1 goto DIFFERENT
echo Yes
goto END

:DIFFERENT
echo No

:END

If you want something done without the use of the DOS FC.EXE command, please let us know what version of DOS / Windows you are using.
0
 
cryptosidCommented:
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
0
 
valleytechAuthor Commented:
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)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
valleytechAuthor Commented:
i guess DOS Script = DOS Batch?
 Anyway i dont know both of them hic hic
0
 
brettmjohnsonCommented:
Why not use the COMP or FC DOS commands.
0
 
grg99Commented:
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.

0
 
wsteinfordCommented:
>> 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.
0
 
valleytechAuthor Commented:
I am using windows XP
0
 
valleytechAuthor Commented:
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.
0
 
GuruGaryCommented:
The last line of the batch file sould be a colon followed by the word "END" like:

:END
0
 
valleytechAuthor Commented:
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.
0
 
valleytechAuthor Commented:
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.
0
 
GuruGaryCommented:
Do you want to copy instead of file compare?  Or copy only if they are different or the destination does not exist?
0
 
valleytechAuthor Commented:
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.
0
 
GuruGaryCommented:
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?
0
 
valleytechAuthor Commented:
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.
0
 
GuruGaryCommented:
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?
0
 
valleytechAuthor Commented:
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.
0
 
GuruGaryCommented:
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.
0
 
valleytechAuthor Commented:
Dear GuruGary,
  could you please explain for me your code?  Because I don't understand &goto. THanks.
0
 
valleytechAuthor Commented:
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
0
 
GuruGaryCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.