Solved

DOS batch file, change numbers within a text file

Posted on 2014-01-20
11
258 Views
Last Modified: 2014-01-20
I have ~2,500 text files like this:

10.00000
0
0
-10.00000
2827500.250
2179999.750

I need to modify the last two lines:

Line [5]  + 4.75
Line [6]  - 4.75

many thanks
0
Comment
Question by:hgj1357
11 Comments
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 200 total points
Comment Utility
The following script changes the last lines of the *.txt files. For safety you can make a copy of all your .txt files before you run the following script.

Start -> run -> Powershell_ise (Enter)


Paste the following : and rename the location of folder c:\drivers to your location
---------------------------------
$files = Get-ChildItem C:\DRIVERS -Filter *.txt
foreach ($file in $files)
{
$text=gc $file.Fullname
$text[-2]="+4.75"
$text[-1]="-4.75"
$text > $file.FullName

}
---------------------------------
Press F5
0
 
LVL 2

Author Comment

by:hgj1357
Comment Utility
I'm not familiar with Powershell.

Where do I paste, and how do I run it .  Thanks
Powershell.jpg
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 300 total points
Comment Utility
I think the OP wants those values modified. In which cases the script should be:

$text[-2] = 4.75 + $text[-2]
$text[-1] = -4.75 + $text[-1]

Note that you need the 4.75 + $text to force Powershell to add numbers, cause $text + 4.75 will concatenate strings.

HTH,
Dan
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@hgj1357: you need to paste it in the lower window
0
 
LVL 2

Author Comment

by:hgj1357
Comment Utility
How do I run it?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
BTW, if you want to keep the 3 decimals (as in 2827500.250) you need to modify the code like this:
Param (
[string]$inputPath = "X:\path\to\your\files"
)

$files = Get-ChildItem $inputPath -Filter *.txt
foreach ($file in $files)
{
    $text=gc $file.Fullname
    $text[-2] = "{0:F3}" -f (4.75 + $text[-2])
    $text[-1] = "{0:F3}" -f (-4.75 + $text[-1])
    $text > $file.FullName
}

Open in new window

Save the code as whatever.ps1 and run it as whatever.ps1 X:\your\path

OR, change X:\path\to\your\files to your actual path, paste the entire script in the lower pane of the Powershell ISE window and then press ENTER.
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 300 total points
Comment Utility
run script
0
 
LVL 2

Author Comment

by:hgj1357
Comment Utility
This works - nearly.  The resultant text file is some kind of ANSI format that I can not use further. (In AutoCAD).  If I open and save as ANSI encoded file type, it then works again.  Is there a way to bulk change the encoding type to ANSI?
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Run this in powershell:

Get-ChildItem "X:\path\to" -Filter *.txt | % { (Get-Content $_.fullname) | Set-Content $_.fullname -Force -Encoding ASCII}

Open in new window


Of course, change X:\path\to to your actual path.
0
 
LVL 2

Author Comment

by:hgj1357
Comment Utility
I used type File>Newfile.txt to fix issue.

All good.

Thanks
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Removed MSDOS Topic and added Powershell Topic since the accepted solution is not an MSDOS based solution.

Gerwin Jansen
EE Topic Advisor
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now