• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

help changing variable to rename files in a random folder

Elliot

New Member
Hi, I would really like it if you help me:
I have this code:

Code:
Option Explicit
Sub rename_5()
Dim NewName As String, mydir As String, objFile As Object
Dim Last_name As String, id As String, b As String, m As String
mydir = Application.ThisWorkbook.Path
id = Range("e2").Value
b = Range("f2").Value
m = Range("g2").Value
With CreateObject("Scripting.FileSystemObject")
For Each objFile In .GetFolder(mydir).Files
If objFile.Name = id & "_" & "tkp" & n & "_tkl" & b & "_CI.xls" Then _
NewName = "CI_tkp" & b & "_" & id & ".xls"

If InStr(objFile.Name, "4t_SHOT_") Then _
NewName = id & "_4t_SHOT_" & Format(Date, "DDMMYYYY") & ".zip"

If objFile.Name = id & "_tkp" & n & "_tkl" & b & "_norm" & ".xml" Then _
NewName = id & "_Plan_" & Format(Date, "DDMMYYYY") & ".xml"

If Len(Dir(mydir & "\" & NewName)) = 0 Then objFile.Name = NewName

Next
End With
MsgBox "Done"
End Sub

This code renames files within a folder, but for that the macro has to be within this folder.
I wish you would help me to replace the variable "mydir" to ask me which folder I want to rename the files.
thanks for your help.
 
Last edited by a moderator:
Try this:
Code:
Sub rename_5()
Dim NewName As String, myDir As String, objFile As Object
Dim Last_name As String, id As String, b As String, m As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    .InitialFileName = Application.ThisWorkbook.Path
    .Show
    If .SelectedItems.Count = 0 Then
        Exit Sub
    Else
        myDir = .SelectedItems(1)
    End If
End With

'Just for debug purposes
MsgBox myDir

id = Range("e2").Value
b = Range("f2").Value
m = Range("g2").Value
With CreateObject("Scripting.FileSystemObject")
    For Each objFile In .GetFolder(myDir).Files
        If objFile.Name = id & "_" & "tkp" & m & "_tkl" & b & "_CI.xls" Then _
        NewName = "CI_tkp" & b & "_" & id & ".xls"
       
        If InStr(objFile.Name, "4t_SHOT_") Then _
        NewName = id & "_4t_SHOT_" & Format(Date, "DDMMYYYY") & ".zip"
       
        If objFile.Name = id & "_tkp" & m & "_tkl" & b & "_norm" & ".xml" Then _
        NewName = id & "_Plan_" & Format(Date, "DDMMYYYY") & ".xml"
       
        If Len(Dir(myDir & "\" & NewName)) = 0 Then objFile.Name = NewName
   
    Next
End With
MsgBox "Done"
End Sub
 
Try this:
Code:
Sub rename_5()
Dim NewName As String, myDir As String, objFile As Object
Dim Last_name As String, id As String, b As String, m As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    .InitialFileName = Application.ThisWorkbook.Path
    .Show
    If .SelectedItems.Count = 0 Then
        Exit Sub
    Else
        myDir = .SelectedItems(1)
    End If
End With

'Just for debug purposes
MsgBox myDir

id = Range("e2").Value
b = Range("f2").Value
m = Range("g2").Value
With CreateObject("Scripting.FileSystemObject")
    For Each objFile In .GetFolder(myDir).Files
        If objFile.Name = id & "_" & "tkp" & m & "_tkl" & b & "_CI.xls" Then _
        NewName = "CI_tkp" & b & "_" & id & ".xls"
      
        If InStr(objFile.Name, "4t_SHOT_") Then _
        NewName = id & "_4t_SHOT_" & Format(Date, "DDMMYYYY") & ".zip"
      
        If objFile.Name = id & "_tkp" & m & "_tkl" & b & "_norm" & ".xml" Then _
        NewName = id & "_Plan_" & Format(Date, "DDMMYYYY") & ".xml"
      
        If Len(Dir(myDir & "\" & NewName)) = 0 Then objFile.Name = NewName
  
    Next
End With
MsgBox "Done"
End Sub


Hi Luke,

This is just what I wanted, you are awesome.
Thank u very much
 
Back
Top