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

42124b1aa1.txt
42124b1ab2.txt
42124b2ac2.txt

would become these two directories with associated files:

42124b1
42124b2

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 •


#1
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

M2


Report •

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

Report •

Related Solutions


Ask Question