Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 442167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:02:42+00:00 2026-05-12T21:02:42+00:00

Using VB6 In a folder, am having n number of text files, file names

  • 0

Using VB6

In a folder, am having n number of text files, file names are like abc.mis-.txt, def.fin@.txt, so i want to rename the filenames like abcmis.txt, deffin.txt. I used a function for renaming.

Code

Dim filename3 As String
filename3 = Dir$(txtSourceDatabaseFile & "\*.txt", vbDirectory)
Do While filename3 <> ""

‘Function for renaming

Dim strInput As String
Dim stroutput As String
Dim strChar As String
Dim intChar As Integer
Dim intLoop As Integer
strInput = filename3
For intLoop = 1 To Len(strInput)
strChar = Mid$(strInput, intLoop, 1)
intChar = Asc(strChar)
If ((intChar >= 48) And (intChar <= 57)) Or _
((intChar >= 65) And (intChar <= 90)) Or _
((intChar >= 97) And (intChar <= 122)) Or _
(intChar = 95) Then
stroutput = stroutput & strChar
End If
Next

Name txtSourceDatabaseFile & "\" & filename3 As txtSourceDatabaseFile & "\" & stroutput & ".txt"


filename3 = Dir$
Loop

Above Coding is working, the problem is am using while conditon, so it will rename all the txt files, It is giving a names likes

For Example.

The first time giving a fileaname as abc in "stroutput"
The second time giving a filename as abcdef in "Stroutput"
...,

It is adding a previous filename also, because am getting a filename in while loop itself.

How to modify my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:02:42+00:00Added an answer on May 12, 2026 at 9:02 pm

    Move your function into a VB6 function that takes the filename as an argument and returns the new one, eg :

    Private Function GetTextOnlyFileName(ByVal strOldName as String) As String
        Dim strTemp As String
        Dim strChar As String
        Dim intChar As Integer
        Dim intLoop As Integer
        For intLoop = 1 To Len(strOldName)
            strChar = Mid$(strOldName, intLoop, 1)
            intChar = Asc(strChar)
            If ((intChar >= 48) And (intChar <= 57)) Or _
                ((intChar >= 65) And (intChar <= 90)) Or _
                ((intChar >= 97) And (intChar <= 122)) Or _
                (intChar = 95) Then
            strTemp = strTemp & strChar
            End If
        Next
        GetTextOnlyFileName = strTemp
    End Function
    

    Then use something like this this to get the new filename :

    Do While filename3 <> ""
        strNewName = GetTextOnlyFileName(strFileName3)
        'next rename the file to strNewName
    
    
        'get name of next file
    Loop
    

    However, what do you want to happen if two different source filenames give the same renamed one? Eg, currently abc@def.txt and ab@cd@ef.txt would return the same thing.

    Your code is currently appending the filename to the previous one because you’re reusing the same variables again and again in your loop without clearing the prior values. My function will work, but you could just add the line strOutPut = “” after the line strInput = filename3 and that should work too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.