Batch Script

Microsoft Microsoft windows server 2003...
July 16, 2009 at 00:50:34
Specs: Windows XP, 2.793 GHz / 3574 MB
Hi Guys,
I am in need of your help again.

I have an interface file called bjobojd0 (it will always have the same file name).

I need a bat script that will look in the file for a specific field and depending on what the number is in that field call other bat files.

Let me explain, the contents of the file is one line as follows:

1119 "0163500180" "HSG" "387855" 0 "S&F Manual Limit Stat" "" "" "" " " 0 "V" "" "YYYYYN" "YYYYYN" ? "00:00" 0 "OR" "OIL REPAIRS" "OR" 0 "" ? "00:00" "JAMES BROOKS" 15/07/2009 "08:21" 15/07/2009 "08:21" 15/07/2009 "09:21" "234" "ROLE & BARKER (R)" 238 "OIL REPAIRS" "" "MRS P SMITH||15||" "CLAXTON CLOSE|CASTLE CLOSE|CAMBERWELL|CB28 9TW" "" "" ""

The file is space delimited and the field I am looking at is always field number 33, in the example above it is "234".

What I need the bat file to do is:

If field 33 = "234" then move file to folder "A" and CALL A.bat
If field 33 = "456" then move file to folder "B" and CALL B.bat
If field 33 = "789" then move file to folder "C" and CALL C.bat

I hope this makes sense and I have explained it O.K., I suppose what I am asking for is a router for this file.

Many thanks in advance

See More: Batch Script

Report •

July 16, 2009 at 02:08:29
This is very difficult to parse with a batch file. Can it be in some
other language? If not, can the input file be formatted differently,
having a special unique character in each line just before field

Report •

July 16, 2009 at 02:31:40
@Advent85 = The file is space delimited and the field I am looking at is always field number 33, in the example above it is "234".

In this space delimited file "234" is round about field 50.

I agree with Klint, very difficult to deal with as special chars are used in the file.

Report •

July 16, 2009 at 02:44:11
Unfortunately not, it is an interface file and must remain in this format to be imported into the other system.

As to another language I am open to suggestions but it must run on a Windows server without using additional software.

Report •

Related Solutions

July 16, 2009 at 02:57:45
No it is always field 33, the spaces contained within the quotes are not counted as it is one field.

Report •

July 16, 2009 at 03:00:22
If it helps it will always be a three figure number contained within quotes.

Report •

July 16, 2009 at 05:45:35
This would break under all sorts of conditions, it depends on a consistent number of quotes and the date fields (to many tokens, over 30, cmd doesn't seem to like it) and their delimiter.

setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("textdoc.txt") do (
	set line=%%a
	set line=!line:a=!
	set line=!line:"=a!
	set line=!line:aa=aba!
	for /f "tokens=5* delims=/" %%b in ("!line!") do (
		for /f "tokens=4 delims=a" %%d in ("%%c") do (
			echo %%d

Report •

July 16, 2009 at 05:47:26
To run it without the requirement for any additional software,
you can write the script using VBScript. This is included by
default on Windows systems. Hopefully, one of the VBScript
experts will come along soon.

One problem with using quotes to enclose some individual
fields (which may contain spaces) is how you code quotes
that are part of the field. For example, some systems use a
special character, e.g.

"This quote: \" is part of this field"


"This field contains the single \\ special character."

Other systems use double quotes:

"This quote: "" is part of this field."

Whoever writes your VBScript will need to know the full syntax rules in order to code it correctly.

(P.S. your sample data is wrong: Camberwell is in South London, postcode SE**, not CB28. My aunt used to live there.)

Report •

July 16, 2009 at 05:53:31
Hi Klint,
Many thanks for the reply, I can see what you mean.

As ever the things that look simple in theory never are.

Would it be possible to search the line for the text string "234".

Report •

July 16, 2009 at 06:17:58
It would be possible to search for the string "234" that may
appear anywhere on the line, but that wouldn't make it a robust
application as it would match other fields, e.g. if the street
address field was also "234". It shouldn't be too difficult to write a
VBScript to look at field 33 specifically but I would leave that to
someone who knows VBScript well. If you are not sure about
how internal quotes are escaped, you can provisionally have a
VBScript that works as long as your data doesn't contain any
fields that contain quotes.

Report •

July 16, 2009 at 06:30:43
Here's a batch file that works by searching for "234" etc
anywhere on the line, not just field 33. Not recommended due
to reasons stated in my earlier post. You can use it until
someone comes up with a better solution.

find """234""" bjobojd0 && (move bjobojd0 A\ && call A.bat & goto :next)
find """456""" bjobojd0 && (move bjobojd0 B\ && call B.bat & goto :next)
find """789""" bjobojd0 && (move bjobojd0 C\ && call C.bat & goto :next)

Report •

July 16, 2009 at 06:34:26
Thank you Klint,
I will try it, it at least gives me a good start.

Report •

July 30, 2009 at 09:19:41

Klint is right- this will be difficult to do in batch alone.

Here is my attempt. We will successively extract next field. If that begins with double quote, the field is till the closing double quote. If it does not begin with double quote, the field is until the following space.

# Script Field33.bat
var str data ; cat "C:\bjobojd0" > $data
var int fieldnum
while ($fieldnum <= 32)
    var str first ; chex -p "1" $data > $first
    if ($first == "\"")
        stex "^\" ^]" $data > null
        stex "^ ^]" $data > null
    set $fieldnum = $fieldnum+1
var str field33 ; stex "]^ ^" $data > $field33
echo -e "DEBUG: Field 33 is " $field33

if ($field33=="\"234\"")
    system move "C:\bjobojd0" "C:\A" ; system "C:\A.bat"

if ($field33=="\"456\"")
    system move "C:\bjobojd0" "C:\B" ; system "C:\B.bat"

if ($field33=="\"789\"")
    system move "C:\bjobojd0" "C:\C" ; system "C:\C.bat"

Script is in biterscripting. Think of it as a preprocessor to batch commands. (It really isn't, but it helps to think that way.) Save the script as "C:\Field33.bat" . Start the command prompt. Enter the following command.

"C:\biterScripting\biterScripting.exe" "C:\Field33.bat"

I have tested the script.


Report •

July 30, 2009 at 11:10:05
Sadly, VBScript doesn't have the best text parsing tools. It is, however, doable.
Function Main
  Dim line, tokens, fso, shell
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set shell = CreateObject("WScript.Shell")
  With fso.OpenTextFile(WScript.Arguments(0))
    tokens = Parse(.ReadLine)
  End With
  Select Case tokens(32)
    Case """234"""
      fso.MoveFile WScript.Arguments(0), "A\"
      shell.Run "cmd /c A.bat"
    Case """456"""
      fso.MoveFile WScript.Arguments(0), "B\"
      shell.Run "cmd /c B.bat"
    Case """789"""
      fso.MoveFile WScript.Arguments(0), "C\"
      shell.Run "cmd /c C.bat"
    Case Else
  End Select
End Function

Function Parse(str)
  Dim r(), tokens, rCnt, maxToken, t
  tokens = Split(str)
  ReDim r(UBound(tokens))
  maxToken = Ubound(tokens)
  For t = 0 To maxToken
    r(rCnt) = tokens(t)
    If Left(tokens(t), 1) = """" Then
      Do Until Right(tokens(t), 1) = """" Or t = maxToken
        t = t + 1
        r(rCnt) = r(rCnt) & " " & tokens(t)
      If tokens(t) = """" Then
        t = t + 1
        r(rCnt) = r(rCnt) & " " & tokens(t)
      End If
    End If
    rCnt = rCnt + 1
  Next 't
  ReDim Preserve r(rCnt  - 1)
  Parse = r
End Function

Report •

July 30, 2009 at 23:51:46
Many thanks Guy's, I have enough material here to resolve my problem.
This is definitely the best forum on the web.

Report •

Ask Question