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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
terryhenderson
Posts: 13
Joined: 22 Jul 2018 04:59

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

#1 Post by terryhenderson » 09 Jun 2019 21:42

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.

aGerman
Expert
Posts: 3623
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 10 Jun 2019 06:17

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

terryhenderson
Posts: 13
Joined: 22 Jul 2018 04:59

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

#3 Post by terryhenderson » 10 Jun 2019 06:49

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.

aGerman
Expert
Posts: 3623
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 10 Jun 2019 07:00

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

terryhenderson
Posts: 13
Joined: 22 Jul 2018 04:59

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

#5 Post by terryhenderson » 10 Jun 2019 07:24

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.

aGerman
Expert
Posts: 3623
Joined: 22 Jan 2010 18:01
Location: Germany

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

#6 Post by aGerman » 10 Jun 2019 07:36

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

terryhenderson
Posts: 13
Joined: 22 Jul 2018 04:59

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

#7 Post by terryhenderson » 10 Jun 2019 10:01

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.

aGerman
Expert
Posts: 3623
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 10 Jun 2019 10:38

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

Post Reply