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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB question to all guru out there....

Status
Not open for further replies.

AnthonyH

MIS
Aug 16, 2001
7
US
To all VB Guru out there - Please give me a hand in writing this code....

Below is a detailed explanation of what we need to accomplished with VB or even PERL....

I need to open, save and close 1200 very short files. They are strictly text files to be used in CNC machines, as job code for cutting metal on lathes.

When we transfer them to the machines, they won't run unless we have already opened them and saved them. We do not want to make any changes at all.

I can open a file in notepad, save it as the same name, and then close it. Then the machine will read it. I just have 1200 to do at a time.

Is there a way to automate this using VB or even PERL
to open the files (1200) close it...(no changes will be made to the files,
just a open and close for the file format specified...the server needs
to see that the file was open inorder to process the jobs....) I could not get the code to do what I want..

---------- Current VB Code --------------

Dim varpart As Variant
Dim strpart As String
Dim strname As String
Dim fso As FileSystemObject
Dim intoutput As Integer
Dim intreal As Integer

Private Sub Command1_Click()

varpart = 54000
intoutput = 1
intreal = intoutput + 1
Do Until (varpart = 55120)
varpart = varpart + 1
strpart = CStr(varpart)

strname = "c:\A" & strpart & ".min"
Open strname For Output As #intreal
Close intreal
Loop

End Sub


---------- End Code -----------------------


Thanks,
Ant
 

Your very close, but on your open statement you are erasing any contents of the file becuse you are opening for output. You should open for input. Also either use the FSO or check out the dir function.

Good Luck

 
Hi There! Thanks for your input. Can you please explain further....maybe fix the mistake or what I'm missing from my code (cut and paste).

I greatly appreciate it!

Thanks,
Ant
 

Ok, give this a try. You will need to pass to this sub the directory of where you want to open and close these files.
[tt]
Private Sub OpenCloseAllFilesInDirectory(DirectoryPathToFiles As String)

On Error GoTo OpenCloseAllFilesInDirectoryError

Dim FName As String, FNumb As Integer

If Right(DirectoryPathToFiles, 1) <> &quot;\&quot; Then DirectoryPathToFiles = DirectoryPathToFiles & &quot;\&quot;

FName = Dir(DirectoryPathToFiles & &quot;*.*&quot;)

Do While FName <> &quot;&quot;
If FName <> &quot;.&quot; And FName <> &quot;..&quot; Then
If (GetAttr(DirectoryPathToFiles & FName) And vbDirectory) <> vbDirectory Then
FNumb = FreeFile
Open DirectoryPathToFiles & FName For Input As #FNumb
Close #FNumb
End If
End If
FName = Dir
DoEvents
Loop

Exit Sub
OpenCloseAllFilesInDirectoryError:

MsgBox Err.Description

End Sub
[/tt]

BTW if you would have looked up the dir function you would have found some code simular to what I just have posted (Thats where I started from).

Good Luck

 
Opening the file for input won't solve your problem, but vb5's code is closer to what you want. So much easier with some details.

In your original post (in this thread), you describe the manual process as 1) open in notepad 2) save file 3) close file. Steps 1 and 3 are taken care of . . . but 2 is the important one.

I can see two possible problems. In a standard DOS ASCII file, lines are terminated with a CR/LF combination. Files are terminated with a EOF marker. I think your files are missing one or both. Your CNC app does not know how to deal with this, either it sees multiple lines in the file as one line because the CR or the LF is missing at the end or it is waiting for the rest of the file because there is no EOF marker. Note that if the files you are using were created on a unix or linux host, the CR is probably missing as those OS's use only a LF at the end of a line.

Notepad is very forgiving about both of those problems. As a file is opened, if the CR or the LF is missing, it assumes both should be there and inserts the missing part in it's internal buffer. If it gets to the end of the file and doesn't find a EOF, it inserts one in it's buffer. When you save the file, you write whatever is in the buffer back to the file, thus fixing the problem. If you simply open the file for read then close it, nothing will happen, except maybe the date stamp will change. One way to check this theory would be to check the file size before you open it in notepad and after you save it from notepad. Should be a couple bytes difference.

If the problem is that these files originated on a unix computer, there are utilities all over the net that do a good job of that conversion. No point in reworking the wheel. If you are missing the EOF, opening the file for append (instead of read) might work, but I'm not sure. Some versions of Basic will not write the EOF unless you actually write something to the file before the close. I suspect that VB 5 or 6 may take care of that, but I don't have time to test at the moment. Maybe someone else already knows.

Failing that, you will have to open the original files for input, open another file in a different directory with the same filename for output, use line input to retrieve the data from the original file, replace any LF's with CR/LF, write the data to the new file, then close both files.

Having said that, vb5prgrmr has a real good structure for the loop to run the whole thing, the code inside would just have to change.

And don't forget, I have been wrong before!
 

mhkwood,

You make some good points that I did not consider so with that in mind...
[tt]
Private Sub OpenCloseAllFilesInDirectory(DirectoryPathToFiles As String)

On Error GoTo OpenCloseAllFilesInDirectoryError

Dim FName As String, FNumb As Integer, S As String

If Right(DirectoryPathToFiles, 1) <> &quot;\&quot; Then DirectoryPathToFiles = DirectoryPathToFiles & &quot;\&quot;

FName = Dir(DirectoryPathToFiles & &quot;*.*&quot;)

Do While FName <> &quot;&quot;
If FName <> &quot;.&quot; And FName <> &quot;..&quot; Then
If (GetAttr(DirectoryPathToFiles & FName) And vbDirectory) <> vbDirectory Then
FNumb = FreeFile
Open DirectoryPathToFiles & FName For Binary As #FNumb
S = Input(FileLen(DirectoryPathToFiles & FName), #FNumb)
Close #FNumb
S = Replace(S, vbLf, vbNewLine)
SetAttr DirectoryPathToFiles & FName, vbNormal
FNumb = FreeFile
Open DirectoryPathToFiles & FName For Output As #FNumb
Print #FNumb, S
Close #FNumb

End If
End If
FName = Dir
DoEvents
Loop

Exit Sub
OpenCloseAllFilesInDirectoryError:

MsgBox Err.Description

End Sub
[/tt]
I think that should be a little closer but please be mindful that I have not debugged this code so you may have to do some adjustments.

Good Luck



 
I think that what you need to do is only change the file Time/Date stamp, in order that CNC machine recognizes as a new one!

Carlos Paiva
 
If so, opening the file not for
Code:
input
or
Code:
output
, but for
Code:
append
(and then closing them immediately) might help.

best regards
 
Actually, you can open a file with the optional &quot;Access&quot; argument set to &quot;Read Write&quot; and save it in any &quot;Mode&quot; (at least re the docs). Certainly Append, Binary and Random support this, but I cannot say I have ever tried with Input or OutPut modes.

It is 'curious' that the process doesn't recognize the file &quot;unless&quot; it has been &quot;oepned and saved&quot;, as this implies the process checks some aspect of the file prior to the use. If the check is more than the filesystem date/time stamp, I would be suprised -so the modification of the date stamp in the file system should be sufficient - although not necessarily any faster than the open/save/close procedure(s) outlined above (with suitable modification to include both read and write in the access arg).

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
If the date/time stamp are all that has to be changed, there is no need for VB or notepad.

Put all the files in the same directory, without any other files there.

Open a command window. type COPY C:\THEDIR\*.* +,, and press enter.

Done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top