Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Batch File, Calculating the Contents of a Text File


  • Please log in to reply
10 replies to this topic

#1 Gotcha_Hotshot22

Gotcha_Hotshot22

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 03 July 2014 - 07:56 PM

Anyone know how to take the contents of a text file, numbers only, calculate and then store them as a value? I'll be constantly adding to this text file and they need to be calculate. Like the balance of a bank but a little different.

 

Please and thanks!


Thanks,

Daniel


BC AdBot (Login to Remove)

 


#2 JohnC_21

JohnC_21

  • Members
  • 24,827 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 03 July 2014 - 08:11 PM

I think that would actually be very hard to do as the numbers in your text file are not integers but text and doing calculations would not be possible. Is there any reason you can't use a spreadsheet to do this?



#3 Gotcha_Hotshot22

Gotcha_Hotshot22
  • Topic Starter

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 03 July 2014 - 08:24 PM

The program needs to be complete dependent on on itself, and it itself is a batch file. I'm able to take the entire contents of the text file and put them in one variable, so if there's a way to split one up...Maybe that would help?


Thanks,

Daniel


#4 pwf119

pwf119

  • Members
  • 10 posts
  • OFFLINE
  •  
  • Local time:10:07 AM

Posted 03 July 2014 - 08:43 PM

The perl programming language would be perfect for this task.  Install active perl on your pc and write a perl script to extract numbers from the text file and perform your calculation.  There are many perl resources available on the web to refer to.



#5 Gotcha_Hotshot22

Gotcha_Hotshot22
  • Topic Starter

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 03 July 2014 - 08:45 PM

Alright, Ill give it a go, thanks!


Thanks,

Daniel


#6 scotty_ncc1701

scotty_ncc1701

  • Members
  • 520 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:07 AM

Posted 03 July 2014 - 08:57 PM

Why not just use a spreadsheet?  Office, LibreOffice, etc would allow you to import a text file, and you can put in formulas to total it up.

 

Have a great day.



#7 scotty_ncc1701

scotty_ncc1701

  • Members
  • 520 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:07 AM

Posted 03 July 2014 - 09:12 PM

Ok, the above being said, you can use powershell.

1.  Create a file called sumit.txt.  Example:

50
100
25
26

2.  Add the values in one on each line.
3.  Start powershell.
4.  Enter the command (no quotes): "gc .\sumit.txt | measure-object -sum -max -min -ave"
6.  Sample output:

Count    : 4
Average  : 50.25
Sum      : 201
Maximum  : 100
Minimum  : 25
Property :

 

More info on the above, search for "measure-object".
 



#8 Gotcha_Hotshot22

Gotcha_Hotshot22
  • Topic Starter

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 03 July 2014 - 09:20 PM

As far as trying perl, I had no luck. And being that I have no experience with perl I'm not able to edit online scripts to get what I want.

 

And to Scotty_ncc1701, that is actually a very good idea but I need it done automatically and in a way that will pipe said output (as one number) back into the batch or at least another file that I can have the batch file read and import the contents as a variable, which I so luckily know how to do already.


Thanks,

Daniel


#9 scotty_ncc1701

scotty_ncc1701

  • Members
  • 520 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:07 AM

Posted 03 July 2014 - 09:39 PM

Complete batch:

1.  Create file like indicated in previous post with the numbers.

2.  Create a file called SUMIT.PS1 (note extension):
2.1.  Add the command (no quotes): "gc .\sumit.txt | measure-object -sum -max -min -ave"

3.  Make sure the text file with numbers, and the "ps1" script are in the same folder.

4.  Start powershell (this is only needed to be done once):
4.1.  Enter the command (no quotes): "Set-ExecutionPolicy RemoteSigned"
4.2.  Powershell doesn't allow scripts to be run by default.  The above forces powershell to run scripts.

4.3.  The above command is permanent, until you reset your computer (e.g. recovery partition or CD)

5.  Exit powershell.

6.  In your batch file:
6.1.  CD to the location of the two above files.
6.2.  Enter "powershell .\sumit.ps1" (no quotes) in the batch file.

Example run:

-----> Begin just showing contents of files

>type sumit.txt
50
100
25
26

>type sumit.ps1
gc .\sumit.txt | measure-object -sum -max -min -ave

-----> End just showing contents of files

>powershell .\sumit.ps1

Count    : 4
Average  : 50.25
Sum      : 201
Maximum  : 100
Minimum  : 25
Property :
 

 

Best of luck! :bananas: :bounce:


Edited by scotty_ncc1701, 03 July 2014 - 09:42 PM.


#10 Gotcha_Hotshot22

Gotcha_Hotshot22
  • Topic Starter

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:07 AM

Posted 04 July 2014 - 03:36 PM

Okay, that's definitely a step in the right direction, but is there a way to only get the sum of all the numbers?


Thanks,

Daniel


#11 scotty_ncc1701

scotty_ncc1701

  • Members
  • 520 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:07 AM

Posted 04 July 2014 - 05:51 PM

Okay, that's definitely a step in the right direction, but is there a way to only get the sum of all the numbers?

 

Create a new file (xx1.ps1), with this in it:

exit gc .\sumit.txt | measure-object -sum | Select-Object -expand Sum

Create a batch file (xx1.bat):

--- Start batch
@echo off
setlocal enableextensions
powershell -file "xx1.ps1"
echo ------ %ERRORLEVEL%
endlocal

--- End batch

Save both in the same folder.

From a command prompt, run the batch file.

This should now get you going.  Note, "sumit.txt" is the same file from my previous post.

Have a great day. :birthday:  USA!


Edited by scotty_ncc1701, 04 July 2014 - 06:13 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users