top of page

Moving a Large Amount of Files with a .bat Command in Windows 10.

  • Writer: Kevin Croes
    Kevin Croes
  • Dec 21, 2023
  • 3 min read

Recently I found myself recovering a hard drive for a friend, but unfortunately the files recovered were no longer in their previous folder structure. If you find yourself in this situation, here's how I managed to make that happen.


You're going to want to make sure all the files are in the same folder. Here's an example using photos from Unsplash.com


Step 1:

Now right click, new and create a text document:





Step 2:

Give the file a name, right click and click edit:





Paste the following code:

@echo off
setlocal EnableDelayedExpansion
set n=0
for %%f in (*.*) do ( 
set /A n+=1 
set list[!n!]=%%~nf 
) 
set Filesx=%n%
for /L %%i in (1,1,%Filesx%) do ( 
mkdir "!list[%%i]!" 
)
for %%f in (*.*) do ( 
move "%%f" "%%~nf" 
)

If you want an explanation of the batch command, read below.

Here's a step-by-step breakdown:

  • @echo off: This command turns off the display of each command in the batch file as it is executed, providing a cleaner output.

  • setlocal EnableDelayedExpansion: This enables delayed variable expansion, which allows variables to be expanded at execution time rather than when the line is parsed.

  • set n=0: Initializes a variable n to zero, which will be used to count the number of files.

  • The first for loop (for %%f in (*.*) do ...) iterates through all files in the current directory (*.*):

  • set /A n+=1: Increments the value of n for each file, effectively counting the number of files.

  • set list[!n!]=%%~nf: Assigns the base name of the current file (%%~nf) to an array named list at the index corresponding to the current value of n.

  • set Filesx=%n%: Stores the total number of files in the variable Filesx.

  • The second for loop (for /L %%i in (1,1,%Filesx%) do ...) iterates from 1 to the total number of files:

  • mkdir "!list[%%i]!": Creates a directory with the name stored in the list array at the current index.

  • The third for loop (for %%f in (*.*) do ...) iterates through all files in the current directory:

  • move "%%f" "%%~nf": Moves each file to a directory with the same name as its base name, effectively organizing the files into separate directories.


Please note that this script assumes that there are no duplicate base names, as it creates directories based on the unique base names of the files. If there are files with the same base name, the script might encounter issues.



Step 3:

Select file, save as, change "save as type" from .txt file to "All Files". Instead of .txt file, we're going to save it as a .bat file.





Step 4:

Now we're going to double click the .bat file and run it.



Step 5:

Now every file has been placed into it's own folder, you can just delete the "Movethesefiles" folder.





Conclusion

One small thing to note is that if the file name contains a character like an exclamation point "!" it may not put inside it's own folder and I don't exactly know why. Moving one file manually is at the very least less annoying than moving hundreds or thousands manually.

I hope this help you sort out thousands of files at once, please like my blog post if this helped you!


Comments


bottom of page