Google Ads

Auto create folder based on filename and move the file into it's folder

This is a Windows batch script that creates folders for each file in the current folder that the script is running. The folders are created with folder names that follow the filenames. After creating the folder, it will move the file into it's own folder. This batch script is particularly useful in organizing files into it's own folder.


Step 1 : Copy the Windows batch code below into your notepad and save it with filename "organize.bat".

@echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)

Step 2 : Copy organize.bat file to the folder where there are files that you want to organize each file to be copy into a folder which has the same name as the filename.

Step 3 : Double click organize.bat file and watch the magic begins.


Comments

  1. Worked beautifully for me - thanks!

    Also please note that this script does not create the folders with file extensions included (e.g. filename.txt does not create a folder "filename.txt" but just "filename"). Worked perfect for what I needed.

    ReplyDelete
    Replies
    1. Thanks! Glad you find it useful.

      Delete
    2. Hi Boon! Thank for your batch, seems to work great!
      Can it be modified to do the following:
      Filenames that start from 22 ("22.*") create a new folder called "22" and move them there.
      And filenames that start from ("40.*") create a new folder called "40" and move them there.
      Thank you so much for your time to answer me!

      Delete
    3. Hi Boon ! thank you for the batch file & the code. really works like magic. very helpful. Just like you - a Boon :)

      Delete
    4. Just in case you like to rename it after copying, mine i need to change it to game.iso

      @echo off
      for %%i in (*) do (
      if not "%%~ni" == "Organizer.bat" (
      md "%%~ni" && move "%%~i" "%%~ni" && ren "%%~ni\%%~i" "game.iso"
      )
      )
      pause


      Delete
    5. Hi, hoping you get this message as this post is a couple of years old. Is there a way to modify your script to move multiple files into 1 folder (bin/cue files)

      Delete
  2. amazing....you saved a lot of time for me.
    prefect script for creating folder and moving file name.

    ReplyDelete
  3. You are a life saver. Thank you very much man.

    ReplyDelete
  4. I am using Windows 10 and I can't get the .bat file to work. Should I save it as ANSI or UNICODE? Both doesn't work...

    ReplyDelete
    Replies
    1. place your code into a notepad file1 Save it as a text file while your working on it. them make a copy but save it a .bat file. The next time you click the .bat file it will run using Dos.

      Delete
    2. THANK YOU - This is brilliant and as everyone else says, a true time saver.

      I hope you can help me a little further please?
      I'm moving lots of music files into folders, Each file has an artist then a hyphen and then the title (Beatles - Hey Jude, Beatles - Please Please Me, Rolling Stones-Brown Sugar, Rolling Stones - The Last Time etc.)..
      How can I modify the bat file to create the folders just by the characters to the left of the first hyphen please? Example is that I have many files with Beatles, Rolling Stones etc. to the left of the hyphen and so many track names to the right. If there is a way to create a Beatles folder and then move all their songs into it please, then the same with Rolling Stones and other artists? All the song files can stay named as they are, it's just getting them into a single folder.

      Many thanks in advance, Nigel.

      Delete
  5. Thank you very much, but is there a way to make the BAT file not be moved Into a folder?

    ReplyDelete
    Replies
    1. Name the file as "organize", if you want to rename it to something else, then open that .bat file and change the word "organize" to the name of that file. I hope this helped.

      Delete
    2. Name the file "organize"

      Delete
    3. I had the same issue. I think it was because I wrongly using a uppercase "o" in the filename (i.e., "Organize.bat" vs. "organize.bat".

      Delete
  6. May many good be with you because of this valuable information.

    Thanks, Very Helpful.

    ReplyDelete
  7. simply amazing, and I make sure no adblocker for you page (while listed)

    ReplyDelete
  8. Wow you really helped me a lot.. TFS!

    ReplyDelete
  9. I need 0001A.jpg, 0001B.jpg... move to folder 0001
    Please help me!

    ReplyDelete
  10. I need 0001A.jpg, 0001B.jpg... move to folder 0001
    Please help me!

    ReplyDelete
  11. Thanks so much, saved me so much time!

    ReplyDelete
  12. HI BOON SEONG, GREAT PROGRAM. IT HELPED US LOT.

    ReplyDelete
  13. Amazing script, saved me a couple of hours work at least :)

    ReplyDelete
  14. i want new new folder same as file name but it should be first 3 letters of file . pls help

    ReplyDelete
    Replies
    1. i have the same problem, i don't wanna the full name on the folder just first 9 letters of file, how can i change the code to do this ?

      Delete
    2. i have the same problem, i don´t wanna full name in the folder, how can i change the code to do this ?

      Delete
  15. Doesn't seem to work on Windows 10. Even running as admin.

    ReplyDelete
    Replies
    1. That means you don't have administrative privileges.

      Delete
  16. Doesn't seem to work on Windows 10. Even running as admin.

    ReplyDelete
    Replies
    1. Sadly, does not work, Access Denied as stated above. Windows 10 Enterprise.

      Delete
  17. Hi, I need urgent help to sort files, and this blog is the only one where I found something similar to my problem.

    I have the following files:

    [WhatsApp] Image 2020-02-27 at 17.29.28.jpeg
    [Chrome] methods.jpg

    And then I need a new folder to be created using the name in the two brackets at the beginning. It doesn't matter if the folder includes the brackets or not. Example:

    [Chrome]
    [Whatsapp]

    ReplyDelete
  18. Thanks .. i used this to organize files with same name and different extensions in the same folder.

    ReplyDelete
    Replies
    1. I tried this with same names and different extensions on some of the files but didn't work - it just ignored the 2nd file it came across?

      Delete
  19. Yes! it does create folder names from filename, anyfile1.xxx anyfile2.xxx etc & move those files to the corresponding folders,

    BUT it also moves the batch file to its own named folder, an unfortunate unexpected side effect, which can be managed - once you're aware its going to happen.

    ReplyDelete
  20. Thank you so much... Saved me a lottt of my time....

    ReplyDelete
  21. Worked perfectly on Windows 10 Enterprise! Thanks so much for this life saving solution. :-)

    ReplyDelete
  22. I have a list of files in a folder upon running a batch as below
    @echo off
    for %%i in (*) do (
    if not "%%~ni" == "organize" (
    md "%%~ni" && move "%%~i" "%%~ni"
    )
    )

    It will create a folder each for the file and move that file to respective folder.

    My requirement is:
    I will have list of files in a folder, also I will have a csv /excel sheet in which I will have 2 columns 1. File Id 2. File Name (It is same file names which are in the folder).Now I want to create a folder for each file but I want to name the folder with the File Id which is in CSV which maps with the file name.

    ReplyDelete
  23. I have a list of files in a folder upon running a batch as below
    @echo off
    for %%i in (*) do (
    if not "%%~ni" == "organize" (
    md "%%~ni" && move "%%~i" "%%~ni"
    )
    )

    It will create a folder each for the file and move that file to respective folder.

    My requirement is:
    I will have list of files in a folder, also I will have a csv /excel sheet in which I will have 2 columns 1. File Id 2. File Name (It is same file names which are in the folder).Now I want to create a folder for each file but I want to name the folder with the File Id which is in CSV which maps with the file name.

    ReplyDelete
  24. Worked beautifully for me - thanks!

    Also please note that this script does not create the folders with file extensions included . Worked perfect for what I needed.

    ReplyDelete
  25. Replies
    1. Thank you sooooooooooooooooo much!!! This script is super helpful :)

      Delete
  26. Thanks, this did exactly what I needed. Very grateful.

    ReplyDelete
  27. Is there any chance of making this work with mouse right-click option on file(s) and "send to folder with same filename" ??

    ReplyDelete
  28. It is creating only folders not moving files under it

    ReplyDelete
  29. If anybody has problems with the bat file being put insided a folder, that's because the name of the bat need to match the name inside the actual code, IT'S CASE SENSITIVE, to have it to work with the code written above in step 1, you need to call the bat organize.bat not Organize.bat

    ReplyDelete
  30. Is there a powershell command that does the same?

    ReplyDelete
  31. Lets say that I have 100 pictures files in one folder. and they are all different names.
    and I want to make each folder with same file names group.

    All file names are like this:

    tiger(1), tiger(2), tiger(3), tiger(4)......tiger(10)
    lion(1), lion(2), lion(3)....lion(6)
    horse(1), horse(2), horse(3)
    pig(1), pig(2), pig(3), pig(4), pig(5)

    I want to make auto folder with folder name tiger and it has tiger(1)....tiger(10)
    another folder called lion and it has files lion(1)....lion(6)
    something like this.
    When I run this script, it creates folder with every single file names.
    Folder goes like tiger(1) another folder goes tiger(2)..

    How can I auto create auto folders with same name group?

    ReplyDelete
  32. shows access denied... tried disabling kaspersky and giving full control to bat file under the properies tab...but no luck

    ReplyDelete
  33. Thanks - BIG time saver - worked perfectly!

    ReplyDelete
  34. I have a huge request!
    How can i configure this to create folders by author from a file?

    I have files - in properties is the "author" tag.
    i want to create folders with Authors name and inside add all files of each author.

    Is this possible?

    ReplyDelete
  35. Thanks so much for this solution!

    ReplyDelete
  36. Thank you, exactly what I was searching for. Worked perfectly for my need. :-)

    ReplyDelete
  37. I ran this script on a network drive (NAS), it did not work, do i need any special command lines, i do have an admin rights on this laptop, i browsing to my NAS and running this script but it does not create any folder, please help!

    ReplyDelete
    Replies
    1. No special configurations needed. Just make sure you run from the same directory where the files need to be processed and it is on MS Windows.

      I was looking for this for my NAS and it worked beautifully.

      Delete
    2. same issue here

      Delete
  38. This is simple and very useful. Could you tell me how to have it not transfer over punctuation to the folder name? so if the filename is file.name.zip, the folder will just be file name and not file.name.
    Thank you.

    ReplyDelete
  39. Any solution for Win10/11 users? I'm getting the access denied error when I run the batch, as others have mentioned before.
    Any work around would be GREATLY appreciated.

    ReplyDelete
  40. Thanks for this I had over 2000 movies on a 14TB hard drive and I wanted to move them into separate folders to use with kodi and your script saved me days of work.

    Now i just need a script that deletes everything after the date, so
    Melancholic.(2018).720p.BluRay.x264.AAC-YTS.MX would just become Melancholic.(2018) or
    Minyan.2020.WEBRip.x264-ION10 would change to Minyan.2020 do you know anyway to do this or point me to somebody that might know.

    ReplyDelete
  41. I made something in Powershell for the same purpose

    $BasePath = "C:\Temp\test"
    $FilesGreaterThen = 1MB
    $MoveAllFilesToRoot = $True

    $AllFiles = Get-ChildItem -Path $BasePath -Recurse -File | Where-Object {$_.Length -Gt $FilesGreaterThen}

    $AllFiles | ForEach-Object {
    $File = ($_.DirectoryName + "\" + $_.Name)

    If($MoveAllFilesToRoot) {
    $Directory = ($BasePath + "\" + $_.BaseName)
    } Else {
    $Directory = ($_.DirectoryName + "\" + $_.BaseName)
    }

    New-Item -ItemType Directory -Force -Path $Directory
    Move-Item -Path $File -Destination $Directory

    If(-Not(Test-Path($_.DirectoryName + "\*"))) { #Delete source directory if empty
    Remove-Item -Path $_.DirectoryName
    }
    }

    ReplyDelete
  42. Hi there
    I'm wondering how I could change this script so that it creates the folder names using the first 7 characters of the file names?
    Can you advise?
    cheers
    Kaz

    ReplyDelete
    Replies
    1. hi Kazb can you send me the script please

      Delete
  43. awesome, cheers dude

    ReplyDelete
  44. It did exactly what I wanted. You have saved me a lot of time, and I just wanted to take a moment to say thank you.

    ReplyDelete
  45. A big thank you! Great solution, works perfectly.

    ReplyDelete
  46. THANK YOU, BOON! YOU ARE WONDERFUL!

    ReplyDelete
  47. This is amazing man thanks !!!

    ReplyDelete
  48. Hello Boom, very nice and useful batch file. Do you know how to get the creation date, EXIF I believe, instead of the file name? That would be the icing on the cake for me to organize my files for a indie movie project. Thanks in advance and have a great day.

    ReplyDelete
  49. It is something really awesome! It saved my days worth of work. Thanks man!!

    ReplyDelete
  50. Hope that someone is still monitoring this !! This works great and would like to ask if someone could provide the batch code for a little bit of a mod. Have a bunch of files based on names+date like "JohnSmith03052022" and "JohnSmithe05162021". Would like to create a directory called "JohnSmith" then move all names containing "JohnSmith" into that directory. Is that possible?

    ReplyDelete
    Replies
    1. Look for a program called ROBOBASKET - That will do the job for you; but it's not free.

      Delete
  51. Hope someone is still monitoring this thread ! Would like to know if someone can provide the batch code for a slight modification. I have files with a naming convention of "UserName+Date". For instance "JohnSmith09232019" and "JohnSmith02312021". Would like to move all files containing "JohnSmith" into a folder named "JohnSmith". Is that something that is possible? I'm not sure of what variable would work in the code. Would appreciate advice !

    ReplyDelete
  52. Works fantastically. Thanks!

    ReplyDelete
  53. W O N D E R F U L! Thank you so much!

    ReplyDelete
  54. Honestly you are awesome. This is an incredible time saver!

    ReplyDelete
  55. Damn impressed. thanks a tonne, works like magic <3

    ReplyDelete
  56. i DO NOT REPLY MOSTLY BUT THIS IS LIKE A MIRACLE FOR A SUPER LAZY PERSON LIKE ME HAHA THANK YOU SO MUCH

    ReplyDelete
  57. Thanks so much! Blessings to you!

    ReplyDelete
  58. YOU SIR ARE AN EFFING HERO, I have never ever commented on smt but this one is a hidden gem <3

    ReplyDelete
  59. saves time and works like magic ... May God Bless You man

    ReplyDelete
  60. holy crap... it actually worked...

    ReplyDelete
  61. Thanks. I just put all my saved films into individual folders, to stop them auto-playing through.

    ReplyDelete
  62. Thanks for the code! I edited the code as I needed a variation. I used this to create a folder with the same name for each file, but don't move the original file.

    @echo off
    for %%i in (*) do (
    if not "%%~ni" == "organize" (
    md "%%~ni"
    )
    )



    Thanks again!

    ReplyDelete

Post a Comment

Thank you for visiting Almost a Technocrat. Due to many spam comments, your comment will be moderated.

Popular posts from this blog

A Better SKMEI 1358 Instructions Manual

A Better SKMEI 1418 / 1427 Instructions Manual

How to export Tasker's Projects, Profiles, Tasks and Scenes

SKMEI 1358 Review

Jailbreak your Amazon Kindle Keyboard