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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@hgj1357: you need to paste it in the lower window
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:
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.
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
}
Save the code as whatever.ps1 and run it as whatever.ps1 X:\your\pathOR, 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
Of course, change X:\path\to to your actual path.
Get-ChildItem "X:\path\to" -Filter *.txt | % { (Get-Content $_.fullname) | Set-Content $_.fullname -Force -Encoding ASCII}
Of course, change X:\path\to to your actual path.
ASKER
I used type File>Newfile.txt to fix issue.
All good.
Thanks
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
Gerwin Jansen
EE Topic Advisor
ASKER
Where do I paste, and how do I run it . Thanks
Powershell.jpg