Computing.Net > Forums > Programming > Batch Script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Batch Script

Reply to Message Icon

Name: Advent85
Date: July 16, 2009 at 00:50:34 Pacific
OS: Windows XP
CPU/Ram: 2.793 GHz / 3574 MB
Product: Microsoft Microsoft windows server 2003 administrator s companion
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: July 16, 2009 at 02:08:29 Pacific
Reply:

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?


0

Response Number 2
Name: Wahine
Date: July 16, 2009 at 02:31:40 Pacific
Reply:

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


0

Response Number 3
Name: Advent85
Date: July 16, 2009 at 02:44:11 Pacific
Reply:

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.


0

Response Number 4
Name: Advent85
Date: July 16, 2009 at 02:57:45 Pacific
Reply:

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


0

Response Number 5
Name: Advent85
Date: July 16, 2009 at 03:00:22 Pacific
Reply:

If it helps it will always be a three figure number contained within quotes.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: July 16, 2009 at 05:45:35 Pacific
Reply:

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


0

Response Number 7
Name: klint
Date: July 16, 2009 at 05:47:26 Pacific
Reply:

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


0

Response Number 8
Name: Advent85
Date: July 16, 2009 at 05:53:31 Pacific
Reply:

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


0

Response Number 9
Name: klint
Date: July 16, 2009 at 06:17:58 Pacific
Reply:

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.


0

Response Number 10
Name: klint
Date: July 16, 2009 at 06:30:43 Pacific
Reply:

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)


0

Response Number 11
Name: Advent85
Date: July 16, 2009 at 06:34:26 Pacific
Reply:

Thank you Klint,
I will try it, it at least gives me a good start.


0

Response Number 12
Name: Sen Hu
Date: July 30, 2009 at 09:19:41 Pacific
Reply:


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


0

Response Number 13
Name: Razor2.3
Date: July 30, 2009 at 11:10:05 Pacific
Reply:

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


0

Response Number 14
Name: Advent85
Date: July 30, 2009 at 23:51:46 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Programming newbie questi... How do I exit from the fo...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Script

batch script to parse filenames www.computing.net/answers/programming/batch-script-to-parse-filenames/15286.html

Batch Script Help Needed www.computing.net/answers/programming/batch-script-help-needed/14475.html

batch script www.computing.net/answers/programming/batch-script/14086.html