Setting Variables WITHIN For Loop in DOS Command Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting Variables WITHIN For Loop in DOS Command Shell
# 1  
Old 02-18-2011
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. Smilie
# 2  
Old 02-18-2011
(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:
Code:
for /? | more

Its easier to read if you redirect the output to a file an read it with notepad.
Code:
for /? > for_help.txt
notepad for_help.txt


Last edited by methyl; 02-18-2011 at 07:21 PM..
# 3  
Old 02-18-2011
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
# 4  
Old 02-18-2011
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.
# 5  
Old 02-18-2011
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HELP - loop a curl command with different variables from input file

Hi guys! Kind of new to bash scripting and now I'm stuck. I need to curl with these variables: "{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }" and my input_file.txt contains server001 10.10.10.01 serverpool1 80... (4 Replies)
Discussion started by: yort
4 Replies

2. Shell Programming and Scripting

Shell Script for Setting Env Variables

Hello All. Good Afternoon. I need one small help regarding setting of env variables for a particular host by getting it from the DB. For ex : 1. I am using LOCALHOST. 2. When I run a ./hostset.sh it should pick up the Oracle home details from associated DB and set it. Please... (1 Reply)
Discussion started by: PavanPatil
1 Replies

3. Shell Programming and Scripting

Convert for command from DOS to SHELL

Well, this command has served me quite well under DOS for %%X in (*.txt) do COMMAND however in linux it just outputs: "./install.sh line 1: '%%x': not a valid identifier. Ideas ? Thanks in advance (2 Replies)
Discussion started by: pasc
2 Replies

4. Shell Programming and Scripting

Setting a variable using variables in a loop

Hi, I am having a bit of trouble with the below code: file=/path/to/file for i in 03 06 07 21; do if ; then eval count$i=`grep -c word $file-$i` fi done Totalcount=0 for i in 03 06 07 21; do if ; then echo $count$i variable not exist; else Tcount=`expr $Tcount + $count$i`; fi... (3 Replies)
Discussion started by: brunlea
3 Replies

5. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

6. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

7. UNIX for Dummies Questions & Answers

Setting shell variables with ":" syntax

I nedd help on the below highlited one. can any one explain on this,How its work in shell scripting.. then that will be very great full... L_ABCD=${L_DBG:=N} Thanks Muddasas (3 Replies)
Discussion started by: muddasani
3 Replies

8. UNIX for Dummies Questions & Answers

For loop control with two variables in csh shell

Hi All How can i control for loop with two different variables in csh shell Regards Nikhil (1 Reply)
Discussion started by: Nikhilindurkar
1 Replies

9. Shell Programming and Scripting

while read loop; scope of variables (shell)

If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results. For example: # Pipe command into while-read loop count= ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies

10. UNIX for Dummies Questions & Answers

Setting up shell variables

Hi everyone, I am trying to set up the .profile for a user I have just created. In trying to set up the shell variables, I want to make the shell be korn shell (default shell i believe is Borne shell), so, this is what I did: SHELL=/usr/bin/ksh export SHELL Whenl executing the .profile,... (1 Reply)
Discussion started by: rachael
1 Replies
Login or Register to Ask a Question