Small Batch Issues.

February 23, 2009 at 02:02:54
Specs: Windows XP, P4 / 2GB
Hello Experts,

Here are some issues I have been stuck on, in batch

i) Is there any du command in cmd ?
I did something like
for /f "tokens=1-3" %%a in, ('dir ^| findstr /r "bytes$"') do
but as you see its not consistent

ii) Is there any substring command in batch like the one we have in bash ${var:0:1}

iii) Is there any way to do decimal calculations in cmd?
e.g multiply 2.3 and 4.9 or something

iv) Also a way to find the length of a variable like ${#var}

I was wondering if there are some ways to do these without the need of any 3rd party utilities.

Thanks.


See More: Small Batch Issues.

Report •


#1
February 23, 2009 at 02:38:45
i) Pretty much what you have there. And what do you mean by "not consistent?" Although I'd probably use:
FOR /F "tokens=3" %%a IN ('DIR /S/A-D/-C ^| FINDSTR "bytes$"') DO SET size=%%a
@ECHO %size%

ii) %path:~0,1%

iii) Integer math only

iv) Sure, but it's not pretty and it tends to break.

SETLOCAL EnableDelayedExpansion
CALL :len %var%
@ECHO %len%
GOTO :eof

:len
SET len=1
SET _tmp=%*
:lenLoop
@IF "!_tmp:~-%len%!"=="%*" GOTO :EOF
@SET /a len += 1
@GOTO :lenLoop

EDIT: In lieu of man pages, you get information from a command by using a command's /? switch.


Report •

#2
February 23, 2009 at 02:47:10
1- I don't know the "du" command, but in the For statement posted you have to remve the comma (,) after the in keyword and maybe add a ( after the do.

2- Yes, type SET /? at prompt to get a quick tutorial about environment variables and substring manipulation.

3- Yes, again by the Set /A command (se above), but just for integers; you have to remove the decimal point, perform the arithmetics and then re- insert the separator keeping track of decimal. Just as you have integer decimal instructions only.

4- There is no such function, you have to compute the value by your own, using cunbersome loops and tricks.


Report •

#3
February 23, 2009 at 02:52:26
I should probably mention the other Windows scripting languages can do what you want without issues.

Report •

Related Solutions

#4
February 23, 2009 at 03:53:32
1>
You may have better luck searching for "File(s)" rather than bytes to avoid bytes free with razors one liner(unless of course that is what you want).

3>
As Ivo said you have to remove the decimal do the math then reinsert the decimal. I wrote a(rather long and cumbersome) script that does this along with work arounds for large numbers - although it can some times be a little slow - especially division. http://www.computing.net/answers/programming/cmd-large-number-work-around/17533.html skip straight to post #22 - No guarantees...

4>
Personally I use this for string length:

@ECHO OFF
Set var=string
:loop
if defined var (
set /a len+=1
set var=%var:~1%
goto loop
)
echo %len%
pause


Report •

#5
February 23, 2009 at 03:54:51
Thanks for the response. Those were really quick.

i) What I meant by not consistent was the fact that if a filename ends with "bytes" it shows up in the filtered output.

ii) (Razor) That is what I was looking for, thanks.

iii) (IVO) I will try to remove the decimal, calculate and put it again.

iv) (Razor) Cutting the characters and incrementing the variable was indeed a cool trick. Awesome.

And yes a million thanks for letting me know the /? switch. I was actually trying --help

Thanks Again.


Report •

#6
February 23, 2009 at 03:55:53
Judago: rather than bytes to avoid bytes free with razors one liner
I'd try it before commenting on it.

Report •

#7
February 23, 2009 at 04:05:01
With Razors one liner filenames the contain bytes are not a problem:

FOR /F "tokens=3" %%a IN ('DIR /S/A-D/-C ^| FINDSTR "bytes$"') DO SET size=%%a

You will always end up with the very last occurrence in the variable because for every time the string is found the corresponding token will be set to the variable - always leaving the last value intact. The output format of dir ensures that this cannot be a file name.


Report •

#8
February 23, 2009 at 04:08:09
Sorry Razor2.3 Kind of just glazed over $ end of line position......

Report •

#9
March 7, 2009 at 20:27:01
Just a small question,

How to do command substitution in DOS?

I have tried,

file=`dir /b | findstr abc.cap`
file='dir /b | findstr abc.cap'
file="dir /b | findstr abc.cap"

However none of them worked.


Report •

#10
March 8, 2009 at 04:08:19
for /f "delims=" %%a in ('dir /b | findstr abc.cap') do set file=%%a

FOR will be your workhorse in command scripts.


Report •

#11
March 9, 2009 at 22:07:08
I Intend to use command substitution many times, if I have to do for each time , I suppose I will run out of variables !

Also I wanted the output to print something like
The date now is `date +%F`
I tried something like,
for /f "tokens=1-4 delims=/ " %%a in, ('echo %date%') do echo The Date now is %%d-%%b-%%c
but its not working and I dont know why. Can you please check.
The output is simply coming blank i.e The Date now is

Thanks,


Report •

#12
March 10, 2009 at 01:54:07
E:\>for /f "tokens=2-4 delims=/ " %b in ("%date%") do echo The Date now is %d-%b-%c


Tue 03/10/2009  4:43:01
E:\>echo The Date now is 2009-03-10
The Date now is 2009-03-10

It's working for me.

Before that line, add this: set date. It should come back with Environment variable date not defined. If it doesn't, get rid of the variable by using, "set date=" (notice: no space after the '=').

Huzefa: I Intend to use command substitution many times, if I have to do for each time , I suppose I will run out of variables !
You might want to pick a different scripting language. VBScript is technically more universal than Command Scripts if you must stick with first party languages. If not, PERL is available for Windows.


Report •

#13
March 10, 2009 at 22:22:36
Miraculously its working for me also now, most probably you are correct, the date variable must have been set something but default, and I didnt even check it.

I will try to taste VBScript as well. Let me just sharpen a bit more in batch.

Don't mind me asking all these noobie questions but its just that I was completely dipped in the ocean of Bash and its been only a few weeks since I have first stepped on the Dry land of Batch :)

Thanks a Lot for the responses.


Report •

#14
March 22, 2009 at 00:07:43
Here's one another small one,

How can i bring the output to one line deliminated by spaces.
I mean if i do dir /b how can i get the output in just one line with filenames deliminated by spaces.
In other words for any output I just need to replace line breaks with spaces like `perl -pe 's/\n// '`

Thanks.


Report •

#15
March 22, 2009 at 00:22:44
If delayed expansion is enabled, you'd do something like this:
set line=
for /f "delims=" %%a in ('dir /b') do set line=!line! %%a

If not, it's still doable, albeit . . . well . . .
set line=
for /f "delims=" %%a in ('dir /b') do CALL :loop %%a
goto :postLoop
:loop
set line=%line% %*
goto :eof
:postLoop
. . .Yeah.

Report •

#16
March 22, 2009 at 23:05:20
Yup,
Thats a neat trick indeed. Works perfect.
Thanks.

Report •

#17
March 30, 2009 at 01:32:37
ok, one more,

How do i echo the character %
I wanted to echo out `date +%B`
I tried to escape it by ^ and also \ but both i suppose didnt work.

Thanks.


Report •

#18
March 30, 2009 at 01:45:06
%%
This is why FOR loops use %%a in scripts and %a out of scripts.

Report •

#19
March 30, 2009 at 01:57:57
Wow, thanks for the lifesaver,
I was trying since 2 hours.

Report •

#20
April 5, 2009 at 00:48:42
Hello again, one question please,

How can I SSH a few commands from Batch script.
I found one free utility from the makers of putty called plink.exe
but still if possible I want to avoid using any third-party utility.
Is there some way out?

Thanks.


Report •

#21
April 5, 2009 at 06:34:53
There is no native SSH program for Windows.

Report •

#22
April 6, 2009 at 03:50:22
I will use plink then,

Thank You.


Report •


Ask Question