Extract only numeric content without spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jorgegs
Posts: 22
Joined: 07 Apr 2016 22:56

Extract only numeric content without spaces

#1 Post by Jorgegs » 29 May 2016 12:42

Hello friends
I have a result in two var like this

Files: 134571
Directories: 3

Which I get with the help from the friend Squashman in this fórum with this expresión
dudir.exe -n -q |findstr /b /i "Files: Directories:" >file2.txt
or with
FOR /f "tokens=1*delims=:" %%G in ('findstr /n "^" D:\1G\GORIGEN.txt') do if %%G equ 1 SET jgGLINEA=%%H

This was very helpful, but now I need ONLY the numbers, without 0 or spaces on the left, result in the var %jgGLINEA%.
The numeric result content can have a variable values and diferent length.

I tryed with, for, find, tokens, and delims, but without a valid result.

Can some one help me in this new occasion ?

Thanks in advance for your time and knowledges.
Jorgegs from Spain

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract only numeric content without spaces

#2 Post by foxidrive » 29 May 2016 13:20

Your question is not as clear as it could be, although it sounds simple.

The subject says you want numeric content, and your question says you do not want zeros - and your example doesn't show zeros.

Are you trying to get the number of files and directories in a subdirectory branch? or just one folder?

Dudir is not a default command...

Jorgegs wrote:I have a result in two var like this

Code: Select all

Files:           134571
Directories:       3

but now I need ONLY the numbers, without 0 or spaces on the left, result in the var %jgGLINEA%.

Jorgegs
Posts: 22
Joined: 07 Apr 2016 22:56

Re: Extract only numeric content without spaces

#3 Post by Jorgegs » 29 May 2016 13:45

Thanks for your answer
Sorry for the examples

Dudir.exe is a renamed program by me, which gives me the var result using

FOR /f "tokens=1*delims=:" %%G in ('findstr /n "^" D:\1G\GORIGEN.txt') do if %%G equ 1 SET jgGLINEA=%%H
FOR /f "tokens=1*delims=:" %%G in ('findstr /n "^" D:\1G\GORIGEN.txt') do if %%G equ 2 SET jgGLINEB=%%H
FOR /f "tokens=1*delims=:" %%G in ('findstr /n "^" D:\1G\GORIGEN.txt') do if %%G equ 3 SET jgGLINEC=%%H
FOR /f "tokens=1*delims=:" %%G in ('findstr /n "^" D:\1G\GORIGEN.txt') do if %%G equ 4 SET jgGLINED=%%H

Result for the Files, the content of var jgGLINEA
Result for Directories, the content of var jgGLINEB
Result for Size, the content of var jgGLINEC
Result for Size on disk, the content of var jgGLINED

Files: 34589
Directories: 45
Size: 87.398 bytes
Size on disk: 87.456 bytes

This Works correctly.

But I need only the numbers without the names Files: or Directories: or Size or Size on Disk at the beginning of each line
and without spaces on the left side of the number,
and without the Word "bytes" at the end, in case of the two optios of Size. But OK with the ". for thousands".
The size of the var can have a different length.

This will means in the example, var A, B ,C and D, only
34589
45
87.398
87.456
In 4 different var. Each with every different result.

Excuse my old english
Thanks for your help
Jorgegs

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract only numeric content without spaces

#4 Post by foxidrive » 29 May 2016 22:31

This works here with your data in file.txt

Files: 34589
Directories: 45
Size: 87.398 bytes
Size on disk: 87.456 bytes


Code: Select all

@echo off
for /f "usebackq tokens=2 delims=:" %%a in ("file.txt") do for /f %%b in ("%%a") do echo %%b

Jorgegs
Posts: 22
Joined: 07 Apr 2016 22:56

Re: Extract only numeric content without spaces

#5 Post by Jorgegs » 29 May 2016 22:59

Fantastic
Thanks for your answer to my problem.

The only question with this solution, is that I get the result all in one var

I need the result in 4 different var.

In any case thank you very much for your great helpfulness

Jorgegs

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract only numeric content without spaces

#6 Post by foxidrive » 30 May 2016 00:15

This is a little bit of a kludge, but it works for your situation.

Code: Select all

@echo off
for /f "usebackq tokens=1,2 delims=:" %%a in ("file.txt") do for /f %%b in ("%%b") do set "var-%%a=%%b"
set "var-sizeondisk=%var-size on disk%"
set "var-size on disk="
set var-
pause & goto :EOF

Jorgegs
Posts: 22
Joined: 07 Apr 2016 22:56

Re: Extract only numeric content without spaces

#7 Post by Jorgegs » 30 May 2016 01:14

Thank you "foxidrive".
I put you in a "DOS" monument

Now I get this result
var-Directories=26
var-Files=1037
var-Size=46.702.989
var-sizeondisk=46.702.989

But I need only the numbers as content for 4 separate var, without names and = symbol.
Because i need them to add the numbers in separate processes.
How can I do it ?

Thanks in advance for your time
Jorgegs

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Extract only numeric content without spaces

#8 Post by elzooilogico » 30 May 2016 05:27

Jorge,
foxidrive wrote:This is a little bit of a kludge, but it works for your situation.

Code: Select all

@echo off
for /f "usebackq tokens=1,2 delims=:" %%a in ("file.txt") do for /f %%b in ("%%b") do set "var-%%a=%%b"
set "var-sizeondisk=%var-size on disk%"
set "var-size on disk="
set var-
pause & goto :EOF

Foxidrive gives you a working solution. if you change set var- with echo %var-directories% %var-Files% %var-size% %var-sizeondisk%

Code: Select all

@echo off
for /f "usebackq tokens=1,2 delims=:" %%a in ("file.txt") do for /f %%b in ("%%b") do set "var-%%a=%%b"
set "var-sizeondisk=%var-size on disk%"
set "var-size on disk="
echo %var-directories% %var-Files% %var-size% %var-sizeondisk%
pause & goto :EOF

Result is
45 34589 87.398 87.456

Replace echo with the command you need.

BTW, if you want to avoid the dot in numbers...

Code: Select all

@echo off
for /f "usebackq tokens=1,2 delims=:" %%a in ("file.txt") do for /f %%b in ("%%b") do set "var-%%a=%%b"
set "var-sizeondisk=%var-size on disk%"
set "var-size on disk="

set "var-size=%var-size:.=%"
set "var-sizeondisk=%var-sizeondisk:.=%"

echo %var-directories% %var-Files% %var-size% %var-sizeondisk%

pause & goto :EOF

Now, result is
45 34589 87398 87456


Saludos! :mrgreen:

Jorgegs
Posts: 22
Joined: 07 Apr 2016 22:56

Re: Extract only numeric content without spaces

#9 Post by Jorgegs » 30 May 2016 06:17

This is fantastic
Thanks for your help.

For me the command FOR is the understanded one.
It is very powerful and for me to complicated.
It is a pity that I do'nt know how to use it.

In any case, thanks, thanks, thanks.
Jorgegs

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract only numeric content without spaces

#10 Post by foxidrive » 30 May 2016 13:44

Thanks for your followup, elzooilogico.

Jorgegs wrote:For me the command FOR is the understanded one.
It is very powerful and for me to complicated.


Jorgegs, unfortunately I didn't pay close attention and it looks more confusing than it should - because the second for command has %%b in three places instead of only one place - and it only works because a fluke of how batch code operates.

The second for command in that line should really be written like this
do for /f %%c in ("%%b") do set "var-%%a=%%c"

- where %%c is changing the text inside %%b and giving the result (in %%c).


I'm not surprised that it looks very odd the way it is written in my code. :)

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Extract only numeric content without spaces

#11 Post by elzooilogico » 31 May 2016 04:00

foxidrive wrote:The second for command in that line should really be written like this
do for /f %%c in ("%%b") do set "var-%%a=%%c"

I can't believe I missed that :oops: Definitively I need more coffee in the morning!

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Extract only numeric content without spaces

#12 Post by douglas.swehla » 01 Jun 2016 11:43

Jorgegs wrote:For me the command FOR is the understanded one.
It is very powerful and for me to complicated.
It is a pity that I do'nt know how to use it.


Here is a series of exercises I found very useful when learning about batch loops in general: http://resources.infosecinstitute.com/cmd-exe-loops-part-iii/

Rob van der Woude's excellent site also has a good overview of FOR loops specifically, along with some exercises:

When all else fails, read the directions :D . The built-in documentation (for /? or help for) is actually pretty thorough, and always available.

Post Reply