Creating folders based on File Name.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KBRAND
Posts: 3
Joined: 11 Jan 2014 04:34

Creating folders based on File Name.

#1 Post by KBRAND » 11 Jan 2014 04:44

Hello All,

I am a newbie min this realm but my objective is to create a series of folders based on a file name.

For example:

1) User drags and drops file "1234_12345678_XXX-XXX.docx"
2) Batch files creates 4 folders named as follows
"1234_12345678_XXX-XXX"
"1234_12345678-A_XXX-XXX"
"1234_12345678-B_XXX-XXX"
"1234_12345678-C_XXX-XXX"

I am uncertain of how to pass the file name as a variable and manupulate the string to add the "A,B,C" to the other folder names. Any help would gretaly be appreciated. Thanks!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Creating folders based on File Name.

#2 Post by foxidrive » 11 Jan 2014 04:53

Code: Select all

@echo off
set "name=%~n1"
for /f "tokens=1,2,* delims=_" %%a in ("%name%") do (
md "%%a_%%b_%%c" 2>nul
md "%%a_%%b-A_%%c" 2>nul
md "%%a_%%b-B_%%c" 2>nul
md "%%a_%%b-C_%%c" 2>nul
)

KBRAND
Posts: 3
Joined: 11 Jan 2014 04:34

Re: Creating folders based on File Name.

#3 Post by KBRAND » 11 Jan 2014 05:15

Outstanding that is exactly what i was looking to do. If i wanted to expand on this and add a sub folder called "XYZ" to each of these folders would it look like this?:

@echo off
set "name=%~n1"
for /f "tokens=1,2,* delims=_" %%a in ("%name%") do (
md "%%a_%%b_%%c" 2>nul\XYZ
md "%%a_%%b-A_%%c" 2>nul\XYZ
md "%%a_%%b-B_%%c" 2>nul\XYZ
md "%%a_%%b-C_%%c" 2>nul\XYZ
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Creating folders based on File Name.

#4 Post by foxidrive » 11 Jan 2014 06:18

Almost. The 2>nul is only there to stop harmless error messages when the folders already exist.
It redirects the STDERR (standard error) stream which is represented by 2, from the console, to the NUL device - into a black hole.

Code: Select all

@echo off
set "name=%~n1"
for /f "tokens=1,2,* delims=_" %%a in ("%name%") do (
md "%%a_%%b_%%c\XYZ" 2>nul
md "%%a_%%b-A_%%c\XYZ" 2>nul
md "%%a_%%b-B_%%c\XYZ" 2>nul
md "%%a_%%b-C_%%c\XYZ" 2>nul
)

KBRAND
Posts: 3
Joined: 11 Jan 2014 04:34

Re: Creating folders based on File Name.

#5 Post by KBRAND » 13 Jan 2014 03:17

Works perfectly. Thank you!!!

Post Reply