Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello,

    I have a folder that is unorganized to my liking, but it contains over 3,000 files in it, so I am looking for an automatic way to sort them as follows:

    This as an example list of the files in the unorganized folder:

    apple.exe
    apple.html
    apple.jpg
    banana.exe
    banana.gif
    pear.bat
    pear.com
    pear.exe
    pear.gif
    pear.wav

    I have already created folders for these files as follows:

    /apple/
    /banana/
    /pear/

    I am looking for an automatic way of putting:
    all files named apple in the folder named apple,
    all files named banana in the folder named banana, and
    all files named pear in the folder named pear


    Thanks in advance,
    HandyAndy
    Time can fix anything.....even a broken clock. - Handy Andy

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    You can do something like this at the Command Prompt.

    Code:
    for /f %x in ('dir /ad /b') do move %x*.* %x
    This lists all directories and then moves any file that has a name that begins with the directory name to the directory.
    To test it - so you don't make a complete mess - use this command first.

    Code:
    for /f %x in ('dir /ad /b') do echo move %x*.* %x
    This echos the move command to the screen so you can see what would happen.

    To use the command you need to:
    1. Open a Command Prompt.
    2. Change to the directory where the files exist - cd \fulldirectoryname.
    3. Run the test command.
    4. Run the real command.
    Don't forget to backup first in case it does something weird.

    cheers, Paul

  3. #3
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks, I haven't tried it yet though.
    Could you break apart your command and explain what each piece does?
    Thanks
    Time can fix anything.....even a broken clock. - Handy Andy

  4. #4
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok after trying the test code out I get the following error:
    "Cannot move multiple files to a single file."

    What have I done wrong? Because I feel like I am so close to getting this to work.
    Time can fix anything.....even a broken clock. - Handy Andy

  5. #5
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello,

    I found a batch code that worked, except now I have another problem.

    Sorry, I forgot a little detail from my example in my first post.
    This is the updated example list of files in the unorganized folder:

    apple.exe
    apple.html
    apple.jpg
    apple (2).jpg
    banana.exe
    banana.gif
    banana (2).gif
    pear.bat
    pear.com
    pear.exe
    pear.gif
    pear (2).gif
    pear.wav

    Using the following batch code that I found, I was able to get it to work except for the files like apple (2).jpg, banana (2).gif, and pear (2).gif.

    @echo off
    setlocal enabledelayedexpansion
    pushd "C:\Change to\Whateveryour\path"
    for %%j in (*) do (
    set name=%%~nj
    if exist !name!\ (
    move "%%j" "!name!"
    ) else (
    echo Warning: %%j not moved to !name!: folder does not exist.
    )
    )
    popd
    pause
    exit

    This will move any files into a folder (in the same directory) with the same name as the file. If the folder does not exist it will not move the files.

    I don't know if I can modify this batch code to also work with those files, but if someone else does I would appreciate it. Thanks.
    Time can fix anything.....even a broken clock. - Handy Andy

  6. #6
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    The code does not handle spaces in file names. You need to change "set name=%%~nj" to "set name=%%~nxj"

    The %%~nj part resolves to the file name, except when there are spaces or dots on the file name.
    To see what the modifiers do, type this at a command prompt.
    Code:
    for /?
    cheers, Paul

  7. #7
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Okay, I made those changes but it still doesn't help me.

    My problem now is that any file named "filename (2)" won't get moved at all. This is because there is no folder called "filename (2)" and there shouldn't be, because "filename (2)" should go into folder "filename".

    Let me know if this can be done, or if you are now confused.......

    Thanks again.
    Time can fix anything.....even a broken clock. - Handy Andy

  8. #8
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    Sorry, that will muck up the directory check, but there is no way around that using your code because the directory name is derived from the file name.
    My code checks for the existence of a directory and then moves any files that match into it. Try this - I've added a back slash to the directory name in the move command.
    Code:
    for /f %x in ('dir /ad /b') do move %x*.* %x\
    cheers, Paul

  9. #9
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Awe-awe-awesome!!! I am impressed, didn't think it could be done.
    Now if you don't mind, could you try to explain to me how it does its magic.

    My guess, in my own visual thinking way...(I know this is not exactly how it works, but hear me out because I think the result would be the same.)
    It arranges all files and folders in the directory by name, in a list, without moving the folders to the top of that list.
    Then it moves the files that are just below a folder into that folder.

    I know I am a strange thinker, but am I somewhat close?

    Thanks again
    Time can fix anything.....even a broken clock. - Handy Andy

  10. #10
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    If we break it down into it's parts you will see how it works.

    1. We need a list of the directories you have created. We use the old DOS command "dir" for this and tell it to only show directories.
    Code:
    dir /ad /b
    2. We take the output of this command and loop through each response. We use a variable to hold each response, in this case "%x".
    Code:
    for /f %x in () do
    3. Now we need to move the files into the directory we have found. We know the files start with the same name as the directory so we can use wild cards to find anything that starts with the directory name.
    Code:
    move %x*.* %x\
    All of the commands we have used have help built in, so you can put "/?" on the end of them to see how to use them.
    Code:
    dir /?
    for /?
    move /?
    cheers, Paul

  11. #11
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    what are these for?.......
    /ab
    /b
    /f

    Also would it be possible to turn this into a batch file?
    Time can fix anything.....even a broken clock. - Handy Andy

  12. #12
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    /ab = list only attributes that equal directory
    /b = return a bare listing, we don't want header or summary information.
    /f = change the default behaviour so we can run an external program, in this case "dir".

    To put this in a batch file you need to double the percent signs, per your posted code, and maybe add a pause so you can see the result. I'll let you work out how and we can help if you get stuck.

    cheers, Paul

  13. #13
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks a lot, I'll give the batch file a try and let you know how it goes.
    Time can fix anything.....even a broken clock. - Handy Andy

  14. #14
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    I still sometimes get the error "Cannot move multiple files to a single file."
    What does this actually mean?
    Time can fix anything.....even a broken clock. - Handy Andy

  15. #15
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    I found some new problems with your command.

    It created files with the first name of everyone found. For example:

    \apples and pears\

    apples and pears.jpg

    Now became:

    \apples and pears\

    apples and pears.jpg

    apples (This file was created and has no extension)

    There were some other weird things that it did too, and it is leaving me stumped.
    I can't make this command into a batch file until I know it works properly.
    Luckily I saved the original contents.

    Any help on this would be great. Thanks.
    Time can fix anything.....even a broken clock. - Handy Andy

Page 1 of 2 12 LastLast

Posting Permissions

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