Answered by:
PowerShell Output - Need Help Exporting Data to Text File via Script

Question
-
Brand new to Powershell (today actually). Let's get to it.
I have done a lot of searching to try and figure out my issues, but have not had luck, or at least not in a way that I understand it for my situation. What I am trying to do is read a .txt file, delete the last few lines, and then re-save as the same file. I believe I have figured out the first two parts, but can't figure out the third. I need help with that part, as well as how to do this as a script.
Example File to Modify - export.txt
"Time " "vertical_accelerations.driver"
0.000000e+000 7.283554e-002
2.000000e-003 4.240822e-002
4.000000e-003 -4.195203e-003
6.000000e-003 -1.029092e-002
8.000000e-003 -5.005013e-003
1.000000e-002 -1.107896e-002
1.200000e-002 -2.887172e-002
1.400000e-002 -5.407065e-002
1.600000e-002 -8.010106e-002
1.800000e-002 -1.002183e-001
2.000000e-002 -1.097979e-001The export.txt file length is not always the same, so what I have come up with for typing into the PS command window is:
$content = (get-content C:\ADAMS\WD\export.txt) $content = $content[0..($content.length-6)] out-file C:\ADAMS\WD\export.txt
However, the last line doesn't work. It just makes export.txt a blank text file. So I need help exporting what I get from the 2nd line. If it has to be another .txt file instead, that is fine.
Finally, as mentioned, this works (at least the first two lines) if I type into the PS command window. What I want is to be able to run this as a script/batch file though. I am utilizing a software (iSight) that allows me to automate this process. First it creates the export.txt file via a command/batch file. Then I want to do what I have described above. Finally I have a command/batch file that will read the new file back into iSight.
Sorry for the lengthy first post, but I was hoping someone here could help me out. Thanks.
Answers
-
Hi,
Your batch file (shell script) would look something like this:
@echo off setlocal enableextensions %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File C:\ADAMS\WD\export_clean.ps1 endlocal
Your PowerShell script would look like the script I posted previously:
$exportFile = "C:\ADAMS\WD\export.txt" $content = get-content $exportFile $content[0..($content.Length - 6)] | out-file $exportFile
Note that I added the $exportFile variable so that in case you change that file name you only have to type it once.
Bill
- Edited by Bill_StewartModerator Tuesday, May 15, 2012 9:45 PM Correction
- Proposed as answer by Mike Crowley Wednesday, May 16, 2012 4:20 AM
- Marked as answer by Jon_gale Wednesday, May 16, 2012 12:35 PM
All replies
-
Hi,
$content = (get-content C:\ADAMS\WD\export.txt) $content = $content[0..($content.length-6)] out-file C:\ADAMS\WD\export.txt
Look at the last line of your code. out-file by itself, with just a file name, makes a blank file. I think you maybe mean something like this:
$content = (get-content C:\ADAMS\WD\export.txt) $content[0..($content.length-6)] | out-file C:\ADAMS\WD\export.txt
Bill
-
Awesome Bill. Thank you very much. Works exactly as what I was looking for.
Any idea on how I might incorporate all this into a batch file? I feel like it needs to be something like this:
set HOME=C:\ADAMS\WD "powershell" invoke-expression "C:\ADAMS\WD\export_clean.ps1"
Where "export_clean.ps1" is my script from above. However, this only opens the command window and begins powershell in the set directory. I need help figuring out how to make it now run my .ps1 file. Thank you again. -
Hi,
Your batch file (shell script) would look something like this:
@echo off setlocal enableextensions %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File C:\ADAMS\WD\export_clean.ps1 endlocal
Your PowerShell script would look like the script I posted previously:
$exportFile = "C:\ADAMS\WD\export.txt" $content = get-content $exportFile $content[0..($content.Length - 6)] | out-file $exportFile
Note that I added the $exportFile variable so that in case you change that file name you only have to type it once.
Bill
- Edited by Bill_StewartModerator Tuesday, May 15, 2012 9:45 PM Correction
- Proposed as answer by Mike Crowley Wednesday, May 16, 2012 4:20 AM
- Marked as answer by Jon_gale Wednesday, May 16, 2012 12:35 PM
-