Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    May 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help moving files into folders with same name

    Hi guys

    I need some help to move more than 1800 files in folders with the same name.

    I have many folders and files with the same names in a large folder structure.
    The files and folders with the same name is already at the same place in the structure, but the files is not inside the folders with same name.
    I want to move all files in the structure into the folders with the same name.

    In a old thread I found this command for cmd:

    Quote Originally Posted by Paul T View Post

    Code:
    for /f "tokens=*" %x in ('dir /ad /b') do move "%x*.*" "%x\"
    cheers, Paul
    This code shown in the post above did work well but did only sort files in the folder the command was given.
    Can someone help with make this work in all subfolders also?

    I appreciate any help

    Cheers

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,205
    Thanks
    49
    Thanked 989 Times in 919 Posts
    This is starting to look like an exam question (this is the third thread)!

    Sub-directories make this a bit more complex, but the basics don't change.

    The easiest method is to list all sub-directories, change to that directory, then run the command.
    First we need to recursively find directories.
    Code:
    dir /s /ad /b
    Next we need to change to the directory.
    Code:
    cd directoryname
    Then run the move command.
    Code:
    for /f "tokens=*" %x in ('dir /ad /b') do move "%x*.*" "%x\"
    To do all that we need to add another for loop so the directory change works. This requires we use a different variable name so Windows doesn't get confused.
    Code:
    for /f "tokens=*" %a in ('dir /s /ad /b') do cd "%a"
    The problem here is that we need to do multiple things with this data so we have to combine the commands using brackets. This can only be done in a batch file, not in a single command, so we'll need to double up the percent signs once we put it in a batch file.
    Code:
    for /f "tokens=*" %a in ('dir /s /ad /b') do (
    Change directory.
    Code:
    cd %a
    Run the original code.
    Code:
    for /f "tokens=*" %x in ('dir /ad /b') do move "%x*.*" "%x\"
    Finally close the brackets
    Code:
    )
    At this point we realize the command didn't work on the top folder, so we need to put the original code at the top of the batch file, before anything else.
    Code:
    for /f "tokens=*" %x in ('dir /ad /b') do move "%x*.*" "%x\"
    I'll let you put it all together in a batch file.

    cheers, Paul

  3. #3
    New Lounger
    Join Date
    May 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply Paul T Pretty funny that it was one of your posts back in 2008 that I found this command. Firstly, I have no idea how to use this as a batch file, sry Im a noob. Second, after what I understand I have to list all subdirectories and I have more than 1800 folders spread out in a chaotic structure. Do I have to write down all the names on all folders to make this work? Is it possible to do this in another way to make it more easy? Cheers
    Last edited by Tore78; 2015-05-17 at 11:20.

  4. #4
    New Lounger
    Join Date
    May 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have no clue about cmd but I found this somewhere on the web

    Code:
    dir /s /b /o:n /ad > f.txt
    This was made by Endoro at stackoverflow. The purpose of this was to print a list of the folders and subfolders of a directory to a file.
    So I tried it for fun and it made a complete list of folders and files. I then thought since it listed all subfolders and files maybe I can use it.

    This was the original code from you Paul T:
    Code:
    for /f "tokens=*" %x in ('dir /ad /b') do move "%x*.*" "%x\"
    I then replaced ('dir /ad /b') with (dir /s /b /o:n /ad) from Endoro

    so the code become this:
    Code:
    for /f "tokens=*" %x in ('dir /s /b /o:n /ad') do move "%x*.*" "%x\"
    and magically it worked ....lol...
    talk about being lucky...hehe

  5. #5
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,205
    Thanks
    49
    Thanked 989 Times in 919 Posts
    "/o:n" tells the DIR command to sort in name order, which is not required for this to work.

    cheers, Paul

  6. #6
    New Lounger
    Join Date
    May 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    aha ok. I will change it then for next time

Posting Permissions

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