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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trim filenames in folder from specific character 2

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
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
 
Something like this should work
Code:
For Each objFile In colFiles

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

    [highlight #FCE94F]p = InStr(strFileName, " ")[/highlight]
    [highlight #FCE94F]If p > 0 Then[/highlight]
        [highlight #FCE94F]strFileName = Left(strFileName, p - 1)[/highlight]
        strNewName = strPath & strFileName & "." & strExtension
        errResult = objFile.Rename(strNewName)
    [highlight #FCE94F]End If[/highlight]
Next
 
Or, if you like brevity, the slightly shorter:

Code:
[blue]For Each objFile In colFiles
    With objFile
        errResult = .Rename(.Drive & .Path & Split(.FileName, " ")(0) & "." & .Extension)
    End With
Next[/blue]
 
Thanks strongm, this is another great option that works great.
 
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.
 
>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
 
I'm lost... not sure how I need to incorporate that into this code.
 
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top