Consulting

Results 1 to 4 of 4

Thread: Solved: to rename and create new folder..

  1. #1

    Solved: to rename and create new folder..

    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...

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Welcome to VBAX

    [VBA]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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    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

  4. #4
    Turn out i was wrong. the problem solved. Many thanks to mdmackillop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •