DIM, FreeFile() FSO & IF THEN

R

rebelscum0000

Dear All,

I have a few questions can someone please help me?

Thanks in Advance
Regards,
Antonio Macias


A) Do I must Dim all the variables I have in my code without
exceptions?

B) What FreeFile() does?

C) How to delete a file trough VBA, without using FSO?
*I read in a post:
"I've only ever found one case where FSO
could do something that I couldn't do using VBA and/or API calls (the
ability to get the LastUpdated date for a folder)"

D) Can someone explain to me what each line of these IF Then condition
does

If Left(VSFLRT, Len(VDAS)) = VDAS Then
*If This = That is True Then what?
*Do nothing?
*Must do something?
*Is this a correct way of testing for a certain string?

'Exclusion code
If Left(VSFLRT, Len(VDAS)) = VDAS Then
ElseIf Left(VSFLRT, Len(VMSO)) = VMSO Then
ElseIf Left(VSFLRT, Len(VPRF)) = VPRF Then
ElseIf Left(VSFLRT, Len(VWIN)) = VWIN Then
Else
'Write it out
Print #VOUF, VSFLRT
End If
End If

This is my entire code
Sub Reading_TxtFile_R()

Dim VOTP As String 'OriginTxtPath
Dim VDTP As String 'DestinationTxtPath
Dim VRTP As String 'RenameTxtPath
Dim VTXN As String 'TxtName
Dim VINF As Integer 'InFile
Dim VOUF As Integer 'outFile
Dim VRET As String 'ReadText
Dim VSFL As String 'SearchFolderLine
Dim VDAS As String 'Documents and Settings
Dim VMSO As String 'MSOCache
Dim VPRF As String 'Program Files
Dim VWIN As String 'WINDOWS
Dim VMYC As Integer 'MyCounter
Dim VSFLR As String 'SearchFolderLineReplace
Dim VSFLRT As String 'SearchFolderLineReplaceTrim


VTXN = "LastSearch"
VSFL = "Search Folder"
VDAS = "C:\Documents and Settings"
VMSO = "C:\MSOCache"
VPRF = "C:\Program Files"
VWIN = "C:\WINDOWS"
VMYC = 0
VOTP = "C:\Program Files\Fineware\hound4\Searches\" & VTXN & ".txt"
VDTP = CurrentDb().Name
VDTP = Left$(VDTP, _
Len(VDTP) - Len(Dir$(VDTP))) & _
(VTXN & ".txt")

'Obtain legal handles
VINF = FreeFile()
Open VOTP For Input As #VINF
VOUF = FreeFile()
Open VDTP For Output As #VOUF

Do Until EOF(VINF) 'Loop until end of file.
Line Input #VINF, VRET 'Read line into variable.

'Then you can read it line by line:
'Do something with the line.
If Left(VRET, Len(VSFL)) = VSFL Then
'Counter
VMYC = VMYC + 1
'Replace
VSFLR = Replace(VRET, VSFL & _
" " & VMYC & ":", "", , , vbTextCompare)
VSFLRT = Trim(VSFLR)
'Exclusion code
If Left(VSFLRT, Len(VDAS)) = VDAS Then
ElseIf Left(VSFLRT, Len(VMSO)) = VMSO Then
ElseIf Left(VSFLRT, Len(VPRF)) = VPRF Then
ElseIf Left(VSFLRT, Len(VWIN)) = VWIN Then
Else
'Write it out
Print #VOUF, VSFLRT
End If
End If
Loop

'Tidy up
Close #VINF
Close #VOUF
 
D

Dirk Goldgar

(answers inline)

rebelscum0000 said:
A) Do I must Dim all the variables I have in my code without
exceptions?

It is not absolutely mandatory, but it is best practice. Relying on
contextual declaration of variables often leads to errors that are hard
to track down. I strongly recommend using Option Explicit -- or
checking the VB option to require variable declaration -- so that you
are in fact forced to declare all variables.
B) What FreeFile() does?

Returns the first unused file number, to serve as the "handle" to a file
you intend to open. By using FreeFile to get the number, rather than
just assuming you can use some fixed number such as 1, you ensure that
your I/O code works regardless of what other file operations may be
taking place concurrently.
C) How to delete a file trough VBA, without using FSO?
*I read in a post:
"I've only ever found one case where FSO
could do something that I couldn't do using VBA and/or API calls (the
ability to get the LastUpdated date for a folder)"

Use the Kill statement.
D) Can someone explain to me what each line of these IF Then condition
does

If Left(VSFLRT, Len(VDAS)) = VDAS Then

Checks to see if VSFLRT begins with the value held by VDAS.
*If This = That is True Then what?

It doesn't say. That would be specified on the next line.
*Is this a correct way of testing for a certain string?

That depends on what you're trying to test. The statement doesn't check
to see if VSFLRT *equals* VDAS; it checks to see if *starts with* VDAS.
'Exclusion code
If Left(VSFLRT, Len(VDAS)) = VDAS Then
ElseIf Left(VSFLRT, Len(VMSO)) = VMSO Then
ElseIf Left(VSFLRT, Len(VPRF)) = VPRF Then
ElseIf Left(VSFLRT, Len(VWIN)) = VWIN Then
Else
'Write it out
Print #VOUF, VSFLRT
End If
End If

What that code is saying is, "Write out VSFLRT only if it doesn't begin
with any of the values held in VDAS, VMSO, VPRT, and VWIN."
 
R

rebelscum0000

Dear Dirk Goldgar,

Thank you very much
Now I need to do "something" with the VSFLRT result, I will start a
new topic

Regards,
Antonio Macias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top