Batch to move files based on partial name

April 24, 2011 at 18:40:38
Specs: Windows 7
I have folder (let's say it's e:\images) with thousands of pictures, all with the following naming conventions:


Basically, the number in the name specifies what the image is. s indicates that it is the first image of that particular item, s01 is the second, s02 the third, and so on. I'm not sure why they were named this way, but I need to move them all into their own folders (creating that folder if it does not exist already) based on the number in the file name and am at a loss as to how to do this. Does anyone know how to create a batch file for this? This is something I used to be able to figure out, but haven't touched a batch file in years and have no idea where to begin anymore. I'm no coder, so anything I've ever done has been trial and error, and I don't have tons of time to spend on this.

See More: Batch to move files based on partial name

Report •

April 25, 2011 at 09:25:55
WARNING: batch script not tested.

@echo off & setlocal EnableDelayedExpansion
cd /D E:\images
for %%j in (*.jpg) do (
  set fname=%%~nj
  set fname=!fname:*_=!
  if not exist !fname! md !fname!
  move %%j !fname!

Report •

April 25, 2011 at 14:00:44
Thank you! Worked like a charm.

Report •

Related Solutions

Ask Question