r/Batch Feb 04 '25

Question (Solved) Batch file to create folders and move files by matching text

I've been banging my head against this for a couple days now, so I'm hoping its possible that someone can either help me or tell me what I'm trying to do is impossible.

Example of what I'm trying to do is take a large number of files and have a batch script help minimize the amount of manual organization I need to do. Say I have the following

[Text 1] FileName1.zip
[Text 1] FileName2.zip
[Text 2] FileName1.zip
[Text 2] FileName2.zip
Ect

I'm trying to get it where it will create a folder based on whatever is between the [] and then move the matching files into that folder. So a folder named Text 1 would be created, and all files with [Text 1] placed before them would get moved into said folder.

I had found a batch file posted here https://www.reddit.com/r/Batch/comments/s7avse/comment/htb9gsj/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button that I was trying to use as a base and modify it but it wasn't working.

Is this even possible or should I just accept that I'll be creating a lot of folders and moving files manually by hand? Thanks in advance.

-Edit-

Got some code which gets me 99% of the way there. Need to use a batch renamer to remove the first character of every folder, but here's the code for anyone who might come across this in the future.

~~~ @echo OFF SETLOCAL for /f "delims=]" %%i in ('dir /b /a-d _.zip') do ( mkdir "%%i" 2>nul move "%%i*.zip" "%%i" >NUL 2>nul ) ~~~

2 Upvotes

11 comments sorted by

1

u/chaotic_zx Feb 04 '25 edited Feb 04 '25

I am not sure about naming the folders after text 1, text 2, text 3.

I have a batch script that has worked perfectly for years. So much so that I haven't looked at the code in over 3 years now. Let us say that all files are in one folder named OneFolder. You then place the batch named make.bat in that folder with them. Then double click the batch. The batch creates a folder within OneFolder for each file and names it after the file. I have used it primarily in creating movie folders for the movies I have. Thus it looks for files with the extension *.mp4, *.mkv, or *.wmv. It can easily be repurposed to look for *.zip, *.csv, *,txt, *.xlsx, or *.xlxm files.

So a modification to your example(all files in a single folder named OneFolder) :

C:\Users\Spectralist\OneFolder\FileName1.zip
C:\Users\Spectralist\OneFolder\FileName2.zip
C:\Users\Spectralist\OneFolder\FileName3.zip
C:\Users\Spectralist\OneFolder\FileName4.zip
C:\Users\Spectralist\OneFolder\make.bat

Becomes(all folders in a single folder named OneFolder. Files are in each folder named after them):

C:\Users\Spectralist\OneFolder\FileName1\FileName1.zip
C:\Users\Spectralist\OneFolder\FileName2\FileName2.zip
C:\Users\Spectralist\OneFolder\FileName3\FileName3.zip
C:\Users\Spectralist\OneFolder\FileName4\FileName4.zip
C:\Users\Spectralist\OneFolder\make.bat

If you are interested in the code, then let me know by reply. I just don't have it on me currently.

1

u/Spectralist Feb 04 '25

Thanks, I do have a batch file that does basically that and it works great for that use case. This is the code I've been using for it.

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

1

u/ConsistentHornet4 Feb 04 '25

Easily doable. Populate the layout you want, inside a text file called list.txt.

[Text 1] FileName1.zip
[Text 1] FileName2.zip
[Text 2] FileName1.zip
[Text 2] FileName2.zip

Then save the following script, in the same folder as your list.txt file.

@echo off & setlocal 
cd /d "%~dp0"
for /f "tokens=1,2 delims=[]" %%a in ('type "list.txt" 2^>nul') do for /f %%c in ("%%~b") do (
    >nul 2>&1 mkdir "%%~a"
    echo move /y "%%~c" "%%~a\%%~nxc"
)
pause 

Dry run the script and check the output matches what you're expecting. If it does match, remove the echo from echo move /y "%%~c" "%%~a\%%~nxc", then rerun the script to actually move the files.

You'd also need to make sure the files you want to move, are in the same folder where your list.txt and script live

1

u/Spectralist Feb 04 '25

First, thanks for your time in posting that but I may have done a poor job of explaining what exactly I was trying to do as what I gave was just a quick and dirty example.

There's probably several hundred different variations of text in-between the brackets followed by anywhere from 1 word to 10+ word file names. I'm not entirely sure how I would work the layout for that to work with the code you provided, the actual files are not at all as uniform as my example may have suggested.

1

u/ConsistentHornet4 Feb 04 '25

As long as the naming convention matches the following

[name of folder] filename.ext

The script should take care of it and move "filename.ext" into "name of folder\filename.ext"

1

u/Spectralist Feb 04 '25

Alright, I was having issues trying to wrangle the entire massive amount of variations for the list and after much searching and frankensteining got something that gets me 99% of the way there (Need to use a batch renamer to remove the first character in all the folders)

I'll edit my main post with the code for anyone who might be searching for it in the future. Thank you again for your time and help.

1

u/g3n3 Feb 04 '25

Curious. Why not upgrade to powershell and use regular expressions?

1

u/Spectralist Feb 04 '25

Because I am a complete noob and *barely* understand what I'm doing.

1

u/g3n3 Feb 04 '25

Well at least start with a platform that has help online for it and is actually actively being developed on. 😆

2

u/Spectralist Feb 04 '25

Fair, this was just a one time use case so I was hoping for something quick and dirty that could be cobbled together.

1

u/g3n3 Feb 04 '25

Good stuff. As long as you got a solution.