Page 1 of 1

Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 09 Jun 2019 21:42
by terryhenderson
Hi all,

I need to create three subfolders under any subfolder that contains the word "classified" in its name.
I have the following code that does the job but under a fixed name subfolder which is "test".

Code: Select all

@echo off
setlocal
 
set folder=C:\My Documents\test
 
for /F "tokens=*" %%G in ('dir "%folder%" /A:D /B') do (
 if not exist "%folder%\%%G\subfolder1" md "%folder%\%%G\subfolder1"
 if not exist "%folder%\%%G\subfolder2" md "%folder%\%%G\subfolder2"
 if not exist "%folder%\%%G\subfolder3" md "%folder%\%%G\subfolder3"
)
I tried to change the

Code: Select all

set folder=C:\My Documents\test
to

Code: Select all

set folder=C:\My Documents\*classified*
but it didn't work

Can I get some precious assistance with this question, please?

Many thanks in advance ..

T.

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 06:17
by aGerman
Try

Code: Select all

md "%%~fG\subfolder1"
You may have to PUSHD to the parent folder first. Otherwise there is a risk that modifier ~f doesn't expand to the right directory.

Steffen

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 06:49
by terryhenderson
Thank you very much Steffen for your reply. In fact, I find your answer above my knowledge level.
I just want to create subfolders under any subfolder that has "classified" as part of its name.

Once again, thank you very much.

T.

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 07:00
by aGerman
Untested:

Code: Select all

@echo off
setlocal

set "parentfolder=C:\My Documents"
set "pattern=*classified*"

pushd "%parentfolder%"
for /F "tokens=*" %%G in ('dir "%pattern%" /A:D /B') do (
 if not exist "%%G\subfolder1" md "%%G\subfolder1"
 if not exist "%%G\subfolder2" md "%%G\subfolder2"
 if not exist "%%G\subfolder3" md "%%G\subfolder3"
)
popd

pause
Steffen

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 07:24
by terryhenderson
I'm more than grateful Steffen for your solution. However, I tested it, but unforturnately I couldn't make it work. Your code was pasted in a batch file at it is, and tested on C:\My Documents directory that has all subfolders with word "classified" as part of their names, but nothing was changed.

What do you think I made wrong?

Thank you very much.

T.

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 07:36
by aGerman
I created folder "C:\My Documents" and subfolders "barclassifiedfoo", "foobar", and "fooclassifiedbar" inside of it. Then I run the above code. It created "subfolder1", "subfolder2", and "subfolder3" in both "barclassifiedfoo" and "fooclassifiedbar" as expected.

Steffen

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 10:01
by terryhenderson
Thank you very much Steffen for your precious code. I knew there's something wrong from my side. I will test it again "more carefully" and I'm sure it will work.
I consider this question solved by you Steffen. Please accept my deep respcet and gratitude.

All the best.

T.

Re: Modifying batch to accommodate for any subfolder with "classified" in its name

Posted: 10 Jun 2019 10:38
by aGerman
Check your paths for typos, make sure there are no characters in the folder names that are not supported (preferably use ASCII only), make sure the paths exist (e.g. don't mess up "C:\My Documents" and "%userprofile%\Documents").

Steffen