Setting Variables WITHIN For Loop in DOS Command Shell

I'm wondering if any of you could lend an assist with a small problem.

First, I'm under the impression I need to use Delayed Environment Variable Expansion (DEVE), based on other things I've read across the web.

Summary: trying to use command shell (cmd.exe) in XP sp3 (if that's relevant) to write a small script that will rename files.

So I require a for-loop that stores a filename for each iteration (simple enough, just use the %A in:

SETLOCAL ENABLEDELAYEDEXPANSION
for /F "usebackq" in (`dir /b C:\directory\*`) do (
set origname=%A
echo %origname%
echo !origname!
)

which results in the literal strings "%origname%" and "!origname!" being printed to the screen, no variable expansion is taking place except in the line where I attempt to set the variable origname...the %A expands to the iteration of the `dir /b C:\directory\*` output which is being processed at the time.

What am I doing wrong?! From what I can tell, by other people's posts online, it seems like maybe DEVE isn't working for me, if !origname! won't expand to the value of %A after it's set in the first command of the command set for the loop operation.

I can't just use %A either, because I require string manipulation of the value %A and command shell is lame and can only perform string manipulaton of variables with the convention %var%, (e.g. %var:~5,1% which would effectively extract the 6th char from the value of %var%.

I intend to perform checks of the 8th character in the %origname% variable and compare it against if test conditions to determine how to handle the file from there.

Make sense?!

I hope somebody here is familiar with DOS and can help me troubleshoot this. It's frustrating and I feel like I have found a cause in proving I'm smarter than esoteric DOS programming conventions. :wall:

(Post of .BAT file withdrawn because it does not work as expected).

In case you don't know this, there is extensive help text available at the cmd prompt:

for /? | more 

Its easier to read if you redirect the output to a file an read it with notepad.

for /? > for_help.txt
notepad for_help.txt

I know that, the sample of code I posted was copied from the interactive command-line, hence single-% denoted variables. I've been reading the help file for "for /?" and it's pretty worthless and borderline esoteric!

I'm not certain whether my DEVE is working correctly.

Thanks for your suggestion though.

---------- Post updated at 08:04 PM ---------- Previous update was at 06:53 PM ----------

I'm not sure what worked so differently for me this time, but I was able to successfully execute the code above as intended using the following syntax. This is for your reference.

at the interactive command-line, you'll use single-%-denoted iteration variables, but in a batch script, you'll use double-%-denoted variables for the iteration statements/command sets. I'll demonstrate batch-script form, italicized commentary is FYI, the bold-print characters are the actual script commands.

SETLOCAL ENABLEDELAYEDEXPANSION
this line turns on delayed environment variable expansion, necessary to store and expand variables set within the for loop. Otherwise, variable values are expanded as the command is parsed (before execution)

for /f "usebackq" %%A in (`dir /b C:\directory\`) do (
this first line is using %%A as the iterator variable, storing each line of output from dir /b (the b option is equivalent to ls -1 in UNIX)

set origname=%%A
stores the value of %%A into a variable (origname) that can be manipulated later in the loop iteration

set eighth=!origname:~7,1!
this stores the eighth character of origname variable, the variable must now be expanded using exclamation points for flags instead of percent-symbols. But is still subject to all the rules of regullar command-shell variables, including string manipulation.

if !eighth!==3 (
begin if condition
move /Y C:\directory\!origname! C:\directory\output\!origname:~0,7!5!origname:~8,6!
based on result of if comparison, moves original file to a parsing directory for a program that uses 8th character to process based on a rule set, as you can (or cannot) tell--the original filename is expected to be a 14-character string and we're modifying the 8th character to conform to a ruleset for processing
) else (
move /Y C:\directory\!origname! C:\directory\output\!origname:~0,7!3!origname:~8,6!
)
)

The else condition is a coverall for files that do conform to a ruleset but erred for one reason or another and thus processes them using a different ruleset.

Hopefully this makes sense to you. Initially, when the command-line was expanding the variables, it was printing some funky stuff. It seemed to just start working, but who knows--I've been staring at this damm screen so long.

ProGrammar

In your posted example the for line was missing the %A (or %%A if we are in a Batch File) variable. Hence assuming a more basic syntax issue.

oh, my fault--I see whatchu mean now. I overlooked that...I was wondering why you posted that; just a minor oversight on my part. The way you explained it previously was kinda ambiguous.

Thanks for your inputs anyway!

Have a good one!