none
batch file to compare two files

    Question

  • Hi,

    I have to write a batch script for comparing two files.
    The filenames should be like -- HP_OTT_todaydate.txt and HP_OTT_yesterdaydate.txt

    HT_OTT is fixed part in both filenames.

    I want to get today's date and yesterday's date and suffix these date to the fixed part of filename.

    I am using FC command and It should be like this,

    FC C:\HP_OTT_TodaysDate.txt   C:\HP_OTT_YesterdayDate.txt

    I have coded something like this in my batch file,

    FC C:\HP_OTT.txt C:\HP_OTT%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt

    This part

    C:\HP_OTT%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt  

    is suffixing today's date to fixed filename part of first file and giving me file name as HT_OTT04052012 and that's what I am expecting.

    But I am getting difficulty for suffixing yesterday's date to the second filename.

    I have been stucked on this issue since 3 days; any alternative approach would be great helpful.

    P.S. -  I am using Windows 7 Pro 64 bit.

    Friday, May 04, 2012 9:39 AM

Answers

  • Date arithmetic is tricky with batch files. Here is a hybrid solution that will do the trick:

    @echo off
    echo dYesterday = DateAdd("d", Now(), - 1) > "%temp%\tempvbs.vbs" 
    echo WScript.Echo Right("0" ^& Day(dYesterday), 2) ^& Right("0" ^& Month(dYesterday),2) ^& Year(dYesterday) >> "%temp%\tempvbs.vbs"
    for /F %%a in ('cscript.exe //nologo "%temp%\tempvbs.vbs"') do set Yesterday=%%a
    echo Yesterday's date was %Yesterday%

    Friday, May 04, 2012 10:55 AM
  • Many Thanks for your feed Oberwald,

    I am able to solve problem using powershell,

    $yesterday = Get-Date -UFormat %d%m%Y -Date (Get-Date).adddays(-1)
    $today=Get-Date -UFormat %d%m%Y
    
    $strReference = Get-Content C:\batchtest\HP_OTT$today.txt
    $strDifference = Get-Content  C:\batchtest\HP_OTT$yesterday.txt
     Compare-Object $strReference $strDifference >C:\batchtest\result.txt
    

    Friday, May 04, 2012 11:39 AM

All replies

  • Date arithmetic is tricky with batch files. Here is a hybrid solution that will do the trick:

    @echo off
    echo dYesterday = DateAdd("d", Now(), - 1) > "%temp%\tempvbs.vbs" 
    echo WScript.Echo Right("0" ^& Day(dYesterday), 2) ^& Right("0" ^& Month(dYesterday),2) ^& Year(dYesterday) >> "%temp%\tempvbs.vbs"
    for /F %%a in ('cscript.exe //nologo "%temp%\tempvbs.vbs"') do set Yesterday=%%a
    echo Yesterday's date was %Yesterday%

    Friday, May 04, 2012 10:55 AM
  • Many Thanks for your feed Oberwald,

    I am able to solve problem using powershell,

    $yesterday = Get-Date -UFormat %d%m%Y -Date (Get-Date).adddays(-1)
    $today=Get-Date -UFormat %d%m%Y
    
    $strReference = Get-Content C:\batchtest\HP_OTT$today.txt
    $strDifference = Get-Content  C:\batchtest\HP_OTT$yesterday.txt
     Compare-Object $strReference $strDifference >C:\batchtest\result.txt
    

    Friday, May 04, 2012 11:39 AM
  • Many Thanks for your feed Oberwald,

    I am able to solve problem using powershell,

    I am intrigued. You reported that you laboured for three days without finding a solution, then you developed your own PowerShell solution within an hour of posting. Could it be that you were too focused on a pure batch file method?

    Friday, May 04, 2012 12:18 PM