?
Solved

Dos Script

Posted on 2006-06-07
23
Medium Priority
?
1,476 Views
Last Modified: 2012-06-27
 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.
0
Comment
Question by:valleytech
22 Comments
 
LVL 5

Expert Comment

by:cryptosid
ID: 16854515
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
 

Author Comment

by:valleytech
ID: 16854539
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
 

Author Comment

by:valleytech
ID: 16854607
i guess DOS Script = DOS Batch?
 Anyway i dont know both of them hic hic
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16855365
Why not use the COMP or FC DOS commands.
0
 
LVL 22

Expert Comment

by:grg99
ID: 16856155
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
 
LVL 10

Accepted Solution

by:
GuruGary earned 2000 total points
ID: 16856555
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
 
LVL 3

Expert Comment

by:wsteinford
ID: 16861228
>> 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
 

Author Comment

by:valleytech
ID: 16863696
I am using windows XP
0
 

Author Comment

by:valleytech
ID: 16863766
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16864150
The last line of the batch file sould be a colon followed by the word "END" like:

:END
0
 

Author Comment

by:valleytech
ID: 16866096
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
 

Author Comment

by:valleytech
ID: 16866120
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16866143
Do you want to copy instead of file compare?  Or copy only if they are different or the destination does not exist?
0
 

Author Comment

by:valleytech
ID: 16866170
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16866536
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
 

Author Comment

by:valleytech
ID: 16866615
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16866691
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
 

Author Comment

by:valleytech
ID: 16871138
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16871993
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
 

Author Comment

by:valleytech
ID: 16872106
Dear GuruGary,
  could you please explain for me your code?  Because I don't understand &goto. THanks.
0
 

Author Comment

by:valleytech
ID: 16872113
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
 
LVL 10

Expert Comment

by:GuruGary
ID: 16872501
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question