1

I want to find a folder whose name starts fld-. FOR lets me use wildcards:

for /D %%f in ("fld-*") do set FOLDER=%%f

However, with the following folders:

.fld-blah\
fld-blahblah\
afld-blah\

it also (somewhat unexpectedly) finds the one starting, ".".

I decided to test the first char using the DOS LEFT operator. This doesn't seem to work on FOR variables (the %%F here), so I need to assign it to a normal variable first. Running the following .bat file twice (you'll see why):

@echo off 
for /D %%f in ("fld-*") do (
set TEST=%%f
echo f:         %%f
echo TEST:      %TEST%
echo TEST[0,1]: %TEST:~0,1%
echo.
)

produces the following output:

C:\Users\Bob\Desktop\test>test.bat
f:         fld-blahblah
TEST:
TEST[0,1]: ~0,1

f:         .fld-blah
TEST:
TEST[0,1]: ~0,1


C:\Users\Bob\Desktop\test>test.bat
f:         fld-blahblah
TEST:      .fld-blah
TEST[0,1]: .

f:         .fld-blah
TEST:      .fld-blah
TEST[0,1]: .

TEST appears only to be assigned after the FOR (...) group of commands completes.

So. Can anyone tell me:

  1. Whether I can make the for wildcard ignore folder names that start with a dot?
  2. Whether there is syntax I can use to apply the LEFT operator to the for variable, %%F?
  3. Whether there is a way I can assign a variable AND use it within a (...) loop
  4. Whether there is some other method I can use to achieve my goal (within DOS, I know I can use Powershell, cygwin, etc.)?
  • 2
    What exactly is “hidden”? ಠ_ఠ – Synetech Aug 30 '13 at 16:34
  • I would have used XYplorer (www.xyplorer.com), if I were you, it has a very delicate built-in scripting language. (Just as a suggestion) – Jacob Rabinsun Aug 30 '13 at 16:35
  • @Synetech Sorry I'm in Linux mode - the "." folder is what I meant (of course it's not hidden in Windows). I'll change the title. – Bob Sammers Aug 30 '13 at 16:40
  • Ah okay; that completely changes things. I though you were asking something else. It is strange though because while a leading period would make the folder nameless and consist of only an extension, you hadn’t used any of the ~ switches, so it should still have worked anyway. And yes, delayed-expansion is easy to forget and frustrating when you do. :-/ – Synetech Aug 30 '13 at 16:49
0

Been on this for hours and it's just dawned on me what I needed to Google for.

The answer is to use delayed variable expansion, so the test.bat script becomes:

@echo off 
SETLOCAL ENABLEDELAYEDEXPANSION
for /D %%f in ("fld-*") do (
set TEST=%%f
echo f:         %%f
echo TEST:      !TEST!
echo TEST[0,1]: !TEST:~0,1!
echo.
)

and behaves as required:

C:\Users\Bob\Desktop\test>test.bat
f:         fld-blahblah"
TEST:      fld-blahblah
TEST[0,1]: f

f:         .fld-blah"
TEST:      .fld-blah
TEST[0,1]: .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.