Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 8th, 2004, 09:36 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular Expression when reading files

I am trying to process a directory of files that need to be renamed and relocated.

I will have in a temp directory several files that will all have the info needed to rename and move them in the first couple of lines. I used a regular expression to determin the file name and directory but not sure how use it on each file and how to loop through all the files in the directory.

Each file will look something like this:

N5 G0 X-20 Y36
N10 G0 Z10
N15 G1 Z-10
N20 G0 Z250
N25 M17

Each file starts with the following 2 lines:


The xxx is the file name, the yyy is the directory it goes in.

My Pattern = "%_N_([0-9A-Za-z_]+)\s*;\$PATH=/_N_WKS_DIR/_N_([0-9A-Za-z_/]+)"

Which should give me the file name as \1 and the directory it goes in as \2.

1. My first issue is how to loop thru the files and open each one.
2. My second issue is how to apply the reg ex pattern to each file. Not sure if I need to go line by line or if I should pass the entire file to the expression.
3. My third is how to retrieve the results of the reg ex, the \1 and \2
4. My fourth issue is how to then save the file out using the \1 and \2 returned by the reg ex.

I think that is all.

I am doing it in VB Script since the application that I was using that creates all the files would only allow me to use my reg ex (within the app itself) on the first match it found. I am now just using the app to break appart all the individual files from one big file, which it does well (its a special serial communication program). I could run this script on the big file itself but thought that would be even more difficult, so I let the app break apart each file and then I am trying to process what it creates so I can name them and put them in the correct directory.

Here is what I have so far, but it is not much.

Option Explicit

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
' Constants for opening files
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
Const OpenFileForReading = 1
Const OpenFileForWriting = 2
Const OpenFileForAppending = 8

Const strTempDir="C:\My Documents\VB_Temp_Holding\"

GetFile 'Call function

Function GetFile()

   Dim TextStream
   Dim S
   Dim File
   dim fso
dim TabStop
dim NewLine

TabStop = Chr(9)
NewLine = Chr(10)

Dim inputstr, re, amt
Set re = new regexp 'Create the RegExp object

re.Pattern = "%_N_([0-9A-Za-z_]+)\s*;\$PATH=/_N_WKS_DIR/_N_([0-9A-Za-z_/]+)"
re.IgnoreCase = true


'Create a FileSystemObject
set fso = CreateObject("Scripting.FileSystemObject")

      Set TextStream = FSO.OpenTextFile(strTempDir & "test.txt", OpenFileForReading)
   Set File = FSO.GetFile(strTempDir & "test.txt")
   Set TextStream = File.OpenAsTextStream(OpenFileForReading)
   Do While Not TextStream.AtEndOfStream
      S = S & TextStream.ReadLine & NewLine

   GetFile= S
'msgbox "The file says: " & s

re.Global = True

End Function

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression balajinrs Visual Studio 2005 0 July 24th, 2008 02:11 AM
Regular Expression Help pallone Javascript 2 October 19th, 2007 03:59 PM
What will be the regular expression for following? [email protected] ASP.NET 1.0 and 1.1 Basics 2 December 26th, 2006 06:49 AM
Regular Expression Help anshul PHP How-To 4 December 8th, 2004 05:19 AM
Regular expression help!!! alex_read Javascript 4 August 18th, 2004 03:44 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.