Windows Batch Script Help

Discussion in 'Software' started by jayzscousa, Dec 10, 2012.

  1. jayzscousa

    jayzscousa Private E-2

    Hi there,

    I need help with the following script please.

    set master="c:\master\"
    set compare="c:\compare\"
    set results="c:\results\"

    for /f %%a IN ("dir %master% /b %filter%) do (SET pfile=%%~da
    fc /N %master%%pfile% %compare%%pfile% > %results%%pfile%
    echo "Comparing %pfile%")


    When I run this I get an error

    > was unexpected at this time.


    Is it possible to run multiple commands in this way?

    The script is intended to loop through the files in %master% and compare them to the contents of %compare%. Then output the differences to %results%.


    Does anybody see anything obvious wrong with it?

    Cheers,

    James
     
  2. mjnc

    mjnc MajorGeek

    I'm not very good at that, but the first thing I saw was that %filter% is not defined.

    Also,
    Code:
    (SET pfile=%%~da
    has no closing parentheses.

    The loop will just continue until there is nothing left to SET, so when the execution finally falls through to the
    Code:
    fc
    line, %pfile% is undefined.

    I think you need something like a goto after the SET pfile.

    :start
    (set pfile) goto filecomp
    :filecomp
    if "%pfile%" == "" goto end
    fc /N
    goto start
    :end
     
    Last edited: Dec 11, 2012
  3. jayzscousa

    jayzscousa Private E-2

    Thanks for the help. It's ok, I realised I could just call the FC command from a VBScript which I'm more familiar with.

    This script will compare two directories and output the differences to a third directory.

    ' Script will look at the files in the master directory and compare them to the files in the compare directory
    ' each file will have a results file created in the results directory.
    ' This file can be used to identify what differences exist.
    ' The program creates a summary report of the differences in the file
    ' 001_Results.txt

    on error resume next

    dim fso
    dim fsox
    dim fsoy
    dim wsh
    dim oexec
    dim mfolder
    dim mfile
    dim rfile
    dim sfile
    dim master
    dim compare
    dim results
    dim txt
    dim flagdiff

    commondir = "SrcFiles\"

    master="C:\UAT Files\Informatica\" & commondir

    compare="C:\Development Files\Informatica\" & commondir

    results="C:\Results\Informatica\" & commondir

    set fso = CreateObject("Scripting.FileSystemObject")
    set fsox = CreateObject("Scripting.FileSystemObject")
    set fsoy = CreateObject("Scripting.FileSystemObject")

    set mfolder = fso.GetFolder(master)
    set sfile = fso_OpenTextFile(results & "001_Results.txt", 2, TRUE)

    set wsh = CreateObject("WScript.Shell")

    for each mfile in mfolder.Files
    set oexec = wsh.Exec("fc """ & master & mfile.name & """ """ & compare & mfile.name & """")

    set rfile = fsox.OpenTextFile(results & mfile.name & ".compare.txt", 2, TRUE)

    flagdiff = 0

    if Err.Number = 0 then

    while not oexec.StdOut.AtEndOfStream
    txt = oexec.StdOut.readline
    if txt = "FC: no differences encountered" then
    flagdiff=1
    end if
    rfile.WriteLine txt
    wend

    if flagdiff=0 then
    sfile.WriteLine mfile.name
    end if

    rfile.close
    else
    Err.Clear
    end if


    next

    sfile.close

    set wsh=nothing
    set rfile =nothing
    set mfile=nothing
    set oexec=nothing
    set mfolder=nothing
    set fso=nothing
    set fsox=nothing
    set fsoy=nothing
    set sfile=nothing
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds