Basic compound variable problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mark One
Posts: 1
Joined: 30 Jun 2010 17:10

Basic compound variable problem

#1 Post by Mark One » 30 Jun 2010 17:21

Hi, I'm a fairly new user. :mrgreen:

I have a loop that creates some consecutive Variables.

set 1=Hello
set 2=my name is
set 3=mark, and I
set 4=have a DOS problem
set 5=I'm hoping
set 6=you can help me with

The number of variables created is NOT fixed
At the end of the loop, variable %L% is the last variable number

L could be any Number between 5 and infinity


How do you return the value of the last variable but one?

Eg, for the above, I can return:
5

but I need to return:
I'm hoping


The bigger picture:
The variables are lines from a .txt file created by dir.
What I want to do is return the line that gives the size of the directory, then I need to manipulate that, leaving just the number of bytes (I think I can do that).
What I'm looking to do with this batch file is add up and then compare directory sizes. This is the only way I can think of doing it.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Basic compound variable problem

#2 Post by aGerman » 30 Jun 2010 17:49

You don't need all these variables. Use a simple FOR loop:

Code: Select all

@echo off &setlocal
for /f "usebackq tokens=3" %%i in ("your.txt") do (
  call set "size=%%x%%"
  set "x=%%i"
)
echo %size%
pause



You could also process the DIR command directly

Code: Select all

@echo off &setlocal
for /f "tokens=3" %%i in ('dir /-c "C:\somewhere"') do (
  call set "size=%%x%%"
  set "x=%%i"
)
echo %size%
pause


Regards
aGerman

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Basic compound variable problem

#3 Post by avery_larry » 02 Jul 2010 11:29

for /f "tokens=3" %%a in ('dir /-c "c:\directory"^|find /i "bytes free"') do set sizeinbytes=%%a

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Basic compound variable problem

#4 Post by aGerman » 02 Jul 2010 11:41

@avery_larry

Do you know where Mark One comes from? Is there a "bytes free" in his DIR output?

Regards
aGerman

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Basic compound variable problem

#5 Post by avery_larry » 02 Jul 2010 11:57

One can hope that they know how to change for their language, and one can also hope that they know how to ask for additional help if this has to work for multiple languages. Besides -- do you know it's token #3 in every language?

AND -- looking at the OP -- I should have warned them that using this method and then adding together multiple directories will only work up to a total of about 2GB because of the limits of DOS math. (Assuming they would use "set /a" to add things before comparing them.)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Basic compound variable problem

#6 Post by aGerman » 02 Jul 2010 12:46

Of course you're right. The reason for my hint is that I have to do with American settings at work and German settings at home. Thats why I try to avoid "language sensitive" scripts.
But indeed, I cannot guarantee that it even works for Russians, Chinese, Japanese...

Regards
aGerman

Post Reply