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!!
Creating folders based on File Name.
Moderator: DosItHelp
Re: Creating folders based on File Name.
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
)
Re: Creating folders based on File Name.
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
)
@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
)
Re: Creating folders based on File Name.
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.
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
)
Re: Creating folders based on File Name.
Works perfectly. Thank you!!!