konijn
Junior Member
Posts: 96
|
Post by konijn on Oct 30, 2021 14:18:28 GMT -5
Hello,
If I do a file with line input. And some lines have as example this code "VN990004" in the line. Now I want delete this lines. I have to search in de string? Is that possible?
Thanks.
Jack
|
|
|
Post by Walt Decker on Oct 30, 2021 14:36:57 GMT -5
Are you saving the lines in an array or just scanning the lines with INSTR() as the lines are read from the file?
Do you want to delete the line containing the code in the file?
|
|
|
Post by tenochtitlanuk on Oct 31, 2021 11:35:21 GMT -5
This may help you a bit.I create a file of random lines or rows with different version numbers in the format you show embedded in some random text. Code reads this file and creates a new, shortened file. You might want instead to over write the old file- but be careful not to overwrite wanted data!
' Create a file with dummy data in format 'asdfghjkl VN9900xx zxcxvbnm'. open "file.txt" for output as #fOut for i =1 to 100 s1$ =rndChrs$( 10) s2$ =" VN9900"
t$ =str$( int( 25 *rnd( 1))) s3$ =right$( "00" +t$, 2)
s4$ =" " s5$ =rndChrs$( 30)
#fOut s1$ +s2$ +s3$ +s4$ +s5$ next i close #fOut
' Read the file. If 'VN990004' present in a line, skip- otherwise write to file count =0
open "newFile.txt" for output as #fOut2
open "file.txt" for input as #fIn do line input #fIn, row$ 'print row$ if instr( row$, "VN990004") =0 then #fOut2 row$ print "."; ' showing progress.. else count =count +1 print "!"; end if loop until row$ ="" or eof( #fIn) <0 close #fIn
close #fOut2
print "" print " I removed "; count; " rows of data referring to 'VN990004'"
end ' _________________________________________________
function rndChrs$( N) ' generate random alpha content for i =1 to N rndChrs$ =rndChrs$ +chr$( 65 +int( 26 *rnd( 1))) next i 'rndChrs$ ="asdfghjkl" end function
|
|
|
Post by xxgeek on Oct 31, 2021 18:33:13 GMT -5
Another method you may wish to try is jumping the line in your code, and saving a backup before deleting or overwriting. Just in case.....
The code below is working from the DefaultDir$, you may wish to change that.
edit - I reread your post and saw you want to take out a portion of the line eg:"VN990004" Walts answer makes more sense, but you may wish to use the code below as an example, and back up too. If you want to remove the entire line with "VN990004" in the line the code below will work once edited to suit using instr$().
open fileName$ for input as #1 tempfile$ = "tempfile" open tempfile$ for output as #2 lineToDelete$ = "WhatEver is on the line you wish to remove from the file" 'represents the entire lie, not a portion while eof(#1) = 0 line input #1, line$ '- better use instr() here if you want to find a "word" in the line and delete the line if line$ = lineToDelete$ then [dontSave] ' edit with 'if instr(lineToDelete$,"VN990004") = 0 then [dontSave] #2, line$ [dontSave] wend close #1 close #2 deleteMe$ = fileName$;".bak" if fileExists(DefaultDir$, fileName$;".orig") = 0 then if fileExists(DefaultDir$, deleteMe$) <> 0 then kill deleteMe$ end if name deleteMe$ as fileName$;".orig" name fileName$ as fileName$;".bak" name tempfile$ as fileName$ else name fileName$ as fileName$;".bak" name tempfile$ as fileName$ end if wait And don't forget to add the fileExists() function
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Nov 1, 2021 3:15:23 GMT -5
Thanks all for the help. Yes instr function was where I looking for was I used the code from tenochtitlanuk for test: open "newFile.txt" for output as #fOut2
open "c:\alpro\in.new" for input as #fIn do line input #fIn, row$ 'print row$ if instr( row$, "VN990004") =0 then #fOut2 row$ print "."; ' showing progress.. else count =count +1 print "!"; end if loop until row$ ="" or eof( #fIn) <0 close #fIn
close #fOut2
print "" print " I removed "; count; " rows of data referring to 'VN990004'"
end ' _________________________________________________
But I was not complete with my question ,its more complicated (for me At the and off the "VN990004" code is a number (cownumber) in 6 digits, for example "VN990004 10" (means cow nr 10) In an other file I have a list with (cow)numbers like: 1 2 3 4 5 6 7
If the number in de code is the same as in de list (file) the line can be delete. If the number is not existing in the list(file) than the line can stay.
|
|
|
Post by tenochtitlanuk on Nov 1, 2021 6:33:57 GMT -5
How about this? For testing, I skip the file-generation code after first run so the starting file is always the same, and I have a file 'cowNumber.fil' with the acceptable cow numbers. I open/check/close this, but you could open at start of code and close only on clean exit. goto [fileCreatedAlready]
' Create a file with dummy data in format 'asdfghjkl VN9900xx zxcxvbnm'. open "file.txt" for output as #fOut for i =1 to 20 s1$ =rndChrs$( 10) s2$ =" VN990004"
s4$ =right$( " " +str$( int( 1 +9 *rnd( 1))), 6) s5$ =" " +rndChrs$( 30)
#fOut s1$ +s2$ +s3$ +s4$ +s5$ next i close #fOut
[fileCreatedAlready] ' Read the file. If 'VN990004' present in a line, skip- otherwise write to file count =0
open "newFile.txt" for output as #fOut2
open "file.txt" for input as #fIn do line input #fIn, row$ 'print row$ I =instr( row$, "VN990004") if I <>0 then cowNumber =val( mid$( row$, I +9, 6))
if I <>0 and found( cowNumber) then #fOut2 row$ print "."; ' showing progress.. else count =count +1 print "!"; end if loop until row$ ="" or eof( #fIn) <0 close #fIn
close #fOut2
print "" print " I removed "; count; " rows of data referring to 'VN990004' but cow number not in 'cowNumber.fil'."
end ' _________________________________________________
function found( cN) open "cowNumber.fil" for input as #fIn2 cN$ =input$( #fIn2, lof( #fIn2)) close #fIn2 if instr( cN$, str$( cN)) then found =1 else found =0 end function
function rndChrs$( N) ' generate random alpha content for i =1 to N rndChrs$ =rndChrs$ +chr$( 65 +int( 26 *rnd( 1))) next i 'rndChrs$ ="asdfghjkl" end function
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Nov 1, 2021 12:19:51 GMT -5
I will put the original files. Test not succesfull yet.. The orginal file. This file has to write fully, but with replacing or delete the "VN990004" code
DH9900010090000624000900008080 VH990001???????????????????????????????? DN99000400312007060 VN990004 509 VN990004 441 VN990004 440 VN990004 439 VN990004 437 VN990004 436 VN990004 435 VN990004 434 VN990004 433 VN990004 432 VN990004 431 VN990004 430 VN990004 429 VN990004 428 VN990004 427 VN990004 426 VN990004 425 VN990004 390 VN990004 389 VN990004 387 VN990004 384 VN990004 383 VN990004 382 VN990004 381 VN990004 380 VN990004 379 VN990004 377 VN990004 376 VN990004 375 VN990004 374 VN990004 373 VN990004 371 VN990004 370 VN990004 369 VN990004 368 DN3900120030000106000300047010003000480100030004901000300050010003000560100030005701000300058010003000590100030006502000300066020003000680100030006901000300070010003000710100030007702000300078020003000800100030008101000300082010003000830100030008902000300090020 VN390012 300000000 0 00000 0 00000 0 0 VN390012 400000000 0 00000 0 00000 0 0 VN390012 500000000 0 00000 0 00000 0 0 VN390012 600000000 0 00000 0 00000 0 0 VN390012 700000000 0 00000 0 00000 0 0 VN390012 800000000 0 00000 0 00000 0 0 VN390012 900000000 0 00000 0 00000 0 0 VN390012 1000000000 0 00000 0 00000 0 0 VN390012 1100000000 0 00000 0 00000 0 0 VN390012 1200001111 0241111 0440000 0 0 VN390012 1300000000 0 00000 0 00000 0 0 VN390012 1400000000 0 00000 0 00000 0 0 VN390012 1500000000 0 00000 0 00000 0 0 VN390012 1600000000 0 00000 0 00000 0 0 VN390012 1700000000 0 00000 0 00000 0 0 VN390012 1800000000 0 00000 0 00000 0 0 VN390012 1900000000 0 00000 0 00000 0 0 VN390012 2000000000 0 00000 0 00000 0 0 VN390012 2100001111 0241111 0400000 0 0 VN390012 2200000000 0 00000 0 00000 0 0 VN390012 2300000000 0 00000 0 00000 0 0 VN390012 2400001111 0241111 0220000 0 0 VN390012 2500000000 0 00000 0 00000 0 0 VN390012 2600000000 0 00000 0 00000 0 0 VN390012 2700000000 0 00000 0 00000 0 0 VN390012 2800000000 0 00000 0 00000 0 0 VN390012 2900000000 0 00000 0 00000 0 0 VN390012 3000000000 0 00000 0 00000 0 0 VN390012 3100000000 0 00000 0 00000 0 0 VN390012 3300000000 0 00000 0 00000 0 0 VN390012 3400000000 0 00000 0 00000 0 0 VN390012 3500000000 0 00000 0 00000 0 0 VN390012 3600000000 0 00000 0 00000 0 0 VN390012 3700000000 0 00000 0 00000 0 0 VN390012 3900000000 0 00000 0 00000 0 0 VN390012 4000000000 0 00000 0 00000 0 0 VN390012 4100000000 0 00000 0 00000 0 0 VN390012 4200000000 0 00000 0 00000 0 0 VN390012 4300000000 0 00000 0 00000 0 0 VN390012 4400000000 0 00000 0 00000 0 0 VN390012 4500000000 0 00000 0 00000 0 0 VN390012 4600000000 0 00000 0 00000 0 0 VN390012 4700000000 0 00000 0 00000 0 0 VN390012 4900000000 0 00000 0 00000 0 0 VN390012 5000000000 0 01111 0 30000 0 0 VN390012 5100001111 0241111 0120000 0 0 VN390012 5200000000 0 00000 0 00000 0 0 VN390012 5300000000 0 00000 0 00000 0 0 VN390012 5400000000 0 00000 0 00000 0 0 VN390012 5500000000 0 00000 0 00000 0 0 VN390012 5600000000 0 00000 0 00000 0 0 VN390012 5700000000 0 00000 0 00000 0 0 VN390012 5800000000 0 00000 0 00000 0 0 VN390012 5900000000 0 00000 0 00000 0 0 VN390012 6200000000 0 00000 0 00000 0 0 VN390012 6300000000 0 00000 0 00000 0 0 VN390012 6400000000 0 00000 0 00000 0 0 VN390012 6500000000 0 00000 0 00000 0 0 VN390012 6600000000 0 00000 0 00000 0 0 VN390012 6700000000 0 00000 0 00000 0 0 VN390012 6800000000 0 00000 0 00000 0 0 VN390012 6900000000 0 00000 0 00000 0 0 VN390012 7000000000 0 00000 0 00000 0 0 VN390012 7100000000 0 00000 0 00000 0 0 VN390012 7200000000 0 00000 0 00000 0 0 VN390012 7300000000 0 00000 0 00000 0 0 VN390012 7400000000 0 00000 0 00000 0 0 VN390012 7500000000 0 00000 0 00000 0 0 VN390012 7700000000 0 00000 0 00000 0 0 VN390012 7800001111 0241111 0230000 0 0 VN390012 7900001111 0241111 0440000 0 0 VN390012 8000000000 0 00000 0 00000 0 0 VN390012 8100000000 0 00000 0 00000 0 0 VN390012 8200000000 0 00000 0 00000 0 0 VN390012 8300000000 0 00000 0 00000 0 0 VN390012 8400000000 0 00000 0 00000 0 0 VN390012 200000000 0 00000 0 00000 0 0 VN390012 8500000000 0 00000 0 00000 0 0 VN390012 8600000000 0 00000 0 00000 0 0 VN390012 8700000000 0 00000 0 00000 0 0 VN390012 8800000000 0 00000 0 00000 0 0 VN390012 8900000000 0 00000 0 00000 0 0 VN390012 9000000000 0 00000 0 00000 0 0 VN390012 9100000000 0 00000 0 00000 0 0 VN390012 9200001111 0241111 0400000 0 0 VN390012 9300000000 0 00000 0 00000 0 0 VN390012 9400000000 0 00000 0 00000 0 0 VN390012 9500001111 0241111 0 60000 0 0 VN390012 9600000000 0 00000 0 00000 0 0 VN390012 9700000000 0 00000 0 00000 0 0 VN390012 9800000000 0 00000 0 00000 0 0 VN390012 9900000000 0 00000 0 00000 0 0 VN390012 10000000000 0 00000 0 00000 0 0 VN390012 10100000000 0 00000 0 00000 0 0 VN390012 10200000000 0 00000 0 00000 0 0 VN390012 10300000000 0 00000 0 00000 0 0 VN390012 10400000000 0 00000 0 00000 0 0 VN390012 10500000000 0 00000 0 00000 0 0 VN390012 10600000000 0 00000 0 00000 0 0 VN390012 10700000000 0 00000 0 00000 0 0 VN390012 10800000000 0 00000 0 00000 0 0 VN390012 10900000000 0 00000 0 00000 0 0 VN390012 11000000000 0 00000 0 00000 0 0 VN390012 11100000000 0 00000 0 00000 0 0 VN390012 11200000000 0 00000 0 00000 0 0 VN390012 11300000000 0 00000 0 00000 0 0 VN390012 11400000000 0 00000 0 00000 0 0 VN390012 11500000000 0 00000 0 00000 0 0 VN390012 11700000000 0 00000 0 00000 0 0 VN390012 11800000000 0 00000 0 00000 0 0 VN390012 12000001111 0240000 0 00000 0 0 VN390012 12200000000 0 00000 0 00000 0 0 VN390012 12300000000 0 00000 0 00000 0 0 VN390012 12400000000 0 00000 0 00000 0 0 VN390012 12500000000 0 00000 0 00000 0 0 VN390012 12600000000 0 00000 0 00000 0 0 VN390012 12800000000 0 00000 0 00000 0 0 VN390012 13600000000 0 00000 0 00000 0 0 VN390012 13900000000 0 00000 0 00000 0 0 VN390012 14000000000 0 00000 0 00000 0 0 VN390012 14100000000 0 00000 0 00000 0 0 VN390012 14400000000 0 00000 0 00000 0 0 VN390012 100000000 0 00000 0 00000 0 0 VN390012 3200000000 0 00000 0 00000 0 0 VN390012 3800001111 0241111 0 70000 0 0 VN390012 4800001111 0241111 0400000 0 0 VN390012 6100001111 0241111 0290000 0 0 VN390012 7600000000 0 01111 0 20000 0 0 EN ZN
This is the control file with numbers, is the number in both files then de "VN990004" is not written
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 117 118 120 122 123 124 125 126 128 136 139 140 141 144 368 369 370 371 373 374 375 376 377 379 380 381 382 383 384
|
|
|
Post by tenochtitlanuk on Nov 1, 2021 17:11:08 GMT -5
Does this do what you want? I saved your data as 'original.fil' and 'control.fil', and it wrote 'newFile.txt'.
' Read the file. If 'VN990004' present in a line, skip- otherwise write to file count =0
CR$ =chr$( 13) +chr$( 10)
open "newFile.txt" for output as #fOut2 open "original.fil" for input as #fIn do line input #fIn, row$
I =instr( row$, "VN990004") if I <>0 then cowNumber =val( mid$( row$, I +9, 6))
if found( cowNumber) then #fOut2 row$; CR$; print "."; ' showing progress.. else count =count +1 print "!"; end if
end if
loop until row$ ="" or eof( #fIn) <0 close #fIn close #fOut2
print "" print " I removed "; count; " rows of data referring to 'VN990004' but cow's number was not in 'control.fil'."
end ' _________________________________________________
function found( cN) open "control.fil" for input as #fIn2 cN$ =input$( #fIn2, lof( #fIn2)) close #fIn2 if instr( cN$, str$( cN)) then found =1 else found =0 end function
function rndChrs$( N) ' generate random alpha content for i =1 to N rndChrs$ =rndChrs$ +chr$( 65 +int( 26 *rnd( 1))) next i 'rndChrs$ ="asdfghjkl" end function
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Nov 2, 2021 6:46:21 GMT -5
Almost there! I couldnt make it by my self. Thanks for the help. Its opposite now, the numbers in the newfile must just the other numbers which are not written now. And all the other lines in the in the original file has to be written also in the newfile.txt
I think I managed it
' Read the file. If 'VN990004' present in a line, skip- otherwise write to file count =0
CR$ =chr$( 13) +chr$( 10)
open "Newfile.txt" for output as #fOut2 open "original.file" for input as #fIn do line input #fIn, row$
I =instr( row$, "VN990004") if I <>0 then cowNumber =val( mid$( row$, I +9, 6))
if found( cowNumber) then #fOut2 row$; CR$; print "."; ' showing progress.. else count =count +1
print "!"; end if
end if if I <>1 then #fOut2 row$; CR$; loop until row$ ="" or eof( #fIn) <0 close #fIn close #fOut2
print "" print " I removed "; count; " rows of data referring to 'VN990004' but cow's number was not in 'control.fil'."
end ' _________________________________________________
function found( cN) open "control.file" for input as #fIn2 cN$ =input$( #fIn2, lof( #fIn2)) close #fIn2 if instr( cN$, str$( cN)) then found =0 else found =1 end function
function rndChrs$( N) ' generate random alpha content for i =1 to N rndChrs$ =rndChrs$ +chr$( 65 +int( 26 *rnd( 1))) next i 'rndChrs$ ="asdfghjkl" end function
|
|
konijn
Junior Member
Posts: 96
|
Post by konijn on Nov 2, 2021 14:32:26 GMT -5
"And don't forget to add the fileExists() function
How can I give a time in this function? For example that there is 10 minuten looking for a file and go on after that specific time.
|
|
|
Post by xxgeek on Nov 2, 2021 16:21:11 GMT -5
"And don't forget to add the fileExists() function How can I give a time in this function? For example that there is 10 minuten looking for a file and go on after that specific time. Sorry, I called it a function, but it's a sub. You could make it a function if you need to. Let me know and I'll write it up. The following values may be approximate, so do due diligence. Be sure to keep scan in the loop, so you can break out if needed, else it will hang until loop finishes. 1000 milliseconds = 1 second You want ten minutes? 1000 x 60 x 10 = 600000 milliseconds eg; From within a sub call test
sub test call pause 600000 end sub
sub pause mil print "@ sub pause - for 600000 milliseconds = 10 minutes" 'don't need this line, just a log in mainwin for reference t=time$("ms")+mil while time$("ms")<t scan wend end sub eg: Not from a sub call pause 600000
sub pause mil print "@ sub pause - for 600000 milliseconds = 10 minutes" 'don't need this line, just a log in mainwin for reference t=time$("ms")+mil while time$("ms")<t scan wend end sub
|
|
|
Post by tenochtitlanuk on Nov 2, 2021 17:51:00 GMT -5
Very computer-busy ways to implement a delay. Better is probably
call Sleep 1000 'Sleep for 1000 milliseconds
Sub Sleep ms CallDLL #kernel32, "Sleep",_ ms as long,_ ret as void End Sub
|
|
|
Post by xxgeek on Nov 2, 2021 17:58:41 GMT -5
Very computer-busy ways to implement a delay. Better is probably call Sleep 1000 'Sleep for 1000 milliseconds Sub Sleep ms CallDLL #kernel32, "Sleep",_ ms as long,_ ret as void End Sub When you get time, could you explain why?, and what actually happens with this method. Just curious....
|
|
|
Post by Rod on Nov 3, 2021 5:14:36 GMT -5
It’s about processor cycles. Windows OS is essentially sequential, though we now have multiple processors Liberty is single threaded and can’t make use of multiple processors.
If you want a program to be alert to user input and ready for any interrupt that comes along what you don’t do is tie the processor into a repeating loop that it MUST process. To complete these loops it must ignore and queue interrupts like a mouse click.
If the loop has an escape route like scan that can be ok but better to just set up another single interrupt with the timer command. That way the program can be doing other stuff like drawing.
The worst kind of loop is the while wend counting loop that is impossible to escape. For the entire delay period the processor can do nothing else.
|
|
|
Post by xxgeek on Nov 3, 2021 8:46:37 GMT -5
It’s about processor cycles. Windows OS is essentially sequential, though we now have multiple processors Liberty is single threaded and can’t make use of multiple processors. If you want a program to be alert to user input and ready for any interrupt that comes along what you don’t do is tie the processor into a repeating loop that it MUST process. To complete these loops it must ignore and queue interrupts like a mouse click. If the loop has an escape route like scan that can be ok but better to just set up another single interrupt with the timer command. That way the program can be doing other stuff like drawing. The worst kind of loop is the while wend counting loop that is impossible to escape. For the entire delay period the processor can do nothing else. Thanks Rod. I think I understand now. A call is made to a Win dll, and that allows one process(a Liberty program) to do its thing, while another process in play in another "program" is unaffected. But if a loop is used in a Liberty program Windows or another program stops dead until the "Liberty programs loop" finishes, or worse, hangs in the loop? Put another way - Windows dll puts the Liberty app to sleep for x seconds while other running apps go on their merry way? But without using the dll, and using a loop with Liberty code instead, forces ALL running apps to halt while the Liberty app controls the processor?
|
|