Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions with fultiple files

Status
Not open for further replies.

mistux

MIS
Mar 27, 2003
10
0
0
US
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:

%_N_STAMP__SPF
;$PATH=/_N_WKS_DIR/_N_L83_204322419_WPD
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:

%_N_xxx
;$PATH=/_N_WKS_DIR/_N_yyy

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
Loop
TextStream.Close

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

re.Test(s)
re.Global = True

End Function
 
Hello mistux,

If those are fixed structure, I would prefer replace() method than the regex. As too many uncertainty, I just make a version out of yours. The GetFile function then return a semi-colon delimited string containing the info.
Code:
Option Explicit

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

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

wscript.echo GetFile()
	
Function GetFile()

Dim TextStream, S, File, fso, sPath, sFile, Folder,  i

set fso = CreateObject("Scripting.FileSystemObject")
set Folder=GetFolder(strTempDir)

GetFile=""

For Each File in Folder.files
	set TextStream = FSO.OpenTextFile(File, OpenFIleForReading)
	 i=0
	Do While (Not TextStream.AtEndOfStream) And (i<2)
		S = TextStream.ReadLine
		Select Case i
			Case 0
				sPath=replace(S,"%_N_","")
				wscript.echo sPath
			Case 1
				sFile=replace(S,";$PATH=/_N_WKS_DIR/_N_","")
				wscript.echo sFile
		End Select
		i=i+1
	Loop
	TextStream.Close
	'You can do all sorts of things to the file now as long as sPath and sFile are defined here
	GetFile= GetFile & sPath & "\" & sFile & ";"
next
GetFile=Left(GetFile, len(GetFile)-1)

End Function
You can decide at which points more actions, such as moving files etc, can be done.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top