Create Directory from Partial Filename

Microsoft Windows xp professional w/serv...
November 23, 2009 at 10:56:56
Specs: Windows XP
I have a directory with thousands of files with similar filenames. I want to use the first 7 characters in a filename to create a subdirectory and move the file into it. So


would become these two directories with associated files:


The batch below gives me unique directories based on the whole name, not the first 7 characters.

@echo off
for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
if exist "%%a" move "%%~na.*" "%%~na"

See More: Create Directory from Partial Filename

Report •

November 23, 2009 at 11:08:03
not tested:
@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set dname=%%~Na
set dname=!dname:~0,7!
if not exist !dname! md !dname!
move %%a !dname!

Helping others achieve escape felicity


Report •

November 23, 2009 at 11:13:37
Perfect! Thanks M2!

Report •

Related Solutions

Ask Question