PDA

View Full Version : Solved: to rename and create new folder..



rizki96
12-10-2011, 03:01 PM
hi,

I want to rename a file in collumn A to a new file with new folder. This column already represent the actualy file on the hardisk.
column A ----> Output
D:\01#j.txt --> D:\01\001.txt
D:\01#x.txt --> D:\01\002.txt
D:\02#b.txt --> D:\02\001.txt
D:\03#a.txt --> D:\03\001.txt
D:\03#b.txt --> D:\03\002.txt
D:\03#g.txt --> D:\03\003.txt
...

Output : making new folder exactly as i mention above.
1. the folder represent the first name of the filename
2. the filename output is a counter of how many first string/value on the original filename.

thanks in advance...

mdmackillop
12-10-2011, 06:59 PM
Welcome to VBAX

Option Explicit

Sub Filing()
Dim Fld
Dim r As Range, cel As Range
Dim Pth As String
Dim i As Long
Dim Fil As String

Pth = "D:\"
Set r = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
For Each cel In r
i = 0
Fld = Split(Right(cel, Len(cel) - Len(Pth)), "#")(0)
On Error Resume Next
MkDir Pth & Fld
On Error GoTo 0
Fil = Dir(Pth & Fld & "*.txt")
Do Until Fil = ""
i = i + 1
Name Pth & Fil As Pth & Fld & "\" & Format(i, "000") & ".txt"
Fil = Dir
Loop
Next
End Sub

rizki96
12-10-2011, 09:28 PM
hi mdmackillop,

Thanks for your help. I have tried your code, turn out the output is
D:\01#j.txt --> D:\01#j\001.txt
D:\01#x.txt --> D:\01#x\001.txt
D:\02#b.txt --> D:\02#b\001.txt
D:\03#a.txt --> D:\03#a\001.txt
D:\03#b.txt --> D:\03#b\001.txt
D:\03#g.txt --> D:\03#g\001.txt

can you help me again to review whats wrong for the code.... many thanks :bow:

rizki96
12-10-2011, 11:58 PM
Turn out i was wrong. the problem solved. Many thanks to mdmackillop