| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: RENaming Files |
> I needed to write a small program to rename all filenames in a
> directory which had spaces in their names, to replace the spaces with
> underscores.
>
> It was written without issue, but I had to resort to outputting the
> RENAME statements (DOS compatible) into a batch file. Then running
> the batch file.
>
> Does anyone know exactly how to rename A$ to B$ under Visual Basic
> v6.0 running Windows XP?
VB has a "Name...As...." statement that will rename files. Here is a
basic (no pun intended) subroutine that will rename all files in a given
directory as you requested (changing spaces to underscores).
Sub ReplaceFileNameSpacesWithUnderScores(DirectoryPath As String)
Dim FileName As String
' Make sure path end with a back slash character
If Right$(DirectoryPath, 1) "\" Then
DirectoryPath = DirectoryPath & "\"
End If
' Use Dir$ function in a loop to visit each file in directory
FileName = Dir$(DirectoryPath & "*.*")
Do While Len(FileName) > 0
Name DirectoryPath & FileName As _
DirectoryPath & Replace(FileName, " ", "_")
FileName = Dir$
Loop
End Sub
Note, however, that you can't call this subroutine from any code that
uses the Dir function within its own looping structure, such as
attempting to use Dir$ to get the directory names on a drive one-by-one
and renaming each file within each directory as they are found (the file
specification settings between the two alternating Dir$ statement calls
would interfere with each other).
Rick - MVP
---
þ RIMEGate(tm)/RGXPost V1.14 at BBSWORLD * Info{at}bbsworld.com
---
* RIMEGate(tm)V10.2áÿ* RelayNet(tm) NNTP Gateway * MoonDog BBS
* RgateImp.MoonDog.BBS at 12/6/04 12:06:24 PM
* Origin: MoonDog BBS, Brooklyn,NY, 718 692-2498, 1:278/230 (1:278/230)SEEN-BY: 633/267 270 5030/786 @PATH: 278/230 10/345 106/1 2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.