1.

Solve : create folders based on part of filename?

Answer»

I would appreciate some help on a batch i am trying to make which takes a bulk of files in a folder (ebook/video/music collection) and create folders based on a variable length first part of the filename (i.e. author name).

NAMING convention is basically 2 parts: Author - Title
the separator for the 2 part file name is -

So far i am able to create a folder based on the filename and move all the files with the same name (regardless extension) into that folder.
This works great for video collection which puts the video,subtitle,coverimage etc in the same folder.
However it takes the full filename as a folder name.

Question: How can i name the created folder with only the part before the - ?

i.e.
ebook file:
Tolkien, J.R.R. - Lord of the Rings Part 01 - Fellowship of the Ring.epub

if not EXIST create the folder: Tolkien, J.R.R.
and move the above ebook into that folder.
Do the same for the next file in the directory.

Note: as above there can be more then one - in the filename, but the desired FOLDERNAME will always before the first -

Batch to create folder based on filename:

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

Any help will be appreciated !

regards,
Sunrayfor /f "delims=" %%A in ('dir /b /a-d ^| find /v "%~nx0"') do (
for /f "tokens=1* delims=-" %%B in ("%%A") do (
if not exist "%%B" md "%%B"
if not exist "%%B\%%A" move "%%A" "%%B"
)
)

Before the first Hyphen or before the 2nd hyphen?

Do you always have a space before and after each hyphen?Quote from: Salmon Trout on June 22, 2012, 01:22:10 PM

for /f "delims=" %%A in ('dir /b /a-d ^| find /v "%~nx0"') do (
for /f "tokens=1* delims=-" %%B in ("%%A") do (
if not exist "%%B" md "%%B"
if not exist "%%B\%%A" move "%%A" "%%B"
)
)
That works and takes care of not having to WORRY about the spaces.
I was trying to do it all in a single FOR LOOP. But the nested for loop definitely fixes all the concerns I had.Many thanks for the very quick reply and solutions.

I did study an example in a previous topic with a similar question which resulted in a batch file calling a VBScript.
As i recall also provided by Salmon Trout ;-)

Again your solution works like a charm !!
Now for me to break it down and actually learn from it ;-p

Thanks are given to both for your efforts.

You guys are great, keep it up !Quote from: Sunray on June 22, 2012, 02:29:27 PM
Now for me to break it down

REM use dir to GET each full filename, pipe through find to exclude this batch file)
for /f "delims=" %%A in ('dir /b /a-d ^| find /v "%~nx0"') do (
REM break filename into 2 tokens: 1. everything before the first dash 2. everything after it
REM we only need the first one
for /f "tokens=1* delims=-" %%B in ("%%A") do (
REM first token is author name
REM if folder of that name does not exist, create it
if not exist "%%B" md "%%B"
REM if this file is not already in the folder, move it there
if not exist "%%B\%%A" move "%%A" "%%B"
)
)


Discussion

No Comment Found