Make a folder based on the filename

Microsoft Windows xp home edition eng 3p...
February 18, 2010 at 02:40:36
Specs: Windows XP
Hi
I'm trying to create a batch file that will create a folder based on the first 5 letters of a filename and then move the file into that new folder.

I've tried using this from another post (http://www.computing.net/answers/programming/make-directories-from-file-name-and-move-file/19988.html)

@echo off
cls
setlocal enabledelayedexpansion

for /f "delims=" %%A in ('dir /a-d /b /tc "*.csv"') do (
md "%%~nA"

set file=%%~tA

set file=!file:~6,4!!file:~3,2!!file:~0,2!_!file:~11,2!!file:~-2!.csv

copy "%%A" "%%~nA\!file!"> nul
)


but this creates a unique folder for each file and then copies/moves it there.

Any ideas on how to edit this batch file so that the folders use the first 5 letters of the filename, and this way files with similar names will end up in the same directory ?

Thanks a lot !


See More: Make a folder based on the filename

Report •


#1
February 18, 2010 at 04:19:30
There is another thread which almost exactly meets your need. But I have no idea where it is.

For future reference, if you put a link on a line by itself, no parens, it will probably be clickable.

===================================
:: not tested

@echo off & setLocal EnableDELAYedeXpansion

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


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 18, 2010 at 10:21:03
That works beautifully :->

I've added a line to change the directory, otherwise it moves the batch file as well !

@echo off & setLocal EnableDELAYedeXpansion

cd TargetDirectory

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

Many thanks !


Report •

Related Solutions


Ask Question