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