Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

renaming a file w/ wildcards in name

Status
Not open for further replies.

dgr7

IS-IT--Management
Dec 29, 2006
43
0
0
US
hello,
I have this code that I'm trying to use but is not working, so I'm posting on here to see if someone can help me with how to fix the error. What the code is trying to do is to look in the subdirectory
D:\B D client data\H T\NewBusiness\
for a file that will have (a string of numbers) + (.txt) as it's file name and rename that file to HT012507.txt.
There is another file in the same directory named HT.txt that needs to be left alone and not renamed. These are the only two .txt files in the subdirectory.

Visual Basic is giving me the
Run-time error '52':
Bad file name or number
when I try to run the code, and stops on the Name line, so I'm assuming the SourceFile line is the problem.

Can anyone help me with what the syntax of the below code so it will work properly?

thanks in advance,
david

Code:
Dim SourceFile, DestinationFile As String
    
 SourceFile = "D:\B D client data\H T\NewBusiness\[1-9]*.txt"
 DestinationFile = "D:\B D client data\H T\NewBusiness\HT" & Format(Date, "mmddyy") & ".txt"
    Name SourceFile As DestinationFile
 
From vb6 help Name Statement;

<<Name arguments cannot include multiple-character (*) and single-character (?) wildcards>>

You may consider using Shell with a DOS command eg. COPY

HTH Hugh
 
The DOS command RENAME may be a better choice... Type RENAME HELP at a command prompt to get the syntax.
 
Hugh,
hello, thanks for the assistance.
I found this webpage and read:
about the DOS rename command,
but I don't see how to type the syntax of it to get it to rename only a file that has a name of
(a string of numbers) + (.txt)
(example: 23450173.txt)
and leave alone the HT.txt file that's in the same subdirectory.
Please help....

next question:
if the DOS rename command is the way to go, then how do I execute that command in VB code?
 
Use DIR to loop through the file names (first time with the arguments, then if needed, the second time with-out), and rename the one that doesn't match HT.txt.
See VB Help on DIR.
 
SBerthold
Thank you!
I just wrote some code with the DIR command in a Do While look and it worked exactly as I wanted.

Now have a related question....if the file I'm renaming it to already exists in the subdirectory, a File Already Exists run-time error 58 happens. Can the code be altered so that it'll overwrite the existing file (and without any user prompts) if it happens to already be there?

thanks again,
david
 
Private Sub Command1_Click()

Dim a$
Dim Path2Files$

Path2Files$ = "D:\B D client data\H T\NewBusiness\"

a$ = Dir$(Path2Files$ & "*.txt")

Do While Len(a$)
If IsNumeric(Left$(a$, InStr(a$, ".") - 1)) Then Exit Do
Dir$
Loop

If Len(a$) Then
If Len(Dir$(Path2Files$ & "HT" & Format(Date, "mmddyy") & ".txt")) Then Kill Path2Files$ & "HT" & Format(Date, "mmddyy") & ".txt"
Name Path2Files$ & a$ As Path2Files$ & "HT" & Format(Date, "mmddyy") & ".txt"
End If

End Sub
 
Hugh,

thanks for the quick response w/ code!
I think I figured out what to do...

david
 
Good!

Note my use of IsNumeric may be imperfect because it will accept strings like "-1234", "+456", "123-E04" etc. If the number of numbers is known a line like! this may be more robust;

If a$ Like "######.txt" then ...

regards Hugh,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top