1.

Solve : For Loop and Variables not appearing (another one???)?

Answer»

Well, I gave up after 25 screens of not finding my answer.
I tried the search at the top, but always returns 0 matches???
what I want to do:
scan a directory for all directories in that folder.
(dir names are of the form aaaa_bbbb_cccc_dddd_eeeee)
parse the dir name getting tokens 2 and 3 (b & c)
assign tokens 2 and 3 to variables

test code I have been tearing my hair out with (how do you tear hair out with test code?). Left in some debugging stuff

cls
setlocal
@echo off
setlocal enabledelayedexpansion
set MFG =

for /D %%F IN (c:\new_data\*.*) DO (
echo %%~nF
for /f "tokens=1,2,3 delims=_ " %%a in ("%%~nF") do (
echo.
echo %%a
echo %%b
echo %%c
echo %%~nF
echo.
set MFG = %%b
echo MFG = %MFG%
echo MFG = !MFG!
)
)
The results from this are:

C:\New_Data>cls

C:\New_Data>setlocal
Data_AAAAAA_AdirA_xyz

Data
AAAAAA
AdirA
Data_AAAAAA_AdirA_xyz

MFG =
MFG =
Data_NEWMFG_NEWPBM_NDIR

Data
NEWMFG
NEWPBM
Data_NEWMFG_NEWPBM_NDIR

MFG =
MFG =
SSSSSS_Automation_OLD_again

SSSSSS
Automation
OLD
SSSSSS_Automation_OLD_again

MFG =
MFG =
the end

---------------------------------------------------------------------------
Can't seem to get the data into variable MFG?

any help appreciated. thanks
Matt.
Nesting FOR loops can produce unexpected results. This may work...used a different technique to get the directories. Note: remove the /s switch if you don't want all the sub-directories.

Code: [Select]for /f "tokens=1-3 delims=_" %%a in ('dir /a:d /s /b c:\new_data') do (
echo %%b
echo %%c
set mfg=%%b
echo %mfg%
)

Let us know how it goes

I stole the above line from another poster!!!!No dice, but thanks for reply.

using:

cls
@echo off
setlocal enabledelayedexpansion
set NewData = C:\NEW_DATA\

for /f "tokens=1-3 delims=_" %%a in ('dir /a:d /b %NewData%') do (
echo %%b
echo %%c
set MFG = %%b
echo MFG = %MFG%
)
echo the end

output is:


C:\New_Data>cls
Abbot
AdirA
MFG =
NEWMFG
NEWPBM
MFG =
Automation
OLD
MFG =
the end
-----------------------------------
Yikes!!!I guess if you declare delayed expansion you need to use it.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set NewData = C:\NEW_DATA\

for /f "tokens=1-3 delims=_" %%a in ('dir /a:d /b %NewData%') do (
echo %%b
echo %%c
set MFG=%%b
echo MFG = !MFG!
)
echo the end

Who knew?

FYI: On a set statement punctuation is meaningful. A space is a REAL character.I got the same results, nada. As U can C, I'm going NUTTY!

Matt.Matt,

Not sure what to say but the code posted works fine on this machine (XP Home). This has happened before with some little obscure setting being the difference between working and not. Perhaps someone else will COME along to help.

THanks for the help though. I may just write a VB for it. I thought I could do it in a couple hrs, but hit this snag.

Later
Matt.You were ABSOLUTELY RIGHT! Your note:

FYI: On a set statement punctuation is meaningful. A space is a real character.

was the problem. I was doing ' = ' instead of '=' on a set.

(All i'll ever need is 640k, yeah, right!!!)

thanks again!
Matt



Discussion

No Comment Found