We help IT Professionals succeed at work.

Need to split folder names into parent and a sub-folders based on a separating character

352 Views
Last Modified: 2012-06-21
I have thousands of folders on my server document folder that have a NNNN-NNNNN naming convention. I need to use the "-" separator between the two numbers in the folder name and create a second layer folder

I.e.: orginial folder name is X:\1234-56789\CORR needs to be replaced with X:\1234\56789\CORR.

I've looked at a plethora of folder renaming utilities but none seem to do exactly what I want to do. I have almost 30,000 folders that need to be renamed.

Appreciate all assistance.
Comment
Watch Question

Top Expert 2009

Commented:
So all your NNNN-NNNNN foldlers appear at the base of X:\?
Top Expert 2009

Commented:
Do a test before you do your actual data.

Copy to a USB drive or wherever first or make sure you have a good backup.
set root=X:
 
for /f "tokens=1,2 delims=-" %%a in ('dir /b /ad %root%\????-?????') DO (
    MD %root%\%%a
    MOVE %root%\%%a-%%b %root%\%%a\%%b
)

Open in new window

Author

Commented:
Yes...all at x:\=

Note that underneath xxxx-xxxxx folders are subfolders and documents. I'll give your script a go.

Author

Commented:
What I am supposed to do with your commands...past it into VBS and run? Edit into a BAT? I need a little more instruction here.

Thanks.
Top Expert 2009

Commented:
Copy the 6 lines of code from 22178654 into notepad. Save it as movedata.bat

Double click to run movedata.bat

Make sure you have a good backup of X: before running the batch file.

Author

Commented:
Works great...except one last thing....the format "NNNN-NNNNN" I gave you is correct in 99% of instances, but are you able to make it so that irrespective of the folder name and the number of characters, to separate at the "-" (dash). That way, folders like Smith John,C-12345 will still be converted.

Top Expert 2009

Commented:
OK. But the folders on the root will only ever have 1 dash?

Easy enough to change.

set root=X:
 
for /f "tokens=1,2 delims=-" %%a in ('dir /b /ad %root%\*-*') DO (
    MD %root%\%%a
    MOVE %root%\%%a-%%b %root%\%%a\%%b
)

Open in new window

Author

Commented:
Yep! Great work....I'll accept that solution and add more pointss if you can help me with the one other similar request:

Is there a command we can run that will run through a folder structure and remove ever space, &, comma, colon, dash, underscore and semicolon from foldernames only, and then truncatte the folder name to 12 characters? This would really only apply to the the third level folders (underneath the 2 folders created from your previous command).
Top Expert 2009

Commented:
I don't care about the points so don't worry about that.

colon : is not valid and can never be put in to a path or filename.

So if you had 2 folders

Johnathan Whitehouse Documents
Johnathan Whitehouse_Music

They both can be merged to

JohnathanWhi

What if you had:

Johnathan Whitney Documents

Can these go into here as well?:

JohnathanWhi

Maybe your structure will never encounter this but I wanted to ask.

Author

Commented:
It actually wouldn't matter - part of the data cleanup is reduce the number of sub folders.
Top Expert 2009

Commented:
OK. Try this out.

If a file has the folder locked it will not be renamed.
With this script any duplicates will not be renamed so the first one would rename but the next ones would remain as it's original folder name.

SETLOCAL ENABLEDELAYEDEXPANSION
Set Root=X:
 
 
FOR /F "TOKENS=1,2,3,4,* delims=\\" %%a in ('dir /b /ad /s %Root%\*.*') do (
    IF "%%d" NEQ "" (
    Set 3rdLevelFolder="%%d"
    Set 3rdlevelFolder="!3rdLevelFolder: =!"
    Set 3rdlevelFolder="!3rdLevelFolder:&=!"
    Set 3rdlevelFolder="!3rdLevelFolder:,=!"
    Set 3rdlevelFolder="!3rdLevelFolder:-=!"
    Set 3rdlevelFolder="!3rdLevelFolder:_=!"
    Set 3rdlevelFolder="!3rdLevelFolder:;=!"
    set 3rdlevelFolder=!3rdlevelFolder:"=!
    echo "!3rdlevelFolder:~0,12!"
    IF "%%d" NEQ "!3rdlevelFolder:~0,12!" REN "%%a\%%b\%%c\%%d" !3rdlevelFolder:~0,12!
    )
)

Open in new window

Author

Commented:
Great job...works a treat, however I have some folder structures are located in x:\clients\=, which would mean that the folder level I need to change is actually level 4. What to I change in your code here to have it only apply to level 4?

And I have stumbled on two other issus:

1.  I have about a 1100 folders that I just need to remove the top level name altogether (i.e. x:\Bad Name\1234-56789\= needs to be modified to x:\1234-56789\=)

2. I have a similar number of folders that need to have text removed from the 1st level folder name. The bad next precedes the good text and is separated by an underscore (i.e. x:\Bad Text_1234-56789\= needs to be changed to x:\1234-56789\=) The Bad Text can be from 4-40 characters in length!.

Once again I guiltily lean upon you tower of knowledge!
Top Expert 2009

Commented:
For the forth level need to add another number to the FOR and use the next letter. I'll keep the variable name 3rdlevelFolder the same or you can change all occurance of this to levelfolder or just folder.
SETLOCAL ENABLEDELAYEDEXPANSION
Set Root=X:
 
 
FOR /F "TOKENS=1,2,3,4,5,* delims=\\" %%a in ('dir /b /ad /s %Root%\*.*') do (
    IF "%%e" NEQ "" (
    Set 3rdLevelFolder="%%e"
    Set 3rdlevelFolder="!3rdLevelFolder: =!"
    Set 3rdlevelFolder="!3rdLevelFolder:&=!"
    Set 3rdlevelFolder="!3rdLevelFolder:,=!"
    Set 3rdlevelFolder="!3rdLevelFolder:-=!"
    Set 3rdlevelFolder="!3rdLevelFolder:_=!"
    Set 3rdlevelFolder="!3rdLevelFolder:;=!"
    set 3rdlevelFolder=!3rdlevelFolder:"=!
    echo "!3rdlevelFolder:~0,12!"
    IF "%%e" NEQ "!3rdlevelFolder:~0,12!" REN "%%a\%%b\%%c\%%d\%%e" !3rdlevelFolder:~0,12!
    )
)

Open in new window

Top Expert 2009

Commented:
1.

Test it though. I did some quick test so it should be OK but you never know.

The RD will remove the bad directory if it's empty. It will error if it's not empty. 'The directory is not empty.'
setlocal enabledelayedexpansion
set root=X:
 
for /f "tokens=*" %%a in ('dir /s /b /ad "%root%\????-?????"') DO (
    for /f "tokens=1,2,3,* delims=\" %%b in ('echo "%%a"') do (
        set folder=%%d
        IF "!folder:~4,1!" == "-" if "%%e" == "" (
            ECHO "%%a"
            MOVE "%%a" %root%
            RD "%%b\%%c"
        )
    )
)

Open in new window

Top Expert 2009

Commented:
2.

It takes the first level folder and if there is a dash at the sixth character from the end.  Rename the folder using the last 10 characters.
setlocal enabledelayedexpansion
set root=X:
 
for /f "tokens=*" %%a in ('dir /s /b /ad "%root%\*-?????"') DO (
    for /f "tokens=1,2,* delims=\" %%b in ('echo "%%a"') do (
        set folder=%%c
        IF "!folder:~-7,1!" == "-" if "%%d" == "" (
            ECHO Renaming "%%a" "!folder:~-11!"
            REN "%%a" "!folder:~-11!"
        )
    )
)

Open in new window

Author

Commented:
Regarding the above (2)...I think it may have renamed it exactly the same thing based on what the DOS command box says....the REN function displays identical to the original file name.
Top Expert 2009

Commented:
You're right folders without a bad text at the beginning will try to do a rename with the exact same name.

I've added code to not attempt to rename if the names are the same.
setlocal enabledelayedexpansion
set root=X:
 
for /f "tokens=*" %%a in ('dir /s /b /ad "%root%\*-?????"') DO (
    for /f "tokens=1,2,* delims=\" %%b in ('echo %%a') do (
        set folder=%%c
        IF "!folder:~-7,1!" == "-" if "%%d" == "" if /i "%%a" NEQ "%root%\%%c" (
            ECHO Renaming "%%a" "!folder:~-11!"
            REN "%%a" "!folder:~-11!"
        )
    )
)

Open in new window

Author

Commented:
Okay....here is my biggest challenge....I have 100,000s of files that have an incompatible format that I need to rename to a compatible format, all dotted throughout the document structure.

THe format is MM-DD-YYYY.NNNN.ext (i.e. 09-20-2004.99.pdf), where MM-DD-YYYY is always the same format, but the NNNN section can be a numeric from 1 to 5 digits in lenght, followed by the dile extension.

I need to rename all documents that fit this format (i.e. start with MM-DD-YYYY) to be just NNNN.ext (i.e. 99.pdf in the example above).

Top Expert 2009

Commented:
So what happens to 12-25-1999.210.pdf and 04-05-2000.210.pdf?

Author

Commented:
There won't be any duplicates...document name 210 can only exist in one place at a time (I am cleanign up a Doc Managment System).

There is a new wrench with the folder renaming....there are too many permutations for your previous scripts to handle with regard to cleaning up folder names. Basically, every folder will have NNNN-NNNNN or NNNN.NNNNN.NNNN  or NNN-NNNNN or NNN.NNNNN.NNNN .somewhere within it. I need to remove everything else but that section from the folder name. The run the very first script you gave me (to create a subfolder based on on everything after "-"), then run a document name clean up.

I've attached a couple of folder examples so you can see the permutations. I have no problem running multiple steps to clean it up.
Screen1.gif
Screen2.gif
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.