extract the first line in a text file to use in renaming the file.

  • Thread starter Thread starter dan dungan
  • Start date Start date
D

dan dungan

Hi,

We want to rename 5500 text files that contain G-code for our CNC
machines.

___________________________________________________
In case you're wondering:

The abbreviation CNC stands for computer numerical control, and refers
specifically to a computer "controller" that reads G-code instructions
and drives a machine tool, a powered mechanical device typically used
to fabricate components by the selective removal of material. CNC
_____________________________________________________

We were using an old database running on window 3.1 to find, edit and
send the G-code to the CNC machine.

That database broke and I'm attempting to emulate the process with
Access 2000.

The CNC progran

We want to change the file naming convention

The first line in the text file is the part number.

I want to capture the part number and rename the text file with the
part number.

Does anyone have suggestions?

Thanks,

Dan
 
Ok.
The first line in the text file is the part number.

I want to capture the part number and rename the text file with the
part number.

Does anyone have suggestions?

This should be very close to what you need:

Sub test9999()

Dim colFiles As New Collection
Dim strFile As Variant
Dim intFile As Integer
Dim strPath As String
Dim strNewFilename As String
Dim strBuf As String
intFile = FreeFile
Call FillDir("c:\My doctest\", "*.txt", colFiles)

For Each strFile In colFiles
strPath = Left(strFile, InStrRev(strFile, "\"))
Open strFile For Input As #1
Line Input #intFile, strBuf
Close intFile
strNewFilename = strPath & "MyFilePrefix" & strBuf & ".txt"
Debug.Print "old = " & strFile & " new = " & strNewFilename
Name strFile As strNewFilename
Next

MsgBox "done"

End Sub

And, the routine filldir *will* traverse sub directories

Sub FillDir(startDir As String, strFil As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colfolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir & strFil)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.*", vbDirectory)

Do While strTemp <> ""
If (GetAttr(startDir & strTemp) And vbDirectory) = vbDirectory Then
If (strTemp <> ".") And (strTemp <> "..") Then
colfolders.Add strTemp
End If
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colfolders
Call FillDir(startDir & vFolderName & "\", strFil, dlist)
Next vFolderName

End Sub
 
Hi Albert,

Thanks for your prompt response.

I truly appreciate your taking time to help me.

I've had to delay replying because the CNC manager's new information
and your code revealed new facts, and I don’t understand how the code
works, so I have some questions:

1. The files are not text files.
They are ascii files--they have no extension.

2. The part number (new file name) is not
located on the first line of the file.

3. There is a % symbol on the first line
4. The part number is on the second line surrounded by
parentheses, like this:
%
:1001(00-4040L*1001), or this:

%
(00-901309**0800)

5. So I removed the references to “*.txt” in the lines:

Call FillDir("C:\Documents and Settings\Analyst\My Documents\Work\CNC
\Programs\CNC-FEMCO\Test\", "", colFiles)

And

strNewFilename = strPath & "MyFilePrefix" & strBuf

I don’t know what “MyFilePrefix” is.

Now the program names a file, MyFilePrefix%, and returns the message,
Run time error 5 on this line:

Name strFile As strNewFilename

6. Also, I’m attempting to understand how the code works
I added comments to the variable dimensions to describe the
variable’s purpose. Did I get it right?

Sub test9999()

Dim colFiles As New Collection 'To hold file names
Dim strFile As Variant 'To capture the file name
Dim intFile As Integer 'To count the files to make
sure
we only process
file once
Dim strPath As String 'To capture the path
Dim strNewFilename As String 'To capture the new file
name
Dim strBuf As String 'To capture the file
extension

I don’t understand the line “intFile = FreeFile” works.
_____________________________________________

Sub FillDir(startDir As String, strFil As String, dlist As Collection)
Dim strTemp As String
Dim colfolders As New Collection
Dim vFolderName As Variant

I understand that you’ve created a Sub with arguments, but I don’t
understand how it works.

Where in the code does the file actually get renamed?

I hope you can help clarify some of this for me.

With great respect and thanks,

Dan
 
Hi Albert,

I've finally got a sub that basically works.

To read the read the second line in the file i just added
the line:

Line Input #intFile, strBuf

And I had to do some string manipulation on the part number.

Thanks for your help!

Dan
 
Back
Top