Solved

DOS batch file, change numbers within a text file

Posted on 2014-01-20
260 Views
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
Question by:hgj1357

LVL 18

Accepted Solution

Raheman M. Abdul earned 200 total points
ID: 39794525
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 ID: 39794574 I'm not familiar with Powershell. Where do I paste, and how do I run it . Thanks Powershell.jpg 0 LVL 34 Assisted Solution Dan Craciun earned 300 total points ID: 39794584 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 ID: 39794587 @hgj1357: you need to paste it in the lower window 0 LVL 2 Author Comment ID: 39794622 How do I run it? 0 LVL 34 Expert Comment ID: 39794653 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
}

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

Dan Craciun earned 300 total points
ID: 39794676
0

LVL 2

Author Comment

ID: 39794826
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

ID: 39794896
Run this in powershell:

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.
0

LVL 2

Author Comment

ID: 39795032
I used type File>Newfile.txt to fix issue.

All good.

Thanks
0

LVL 37

Expert Comment

ID: 39795259
Removed MSDOS Topic and added Powershell Topic since the accepted solution is not an MSDOS based solution.

Gerwin Jansen
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.