-1

I need to add a counter pattern to sub-folders of a chosen folder.

My VBScript so far is:

AuswahlTitel = "Bitte Datei oder Verzeichnis auswählen" 
StartOrdner = "17"  'Arbeitsplatz 
Set Dateiauswahl = CreateObject("Shell.Application").BrowseForFolder(0,AuswahlTitel,16,StartOrdner) 
Set Ordner = Dateiauswahl.Self 

RenameFolders(CreateObject("Scripting.FileSystemObject").GetFolder(Ordner.Path))

Sub RenameFolders(Ordner)
    Dim i, name, newName, number, a
    i = 0
    newName = ""
    For Each Unter In Ordner.SubFolders
        name = Unter.Name
        If name.EndsWith("B1") Then
            i = i + 1
            If i<10 Then
                number = "00" & i
            ElseIf i<100 Then
                number = "0" & i
            Else
                number = i
            End if
        End If
        a = Split(name,"_")
        newName = a(0) & "_" & a(1) & "_" & a(2) & "_" & a(3) & "_Heft_" & number & "_" & a(4) & "_" & a(5)
        Unter.Name = newName
    Next
End Sub

for some unknowen reason I only get the following error:error

So the command name = Unter.Name seems not to work and I don't know why.

Can anyone point me to the right direction?

Sebastian Röher
  • 417
  • 7
  • 20
  • You are accessing `name` as though it contains an `object` with properties when it is just a `string` containing the folder name. It will be `name.Endswith("B1")` is where the code is failing, almost looks like you are mixing .Net syntax with VBScript. – user692942 Nov 10 '15 at 10:24
  • yeah i found that meanwhile too thatnks for the hint. I now got it working and postet it as answer below – Sebastian Röher Nov 10 '15 at 10:45

1 Answers1

1

Found the solution. As Lankymart mentioned in his comment i was mixing up .Net with VBS.

the working code would be:

AuswahlTitel = "Bitte Datei oder Verzeichnis auswählen" 
StartOrdner = "17"  'Arbeitsplatz 
Set Dateiauswahl = CreateObject("Shell.Application").BrowseForFolder(0,AuswahlTitel,16,StartOrdner) 
Set Ordner = Dateiauswahl.Self 

RenameFolders(CreateObject("Scripting.FileSystemObject").GetFolder(Ordner.Path))

Sub RenameFolders(Ordner)
    Dim i, name, newName, number, fso, Unter
    i = 0
    newName = ""
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each Unter In Ordner.SubFolders
        'Set Unter = fso.GetFolder(Unter.Path)
        name = CStr(Unter.Name)
        If (InStr(name,"B1")>0) Then
            i = i + 1
            If i<10 Then
                number = "00" & i
            ElseIf i<100 Then
                number = "0" & i
            Else
                number = i
            End if
        End If
        a = Split(name,"_")
        newName = a(0) & "_" & a(1) & "_" & a(2) & "_" & a(3) & "_Heft_" & number & "_" & a(4) & "_" & a(5)
        Unter.Name = newName
    Next
End Sub

Ans as you see i replaced name.EndsWith("B1") with InStr(name,"B1")>0

Sebastian Röher
  • 417
  • 7
  • 20
  • Not quite the same as `EndsWith()` but will do the job, you could also use `If Right(name, 2) = "B1" Then` to mimic `Endswith()` closer. – user692942 Nov 10 '15 at 12:13