Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file help  (Read 5077 times)

0 Members and 1 Guest are viewing this topic.

Faded-Maximus

    Topic Starter


    Starter

    Batch file help
    « on: March 18, 2009, 07:54:41 AM »
    Trying to write a batch file for windows that will read from two files and execute the fc command on line 1 of each file, line 2, etc.

    file1 contains:
    file1.txt
    file2.txt

    file2 contains:
    file3.txt
    file4.txt

    the script would run fc file1.txt file3.txt then fc file2.txt file4.txt

    ----
    Here is the unix version:
    Code: [Select]
    #!/bin/bash
    directoryA=A.txt
    directoryB=B.txt
    numLines=0
    output=diff.txt
    rm diff.txt

    while read line
      do numLines=$(($numLines+1));
    done < $directoryA

    for ((i = 1; i <= $numLines; i++))
      do
        lineA=`sed -n $i\ p $directoryA`
        lineB=`sed -n $i\ p $directoryB`
    echo "$lineA $lineB Comparison:" >> $output
        diff $lineA $lineB >> $output
    done

    here is what I have for the batch file so far, but am stuck..
    Code: [Select]
    @echo off

    set directoryA="A.txt"
    set directroyB="B.txt"
    set output=diff.txt
    set /a numLines=0
    del diff.txt

    for /f "eol= tokens=* delims= usebackq" %%i in (%directoryA%)
     do
      SET /a lineA=
      SET /a lineB=
      echo "%lineA% %lineB% Comparison:" >> %output%
      fc %lineA% %lineB% >> %output%

    ghostdog74



      Specialist

      Thanked: 27
      Re: Batch file help
      « Reply #1 on: March 18, 2009, 08:26:07 AM »
      who says you can't use bash in windows? :)
      Code: [Select]
      file1="file1.txt"
      file2="file2.txt"
      i=0
      j=0
      x=0
      while read line
      do
       input1[$i]="$line"
       i=$(( i+1 ))
      done < "$file1"

      while read line
      do
       input2[$j]="$line"
       j=$(( j+1 ))
      done < "$file2"

      while [ "$x" -le "$j" ];
      do
      echo diff ${input1[$x]} ${input2[$x]}
      x=$(( x+1 ))
      done


      Code: [Select]
      C:\test>bash test.sh
      diff file1.txt file3.txt
      diff file2.txt file4.txt

      download diff for windows here

      Faded-Maximus

        Topic Starter


        Starter

        Re: Batch file help
        « Reply #2 on: March 18, 2009, 08:30:14 AM »
        Nobody ever said that really, but is there a batch only way of doing it and not downloading bash for windows? None of the computers have bash for windows, and if there was a solution that didn't involve that, I would prefer it.

        ghostdog74



          Specialist

          Thanked: 27
          Re: Batch file help
          « Reply #3 on: March 18, 2009, 08:46:45 AM »
          sure, here's a vbscript solution. However, it only echoes out the commands at the end. I leave it to you to find out how to call external command fc from within vbscript

          Code: [Select]
          Dim Input1_Arr()
          Dim Input2_Arr()
          Set objFS = CreateObject("Scripting.FileSystemObject")
          strFile1= "C:\test\file1.txt"
          strFile2= "C:\test\file2.txt"
          Set objFile1 = objFS.OpenTextFile(strFile1,1)
          Set objFile2 = objFS.OpenTextFile(strFile2,1)
          i=0
          j=0
          Do Until objFile1.AtEndOfStream
          ReDim Preserve Input1_Arr(i)
          Input1_Arr(i) = objFile1.ReadLine
          i = i + 1
          Loop
          Do Until objFile2.AtEndOfStream
          ReDim Preserve Input2_Arr(j)
          Input2_Arr(j) = objFile2.ReadLine
          j = j + 1
          Loop

          For x=0 To j-1
          WScript.Echo "fc " & Input1_Arr(x) & " " & Input2_Arr(x)
                  Wscript.Echo "Call external command fc here"
          Next


          Faded-Maximus

            Topic Starter


            Starter

            Re: Batch file help
            « Reply #4 on: March 18, 2009, 09:22:14 AM »
            Thanks. I apprecriate the reply.

            Perhaps a silly question, but how do you execute the vb script?

            ghostdog74



              Specialist

              Thanked: 27
              Re: Batch file help
              « Reply #5 on: March 18, 2009, 09:26:55 AM »
              save the script as myscript.vbs and on command line
              Code: [Select]
              c:\test> cscript /nologo myscript.vbs
              please check out the documentation link i gave if you want to know more about vbscript.

              Dias de verano

              • Guest
              Re: Batch file help
              « Reply #6 on: March 18, 2009, 10:43:23 AM »
              Here's a pure batch solution, dull, I know, but it is what the OP asked for.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion

              set file1="file1.txt"
              set file2="file2.txt"

              REM count lines
              REM presumes both files have same number of lines
              for /f "tokens=1,* delims=:" %%L in ('findstr /N /R ".*" %file1%') do set /a lines1=%%L

              for /L %%N in (1,1,%lines1%) do (
              set line=%%N
              set linecount=1
              for /f "delims==" %%L in ('type %file1%') do (
              if !linecount! equ !line! set file1line=%%L
              set /a linecount+=1
              )
              set linecount=1
              for /f "delims==" %%L in ('type %file2%') do (
              if !linecount! equ !line! set file2line=%%L
              set /a linecount+=1
              )
              FC "!file1line!" "!file2line!"
              )


              2 files

              Code: [Select]
              file1.txt       file2.txt
              file1-001.def   file2-001.abc 
              file1-002.def   file2-002.abc 
              file1-003.def   file2-003.abc 
              file1-004.def   file2-004.abc 
              file1-005.def   file2-005.abc 
              file1-006.def   file2-006.abc 
              file1-007.def   file2-007.abc 
              file1-008.def   file2-008.abc 
              file1-009.def   file2-009.abc 
              file1-010.def   file2-010.abc 
              file1-011.def   file2-011.abc 
              file1-012.def   file2-012.abc 
              file1-013.def   file2-013.abc 
              file1-014.def   file2-014.abc 
              file1-015.def   file2-015.abc 
              file1-016.def   file2-016.abc 
              file1-017.def   file2-017.abc 
              file1-018.def   file2-018.abc 
              file1-019.def   file2-019.abc 
              file1-020.def   file2-020.abc 
              file1-021.def   file2-021.abc 
              file1-022.def   file2-022.abc 
              file1-023.def   file2-023.abc 
              file1-024.def   file2-024.abc 
              file1-025.def   file2-025.abc 

              Output:

              Code: [Select]
              FC "file1-001.def" "file2-001.abc"
              FC "file1-002.def" "file2-002.abc"
              FC "file1-003.def" "file2-003.abc"
              FC "file1-004.def" "file2-004.abc"
              FC "file1-005.def" "file2-005.abc"
              FC "file1-006.def" "file2-006.abc"
              FC "file1-007.def" "file2-007.abc"
              FC "file1-008.def" "file2-008.abc"
              FC "file1-009.def" "file2-009.abc"
              FC "file1-010.def" "file2-010.abc"
              FC "file1-011.def" "file2-011.abc"
              FC "file1-012.def" "file2-012.abc"
              FC "file1-013.def" "file2-013.abc"
              FC "file1-014.def" "file2-014.abc"
              FC "file1-015.def" "file2-015.abc"
              FC "file1-016.def" "file2-016.abc"
              FC "file1-017.def" "file2-017.abc"
              FC "file1-018.def" "file2-018.abc"
              FC "file1-019.def" "file2-019.abc"
              FC "file1-020.def" "file2-020.abc"
              FC "file1-021.def" "file2-021.abc"
              FC "file1-022.def" "file2-022.abc"
              FC "file1-023.def" "file2-023.abc"
              FC "file1-024.def" "file2-024.abc"
              FC "file1-025.def" "file2-025.abc"
              « Last Edit: March 18, 2009, 11:16:06 AM by Dias de verano »

              Dias de verano

              • Guest
              Re: Batch file help
              « Reply #7 on: March 18, 2009, 11:41:51 AM »
              I got this PM from the OP

              Quote
              Hey thanks alot for the batch code you provided earlier. I tried copying it and running it and I get an error saying:

              Code:

              FC: Insufficient number of file specifications
              FC: Invalid switch
              FC: Insufficient number of file specifications



              Any ideas why I might be getting this?


              Change the line starting "FC" to start "Echo FC" and study the output on the screen & post it here