Batch Folder_Set in For as Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lmstearn
Posts: 52
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Batch Folder_Set in For as Variable

#1 Post by lmstearn » 07 Dec 2014 15:27

Hi there to all the dedicated DOStippers reading this forum!

I posted this at Stackoverflow but got absolutely no answer to date. :shock:

The conundrum is the "folder_set", or "File_set" or folder/file lists part of a "For" command to be referred through a variable. Suppose we wish to define a folder set in the "For /D" by:

Code: Select all

SET "FOLDERSET=X* Y* Zee*"
and hope that something like


Code: Select all

for %%B in (!FOLDERSET!) do (

will produce the miracle of iterating through the arrays as actual folders rather than just a string array. It does appear to work when just using

Code: Select all

SET "FOLDERSET=*"

or

Code: Select all

SET "FOLDERSET=S*"

for folders beginning with "S" or

Code: Select all

SET "FOLDERSET=S* G*"

again for folders beginning with "S" with G* not iterated, but the original problem remains to set up the appropriate delimiters for above wildcard list. Any way possible?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch Folder_Set in For as Variable

#2 Post by Squashman » 07 Dec 2014 15:38

Are you saying you want to use a specific delimiter for your File or Folder set?

lmstearn
Posts: 52
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Batch Folder_Set in For as Variable

#3 Post by lmstearn » 07 Dec 2014 18:39

Squashman wrote:Are you saying you want to use a specific delimiter for your File or Folder set?

Yeah, well, anything to get it working really. But it's going to be awkward when just working with For /D.
It interprets the first non-spaced wildcard "X*" in FOLDERSET but ignores Y* Zee* when separated by space. Tried commas, semicolons etc no diff.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch Folder_Set in For as Variable

#4 Post by ShadowThief » 07 Dec 2014 19:13

What about splitting up the string first?

Code: Select all

@echo off

set "folderset=S* G* Z*"

for /f "tokens=* delims= " %%A in ("%folderset%") do (
   for /d %%B in (%%A) do echo %%B
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch Folder_Set in For as Variable

#5 Post by dbenham » 07 Dec 2014 21:22

As I've posted in my StackOverflow answer, I don't understand what your problem is. It works just fine for me:

Code: Select all

@echo off
setlocal
set "folderset=s* x*"

echo Using normal expansion:
for /d %%F in (%folderset%) do @echo %%F
echo(

echo Using delayed expansion
setlocal enableDelayedExpansion
for /d %%F in (!folderset!) do @echo %%F

--OUTPUT--

Code: Select all

Using normal expansion:
SETA
size
snake
svnsave
svntest
xml
xyz

Using delayed expansion
SETA
size
snake
svnsave
svntest
xml
xyz


Dave Benham

lmstearn
Posts: 52
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Batch Folder_Set in For as Variable

#6 Post by lmstearn » 07 Dec 2014 21:39

That's a helpful hint. Here's the code (creating a directory somewhere called MYDIRFOUND):

Code: Select all

SET "FOLDERSET=D* G* S* T*"
Setlocal EnableDelayedExpansion

for %%T in (B C D E F G) do (
if exist %%T: (


cd /D %%T:
cd ..\..

REM Check this later
REM for /f "tokens=* delims= " %%C in ("%FOLDERSET%") do (

for /D %%C in (!FOLDERSET!) do (
REM This works beautifully!

Set "MYTEMP=%%~dpC"
If exist !MYTEMP! (
cd !MYTEMP!
echo !MYTEMP!
REM Unfortunately just echoes the drive letters of T
pause
)
for /D /R %%K in (%%C) do (


Set MYTEMP=%%K
SET _MYDIR=!MYTEMP:MYDIRFOUND=!

IF NOT !MYTEMP!==!_MYDIR! (
call SET "_MYDIR=%%~K"
echo !_MYDIR!
pause
goto MYDIRFOUND
)
)
)

REM DRIVE LOOP AND EXIST
)
)
goto eof
:MYDIRFOUND
echo success
pause


Something missed?
Last edited by lmstearn on 08 Dec 2014 03:03, edited 5 times in total.

lmstearn
Posts: 52
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Batch Folder_Set in For as Variable

#7 Post by lmstearn » 07 Dec 2014 21:42

@Dave

The problem is when the nonexistent folders are first e.g. ""folderset=ZZ* XX* U*"
I could never get it doing U*

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch Folder_Set in For as Variable

#8 Post by dbenham » 07 Dec 2014 22:44

lmstearn wrote:@Dave

The problem is when the nonexistent folders are first e.g. ""folderset=ZZ* XX* U*"
I could never get it doing U*

:? I still don't see the problem.

I don't have any Q* folders in my directory, and everything works fine:

Code: Select all

@echo off
setlocal
set "folderset=q* s* x*"
for /d %%F in (%folderset%) do echo %%F

--OUTPUT--

Code: Select all

SETA
size
snake
svnsave
svntest
xml
xyz


Dave Benham

lmstearn
Posts: 52
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Batch Folder_Set in For as Variable

#9 Post by lmstearn » 07 Dec 2014 23:04

Well it's working now. Sort of. Dunno what's going on. All the above code works like a charm now. The one I posted has now the FOR /D back in.
Thanks!

Edit: what's slightly hackling is that with the following:

Code: Select all

Set "MYTEMP=%%~dpC"
If exist !MYTEMP! (
cd !MYTEMP!
echo !MYTEMP!


The above code only echoes the drive. Doesn't echo any of the iterated folders
Edit: Aargh %%~dpC is not needed as %%C is a path already!

Post Reply