-
April 19th, 2013, 04:47 PM
#1
Kill file issue
Hey, Second post...same program...Currently trying to code a program that will be used in a school style situation. What im stuck with is deleting a record from the document. It should copy them all, then kill the first file before renamign the second. doing all the steps one by one all work...however when i try to do them all as one it doesnt work...it gets to the step of trying to rename the files before going wrong?
The file uploaded on here wont work, so i've compressed the files and hosted it here, http://www.filehostfree.com/?d=5171BA881
Please, any help would be great. Stuck on this for days now
Cheers!
-
April 19th, 2013, 04:57 PM
#2
Re: Kill file issue
Zip and attach your files to a post here. Most people doesn't like to view files from other locations.
-
April 19th, 2013, 07:40 PM
#3
Re: Kill file issue
Explain this: " deleting a record from the document. It should copy them all, then kill the first file before renamign the second."
-
April 20th, 2013, 01:55 AM
#4
Re: Kill file issue
Should be no need to send big files. Most likely the code in question would only be a few lines, I doubt that it would be more than 100. The rest of the project is not needed, just the part that does the copy, rename, kill or whatever it is doing. Hard to tell from the OP. Talks about deleting a record, copying them all, killing files and renaming files. could be that it is a timing issue you face where you are trying to rename or kill a file that is still in use.
Always use [code][/code] tags when posting code.
-
April 20th, 2013, 02:27 AM
#5
Re: Kill file issue
Hi, thank you for the replies so far. I'm currently out, so will try uploading the files again later. Also I will
Copy the code aswell. The program stores information on different pieces of equipment in a text file. What I'm trying to do, is delete one of the records from the file. I've attempted to code this by opening the orginal document and a new one side by side. Then, it copy's one record at a time from the first document to the second, excluding the one I wish to delete. Finally, doucment 1 is closed and deleted before document 2 is renamed what document 1 was to allow the program to continue running. Sorry for all the confusion, never have been good getting my words out...hence a love for ICT. As noted, ill try the upload again aswell as the code.
Thanks as ever,
jordie
-
April 21st, 2013, 08:36 AM
#6
Re: Kill file issue
Hey, heres the code...
Code:
Private Sub cmddelete_Click()
Dim File As String
Dim index As Integer
Dim NumberOfRecords As Integer
Dim recordnumbertodelete As Integer
Dim newfilename As String
Dim product As OneProduct
If lstdisplayfile.Text <> "" Then
recordnumbertodelete = lstdisplayfile.ListIndex + 1
newfilename = App.Path & "\newproducts.text"
Open Filename For Random As #1 Len = Len(product)
Open newfilename For Random As #2 Len = Len(product)
NumberOfRecords = LOF(1) / Len(product)
For index = 1 To NumberOfRecords
Get #1, , product
If index <> recordnumbetodelete Then
Put #2, , product
End If
Next index
Close #2
Close #1
Call killfile
'File = "E:\School work\Year 13\ICT\visual basic\Exam\products.text"
'Kill File
'Kill "E:\School work\Year 13\ICT\visual basic\Exam\products.text"
'Kill App.Path & "\products.text"
'Name "newproducts.text" As "products.text"
'Msgbox (Filename)
'MsgBox (newfilename)
'Call lstdisplayfile1
'Else
'MsgBox ("You must select a product first")
End If
End Sub
-
April 21st, 2013, 12:12 PM
#7
Re: Kill file issue
As far as KILLING the first file, I'd create a simple 3 line BATCH program (or command) that deletes the file AFTER the program finishes. Or better yet, rename it BEFORE you open it, then save it with the original name again.
Here's a better way to split the file into lines, or sections (depending on the identifier used)
Code:
Option Explicit
Private Sub Form_Load()
Dim x As Integer, st As String, y As Integer
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
On Error GoTo trapit
Open App.Path & "\to do3.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
Dim words() As String
For x = 0 To UBound(str)
words = Split(str(x)) ' Split into WORDS
For y = 0 To UBound(words)
st = st & str(x) & vbCrLf & vbCrLf ' one line for each word
Next y
Next x
MsgBox st
Exit Sub
trapit:
If Err.Number = 53 Then
MsgBox ("Didn't work")
End If
End Sub
This splits into lines, they sections between the' ,' fields
-
April 21st, 2013, 04:38 PM
#8
Re: Kill file issue
Hey David, thanks for the reply. However, Im slightly confused on the three line batch program? and surely, if i rename the file before i open it...when it goes to open it...it wont be able to find it?
Cheers
-
April 21st, 2013, 01:53 PM
#9
Re: Kill file issue
Mình r?t Ã*t khi d?c hay comment bÃ*i vi?t trên forum. Nhung bÃ*i vi?t c?a b?n th?c s? hay vÃ* mình dã ph?i d?c chúng.
C?m on nh?ng chia s? r?t thú v? c?a b?n.
Trân tr?ng.
I rarely read or comment on the forum post. But your post is really good and I had to read them.
Thanks for sharing your very interesting.
Best regards.
-
April 21st, 2013, 06:09 PM
#10
Re: Kill file issue
Make 2 programs. One with a textbox forr input (original name) which renames it to TEMP.* or whatever. Your app could open THAT one
-
April 24th, 2013, 12:37 PM
#11
Re: Kill file issue
Hi David, I finally have the product deleting the old file and renaming the new! However, now when i click delete no records are removed...instead all the records are moved without the orginal one being deleted? Below is the new and improved code, as always...any help is great!
Code:
Private Sub cmddelete_Click()
Dim File As String
Dim index As Integer
Dim NumberOfRecords As Integer
Dim recordnumbertodelete As Integer
Dim newfilename As String
Dim newnewfilename As String
Dim product As OneProduct
If lstdisplayfile.Text <> "" Then
recordnumbertodelete = lstdisplayfile.ListIndex + 1
MsgBox (recordnumbertodelete)
Filename = App.Path & "\products.Text"
newfilename = App.Path & "\newproducts.text"
Open Filename For Random As #1 Len = Len(product)
Open newfilename For Random As #2 Len = Len(product)
NumberOfRecords = LOF(1) / Len(product)
For index = 1 To NumberOfRecords
Get #1, , product
If index <> recordnumbetodelete Then
Put #2, , product
End If
Next index
Close #2
Close #1
Call killfile
Name "newproducts.Text" As "products.Text"
'File = "E:\School work\Year 13\ICT\visual basic\Exam\products.text"
'Kill File
'Kill "E:\School work\Year 13\ICT\visual basic\Exam\products.text"
'Kill App.Path & "\products.text"
'Name "newproducts.text" As "products.text"
'Msgbox (Filename)
'MsgBox (newfilename)
'Call lstdisplayfile1
'Else
'MsgBox ("You must select a product first")
End If
End Sub
-
April 24th, 2013, 12:58 PM
#12
Re: Kill file issue
You have a couple of issues there.
There is no need to use the Call keyword. I think the last time that was needed was about 20 years ago, can't even remember the last time I used it in a program, maybe in VBDos?
There is also no point in using a sub rountine to kill a file especially if you are not passing a filename to it as it serves no purpose beyond that which could be done with a single line of code which would be faster and easier to read and debug.
Code:
Name "newproducts.Text" As "products.Text"
You are not including a path to either file here so you may get unexpected results depending on what the current directory is at the time the code executes.
You have also defined and set two variables earlier in the code for these path\filenames and this is what you should be using if for no reason other than it makes the code easier to follow and easier to modify if needed.
You should get rid of all that commented code, just makes it confusing.
Also the standard extension for a text file is .txt rather than .text
Code:
Close #2
Close #1
Kill Filename
Name newfilename as FileName
If you get an error on the kill or rename function it could be because the file has not been released when the code executes. I don;t recall ever having such an issue but if that is the case you may want to try a short sleep statement in there to delay the renaming of the file. I don't think it should be needed though.
Last edited by DataMiser; April 24th, 2013 at 01:00 PM.
Always use [code][/code] tags when posting code.
-
April 24th, 2013, 01:41 PM
#13
Re: Kill file issue
Code:
Get #1, , product
If index <> recordnumbeRtodelete Then
Put #2, , product
End If
Success! I miss-spelt the word record...However...it may just be me being dumb here...once the record is delete it replaces it with a 0...this 0 will only be removed by ending and restarting the program? Even calling the list box to redisplay doesnt shift it...any ideas?
Thanks!
Last edited by jordiebarrett; April 24th, 2013 at 01:45 PM.
-
April 24th, 2013, 01:38 PM
#14
Re: Kill file issue
Hi, again thanks for the reply...ive done all the steps you said about cleaning the code up and the .txt
However, i still cant get it to delete the record i want it to when pressing the cmd button for the delete. Instead, all the records (including the one i want to delete) is moved
-
April 24th, 2013, 03:41 PM
#15
Re: Kill file issue
Look at the code in Post #7. Never use a file number (as it may already be in use). Calculate it, like I did. Also, write the file at the END rather than each line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|