Sponsored Content
Top Forums Shell Programming and Scripting Setting Variables WITHIN For Loop in DOS Command Shell Post 302498027 by ProGrammar on Friday 18th of February 2011 07:04:37 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
CD(1)							    BSD General Commands Manual 						     CD(1)

NAME
cd -- change working directory SYNOPSIS
cd directory DESCRIPTION
Directory is an absolute or relative pathname which becomes the new working directory. The interpretation of a relative pathname by cd depends on the CDPATH environment variable (see below). ENVIRONMENT
The following environment variables affect the execution of cd: CDPATH If the directory operand does not begin with a slash (/) character, and the first component is not dot (.) or dot-dot (..), cd searches for the directory relative to each directory named in the CDPATH variable, in the order listed. The new working directory is set to the first matching directory found. An empty string in place of a directory pathname represents the current directory. If the new working directory was derived from CDPATH, it will be printed to the standard output. HOME If cd is invoked without arguments and the HOME environment variable exists and contains a directory name, that directory becomes the new working directory. See csh(1) for more information on environment variables. The cd utility exits 0 on success, and >0 if an error occurs. SEE ALSO
csh(1), pwd(1), sh(1), chdir(2) STANDARDS
The cd command is expected to be IEEE Std 1003.2 (``POSIX.2'') compatible. BSD
June 5, 1993 BSD
All times are GMT -4. The time now is 03:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy