473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Dir() function problem

Re: Access 2000

According to the help, to loop through a folder, you use code like
this:

' Returns filename with specified extension. If more than one *.ini
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in
the
' same directory.
MyFile = Dir
--------------
ok...I'm trying something similar.
I am trying to loop through a folder and do something with each of the
4 .mdb files in the folder.
Whenever I add something in my loop, I get an "Invalid procedure call
or argument" error on this line: strOldFile = Dir
When I add the "FileExists" function, I need to do this to see if the
new file exists then if it does, delete the old one.
If I remove the "FileExists" line, the >>strOldFile = Dir<< line of
code works fine for all files in the loop.
I have tried this by adding an array of different lines of code, but
when I do, then the >>strOldFile = Dir<< line will always return
null on the 2nd try, and there are 4 files I need to look at in this
loop.

What good is this process......
MyFile = Dir("C:\WINDOWS\*.INI")
MyFile = Dir
.....when I cannot add anything to it?

--------
Here is my code:
strOldFile = Dir("C:\My Documents\*.mdb")
Do While strOldFile <> ""
strNewFile = Left$(strOldFile, (Len(strOldFile) - 4)) & "_CMPCT.MDB"
blnreturn = FileExists(strDirectory & strNewFile)
Debug.Print "Old File: " & strOldFile & " / " & "New File: " &
strNewFile
strOldFile = Dir
Loop

-------

Thanks for the assistance.

RLN
Nov 12 '05 #1
4 7259
In article <13**************************@posting.google.com >,
rl**********@yahoo.com (Ron Nolan) writes:
Re: Access 2000

According to the help, to loop through a folder, you use code like
this:

' Returns filename with specified extension. If more than one *.ini
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in
the
' same directory.
MyFile = Dir
--------------
ok...I'm trying something similar.
I am trying to loop through a folder and do something with each of the
4 .mdb files in the folder.
Whenever I add something in my loop, I get an "Invalid procedure call
or argument" error on this line: strOldFile = Dir
When I add the "FileExists" function, I need to do this to see if the
new file exists then if it does, delete the old one.
If I remove the "FileExists" line, the >>strOldFile = Dir<< line of
code works fine for all files in the loop.
I have tried this by adding an array of different lines of code, but
when I do, then the >>strOldFile = Dir<< line will always return
null on the 2nd try, and there are 4 files I need to look at in this
loop.

What good is this process......
MyFile = Dir("C:\WINDOWS\*.INI")
MyFile = Dir
....when I cannot add anything to it?

--------
Here is my code:
strOldFile = Dir("C:\My Documents\*.mdb")
Do While strOldFile <> ""
strNewFile = Left$(strOldFile, (Len(strOldFile) - 4)) & "_CMPCT.MDB"
blnreturn = FileExists(strDirectory & strNewFile)
Debug.Print "Old File: " & strOldFile & " / " & "New File: " &
strNewFile
strOldFile = Dir
Loop

-------

Thanks for the assistance.

RLN


Ron

At a guess, FileExists is a user created function that utilises the Dir
function. Unfortunately, using Dir in this way resets the first Dir in the
loop. You may wish to create an array and write the file names to this array.
Something like:
Sub SLoopDir()
Dim strOldFile As String
Dim strNewFile As String
Dim astrDBFiles() As String
Dim intLoop As Integer
ReDim astrDBFiles(1 To 2, 1 To 10)
intLoop = 1
strOldFile = Dir("C:\My Documents\*.mdb")
Do While strOldFile <> ""
strNewFile = Left$(strOldFile, (Len(strOldFile) - 4)) & "_CMPCT.MDB"
astrDBFiles(1, intLoop) = strOldFile
astrDBFiles(2, intLoop) = strNewFile
intLoop = intLoop + 1
If intLoop Mod 10 = 0 Then ReDim Preserve astrDBFiles(1 To 2, 1 To
intLoop + 10)
strOldFile = Dir
Loop
If intLoop > 1 Then ReDim Preserve astrDBFiles(1 To 2, 1 To intLoop - 1)
For intLoop = 1 To UBound(astrDBFiles)
Debug.Print "old: " & astrDBFiles(1, intLoop) & vbTab & "new: " &
astrDBFiles(2, intLoop)
Next intLoop
End Sub

Once you have the file names in the array, you can then check if they exist,
and perform actions as required.
--

Jon

www.applecore99.com - Access Tips and Tricks

Nov 12 '05 #2
> Ron

At a guess, FileExists is a user created function that utilises the Dir
function. Unfortunately, using Dir in this way resets the first Dir in the
loop. You may wish to create an array and write the file names to this array.
Something like:
Sub SLoopDir()
Dim strOldFile As String
Dim strNewFile As String
Dim astrDBFiles() As String
Dim intLoop As Integer
ReDim astrDBFiles(1 To 2, 1 To 10)
intLoop = 1
strOldFile = Dir("C:\My Documents\*.mdb")
Do While strOldFile <> ""
strNewFile = Left$(strOldFile, (Len(strOldFile) - 4)) & "_CMPCT.MDB"
astrDBFiles(1, intLoop) = strOldFile
astrDBFiles(2, intLoop) = strNewFile
intLoop = intLoop + 1
If intLoop Mod 10 = 0 Then ReDim Preserve astrDBFiles(1 To 2, 1 To
intLoop + 10)
strOldFile = Dir
Loop
If intLoop > 1 Then ReDim Preserve astrDBFiles(1 To 2, 1 To intLoop - 1)
For intLoop = 1 To UBound(astrDBFiles)
Debug.Print "old: " & astrDBFiles(1, intLoop) & vbTab & "new: " &
astrDBFiles(2, intLoop)
Next intLoop
End Sub

Once you have the file names in the array, you can then check if they exist,
and perform actions as required.

Jon,

This worked beautifully! Thank you very much for your assistance
here! Have a great day.

Ron
Nov 12 '05 #3
Jon,

Your solution here works, but I wanted to ask some questions, mainly
for my own education, so I could understand your methods better:
Dim astrDBFiles() As String ReDim astrDBFiles(1 To 2, 1 To 10)<<

My thoughts here: is it less efficient to just dim it as an array from
the beginning?

If intLoop Mod 10 = 0 Then ReDim Preserve astrDBFiles(1 To 2, 1 To

intLoop + 10)<<
What does "redim preserve" do?

Thanks,

RLN
Nov 12 '05 #4
astrDBFiles _is_ dim'd as an array from the start -- but without _specific
dimensions_.

You use ReDim to reset the dimensions of an array when you only know how big
it is as you continue through the code. The Preserve option preserves the
contents of the array through the ReDim -- otherwise it is redimensioned and
reinitialized.

Larry Linson
Microsoft Access MVP

"Ron Nolan" <rl**********@yahoo.com> wrote in message
news:13**************************@posting.google.c om...
Jon,

Your solution here works, but I wanted to ask some questions, mainly
for my own education, so I could understand your methods better:
Dim astrDBFiles() As String ReDim astrDBFiles(1 To 2, 1 To 10)<<

My thoughts here: is it less efficient to just dim it as an array from
the beginning?

If intLoop Mod 10 = 0 Then ReDim Preserve astrDBFiles(1 To 2, 1 To

intLoop + 10)<<
What does "redim preserve" do?

Thanks,

RLN

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: build | last post by:
G'day All, I have a problem with this loop. There are a number of .txt files in 'myPath'. tmpFile = Dir(myPath & "\*.txt") 'PROCESS FOLDER Do Until tmpFile = "" <lottsa code> <too much to...
7
by: Frank McCown | last post by:
This has probably been answered a million times, but I can't seem to find the answer. How can I change the owner of a directory using a Perl command? I know chown can be used with files, but it...
15
by: Viviana Vc | last post by:
How can I programatically do the equivalent of the following: cacls "C:\Program Files\test" /T /G Everyone:f ? Thanks, Viv
3
by: Ronald W. Roberts | last post by:
I'm not sure where this routine came from, but here is the problem. On certian computers this error occurs, but it does not on others. The error is: "Undefined Function "DIR" in expression". ...
3
by: Joe Costa | last post by:
I have written the following code to search for the right file depending on the startup file for "Client Access". The menu database that I made will load the correct config file specific for each...
3
by: Bugs | last post by:
Hi Everyone. This problem is driving me crazy. Im using Access2003. Basically what I wish to do is create a new record for each jpg bmp or gif file in the current directory. I need to...
3
by: dibblm | last post by:
Below is current code used. I can only list one directory then move to next. I want to search one more directory further and can't seem to find how to get one deeper. What I want to accomplish is...
1
by: Scott | last post by:
Apparently VB.Net has a new behaviour for the DIR function and I quote: - ================================================================================================== In Visual Basic 6.0,...
1
by: stuart.medlin | last post by:
I have an Access 2003 application that I recently converted from Access 97. I have a routine in which I use to export the data that the users have keyed into a text file. This text file resides...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.