Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
mr mr is offline
external usenet poster
 
Posts: 1
Default Numbering files according to existing files

Hi

I have a sub (see below) that I use to number files with, according to
which revision the file represents.
The sub checks which filenumbers already exist in a given directory
and gives the new file the next number (e.g. projectname7 = next file
will be named projectname8).

The sub works.

However, the directory now contains several thousand files, making the
sub very slow.
I assume this is because potentially every file in the directory must
be checked in each loop.

Now, is there a smarter way of doing this? (preferably one that takes
into account the possibilty of not all revisions, being present in the
directory. The current version will name a file projectname1 if the
filename is not represented in the directory, even if projectname2
exists).

Public Sub TxtFileNumber()

'The txtfile number is determined. A search is performed for existing
files and the new file is assigned the next available number.
Dim NumFiles As Long
Dim FileName As String
Dim FileNames() As String 'This is an Array.

'The first file in the directory is found.
FileName = Dir("C:\something")
NumFiles = 1

'The array is redimensioned, without deleting content.
ReDim Preserve FileNames(NumFiles)
FileNames(NumFiles) = FileName

'Filenames are inserted into an array until there are no more in the
directory.
Do While FileName < ""
FileName = Dir() 'Next file in directory.
If FileName < "" Then
NumFiles = NumFiles + 1
ReDim Preserve FileNames(NumFiles)
FileNames(NumFiles) = FileName
End If
Loop

NextNumber = 1
Found = True

'A search is performed for projectname1. If this file is in the
directory a search for projectname2 is performed.
'This continues until projectnameX (where X is an integer) is not
found. The new file then receives this number (done elsewhere - the
number is the important part here).

While Found = True
For Each fname In FileNames
If fname = "projectname" & NextNumber & ".txt" Then
Found = True
NextNumber = NextNumber + 1
Exit For
End If
Found = False
Next fname
Wend
Range("NextNumber").Formula = NextNumber

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Numbering files according to existing files

You could maintain a database (or excel worksheet) where you list the base
file name and the existing revisions or the basefilename and the last
revision number. Then access this to create the new file - and update the
database.

--
Regards,
Tom Ogilvy

"mr" wrote in message
om...
Hi

I have a sub (see below) that I use to number files with, according to
which revision the file represents.
The sub checks which filenumbers already exist in a given directory
and gives the new file the next number (e.g. projectname7 = next file
will be named projectname8).

The sub works.

However, the directory now contains several thousand files, making the
sub very slow.
I assume this is because potentially every file in the directory must
be checked in each loop.

Now, is there a smarter way of doing this? (preferably one that takes
into account the possibilty of not all revisions, being present in the
directory. The current version will name a file projectname1 if the
filename is not represented in the directory, even if projectname2
exists).

Public Sub TxtFileNumber()

'The txtfile number is determined. A search is performed for existing
files and the new file is assigned the next available number.
Dim NumFiles As Long
Dim FileName As String
Dim FileNames() As String 'This is an Array.

'The first file in the directory is found.
FileName = Dir("C:\something")
NumFiles = 1

'The array is redimensioned, without deleting content.
ReDim Preserve FileNames(NumFiles)
FileNames(NumFiles) = FileName

'Filenames are inserted into an array until there are no more in the
directory.
Do While FileName < ""
FileName = Dir() 'Next file in directory.
If FileName < "" Then
NumFiles = NumFiles + 1
ReDim Preserve FileNames(NumFiles)
FileNames(NumFiles) = FileName
End If
Loop

NextNumber = 1
Found = True

'A search is performed for projectname1. If this file is in the
directory a search for projectname2 is performed.
'This continues until projectnameX (where X is an integer) is not
found. The new file then receives this number (done elsewhere - the
number is the important part here).

While Found = True
For Each fname In FileNames
If fname = "projectname" & NextNumber & ".txt" Then
Found = True
NextNumber = NextNumber + 1
Exit For
End If
Found = False
Next fname
Wend
Range("NextNumber").Formula = NextNumber

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to copy and paste over existing files Cam Excel Discussion (Misc queries) 1 July 1st 09 08:37 PM
Can't open existing files by double-clicking. Kirby Excel Discussion (Misc queries) 9 July 29th 08 02:24 PM
Number format on existing files symyf Excel Discussion (Misc queries) 0 May 10th 06 05:45 PM
How I stopped a Book 1 opening with existing files sirbob Excel Discussion (Misc queries) 0 April 4th 06 07:01 PM
automatic sequential numbering of new excel files pkw59 Setting up and Configuration of Excel 1 October 4th 05 11:53 AM


All times are GMT +1. The time now is 09:38 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"