It's a lot to digest and it was written for a different purpose than you are using it. I made a file/folder dialog popup for users with Access2000. Now I just use a cDialog class.
Here's what's going on with the function. The first loop is
getting all of the files from the directory you specified and assigning them to an array.
The second loop is assinging the value from array1 into array2 and creating a new name for the file and storing it in the second index of the array. Then, within this loop, you use array2 to pass file specs to the fileSystemObject
to copy and delete each file in the directory.
Here is a step by step.
This is where you prime you file or folder string.
strTempName is being passed the first file or folder in the
path that you set (strdirpath). This also sets the path
for the rest of the loop.
Code:
strTempName = Dir(strdirpath, vbDirectory)
Here, you are saying, if strTempName isn't an empty string, loop again.
Code:
Do Until Len(strTempName) = 0
Now, you want to exclude the . and .. directories
Code:
' Exclude ".", "..".
If (strTempName <> ".") And (strTempName <> "..") Then
Now, we want to make sure we are looking at files and not folders.
Code:
If (GetAttr(strdirpath & strTempName) And _
vbDirectory) <> vbDirectory Then
Here, you are increasing the array size
assigning the current file to the array
at your current file count
and then, incrementing your file counter.
Code:
' Increase the size of the array
' to accommodate the next file
' and add the filename to the array.
ReDim Preserve varFile1(lngFileCount)
'Populate the array with the current file
varFile1(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
Get your next file or folder name. Every time you call
this function, it will get the next file or folder
available in the directory you set earlier. If you call the function and it returns an empty string, there are no more files or folders in the directory.
Code:
' Use the Dir function to find the next filename.
strTempName = Dir()
Now, you are going to set the second arrays size based on
the number of files found in the directory you specified.
Code:
ReDim varFile2(lngFileCount, 1) As Variant
Here, you are setting up a forNext loop.
lngIdx will be used as the index in your arrays.
lngFildCount is the number of items in your first array
that was populated during the first step.
Code:
For lngIdx = 0 To lngFileCount - 1
The next part is confusing. I see also that I made a mistake in my code.
This line was not necessary so remove it.
Code:
varFile2(lngIdx, 0) = varFile1(lngIdx)
Here, you are disecting the file name to pull out
"Shares_1". After re-reading your post, I find that this is
not necessary so... take it out.
Code:
lngUScore1 = InStr(1, varFile1(lngIdx), "_")
lngUScore2 = InStr(lngUScore1, varFile1(lngIdx), "_")
You have a two dimensional array. Index 1 will relate to the index in your first array (the count of all files found)
The second index will be for the old name of the file and new name for the file. Index 0 will be the old name and
index 1 will be the new name.
Code:
'bring in old name of file
varFile2(lngIdx, 0) = strdirpath & varFile1(lngIdx)
I have changed the way a name is assigned to the file.
Now it is just "Shares" & current file index & ".txt"
Code:
'create new name of file
varFile2(lngIdx, 1) = strdirpath & "shares" & lngIdx & ".txt"
Now we are going to use that information
you are going to call the fileSystemObject and use the CopyFile function to rename the file to the new name.
Code:
oFS.CopyFile varFile2(lngIdx, 0), varFile2(lngIdx, 1)
Now we are deleting the old file that was just copied and renamed'
Code:
oFS.DeleteFile varFile2(lngIdx, 0)