Link to home
Start Free TrialLog in
Avatar of hgj1357
hgj1357

asked on

DOS batch file, change numbers within a text file

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
ASKER CERTIFIED SOLUTION
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hgj1357
hgj1357

ASKER

I'm not familiar with Powershell.

Where do I paste, and how do I run it .  Thanks
Powershell.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@hgj1357: you need to paste it in the lower window
Avatar of hgj1357

ASKER

How do I run it?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hgj1357

ASKER

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?
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.
Avatar of hgj1357

ASKER

I used type File>Newfile.txt to fix issue.

All good.

Thanks
Removed MSDOS Topic and added Powershell Topic since the accepted solution is not an MSDOS based solution.

Gerwin Jansen
EE Topic Advisor