Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.batI 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

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
33?

@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.

Hi,
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.

No it is always field 33, the spaces contained within the quotes are not counted as it is one field.

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 ) ) )

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"
and
"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.)

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".

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.

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)

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) do var str first ; chex -p "1" $data > $first if ($first == "\"") stex "^\" ^]" $data > null else stex "^ ^]" $data > null endif set $fieldnum = $fieldnum+1 done var str field33 ; stex "]^ ^" $data > $field33 echo -e "DEBUG: Field 33 is " $field33 if ($field33=="\"234\"") do system move "C:\bjobojd0" "C:\A" ; system "C:\A.bat" done endif if ($field33=="\"456\"") do system move "C:\bjobojd0" "C:\B" ; system "C:\B.bat" done endif if ($field33=="\"789\"") do system move "C:\bjobojd0" "C:\C" ; system "C:\C.bat" done endif
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.
Sen

Sadly, VBScript doesn't have the best text parsing tools. It is, however, doable.
Main 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) Loop 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

Many thanks Guy's, I have enough material here to resolve my problem.
This is definitely the best forum on the web.

![]() |
Programming newbie questi...
|
How do I exit from the fo...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |