×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Trim filenames in folder from specific character
2

Trim filenames in folder from specific character

Trim filenames in folder from specific character

(OP)
I am using this code to trim filenames. I cannot figure out how to expand this to trim ALL files in the folder after the first SPACE from the left in the filename.

CODE -->

Before: 41-010 Rev A.txt
After: 41-010.txt 

My Code:

CODE -->

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Scripts'} Where " & "ResultClass = CIM_DataFile")

For Each objFile In colFiles

    strPath = objFile.Drive & objFile.Path
    strExtension = objFile.Extension
    strFileName = objFile.FileName


    If Right(strFileName, 1) = "A" Then

        intLength = Len(strFileName)
        strFileName = Left(strFileName, intLength - 6)

    End If

    strNewName = strPath & strFileName & "." & strExtension
    errResult = objFile.Rename(strNewName)

Next 

RE: Trim filenames in folder from specific character

Something like this should work

CODE

For Each objFile In colFiles

    strPath = objFile.Drive & objFile.Path
    strExtension = objFile.Extension
    strFileName = objFile.FileName

    p = InStr(strFileName, " ")
    If p > 0 Then
        strFileName = Left(strFileName, p - 1)
        strNewName = strPath & strFileName & "." & strExtension
        errResult = objFile.Rename(strNewName)
    End If
Next 

RE: Trim filenames in folder from specific character

(OP)
Thanks guitarzan, this works great!

RE: Trim filenames in folder from specific character

Or, if you like brevity, the slightly shorter:

CODE

For Each objFile In colFiles
    With objFile
        errResult = .Rename(.Drive & .Path & Split(.FileName, " ")(0) & "." & .Extension)
    End With
Next 

RE: Trim filenames in folder from specific character

(OP)
Thanks strongm, this is another great option that works great.

RE: Trim filenames in folder from specific character

(OP)
Strangely, I ran both methods two different times and got a couple filenames that did not change. Maybe one of you can figure out why.

CODE -->

4-961010-02 revE.txt 

** UPDATE: This seems to happen just randomly. It will just skip some files for what seems like no apparent reason. After it finishes, I delete all other files in the folder and keep a handful that did not change. When I re-run the code, those files change and are correct.

RE: Trim filenames in folder from specific character

>It will just skip some files for what seems like no apparent reason

I suspect that may be down to the usage of WMI to get the folder listing

RE: Trim filenames in folder from specific character

(OP)
strongm; is there a way to fix that?

RE: Trim filenames in folder from specific character

Yes, you could use the filesystemobject

RE: Trim filenames in folder from specific character

(OP)
I'm lost... not sure how I need to incorporate that into this code.

RE: Trim filenames in folder from specific character

The quick answer is, your loop would change to:

CODE

Set fso = CreateObject("Scripting.FileSystemObject")
Set colFiles = fso.GetFolder("C:\Scripts").Files

For Each objFile In colFiles

   ...

Next 

But the FileSystem Object has different methods than the WMI way you were using, so the code inside the loop would have to change. For now, here is a reference to FSO's methods

http://www.devguru.com/content/technologies/vbscri...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close