Powershell script to change case

dalesdales Member Posts: 225
Hi all,

I've got a bit of an issue I'm trying to figure my way around but think I will need your assistance. I started learning powershell yesturday for our virtualcenter and really like it. Today someone comes to me with a problem which I think can be fixed with powershell but a bit of expert advice wouldn't go amiss.

We have a text file that is sent to us every day with certain details in it, the text file comes from an online app which allows you to type results in upper and lower case, only thing is typing results in lower case means importing the file into the relevant app crash. We have no control over said online app so we gotta figure a way of changing the text inside the file to upper case and preferrably only apply it to a certain set of charecters in eachs line, although I'm just trying to figure out how to change the case globally at the moment before worrying about that.

At the mo I have:

$a = get-content "test.txt"
$a = $a.ToUpper()

from what I have read it should work but it is not changing the case when I look at the content by echo $a.
Also do I have to script a save file or something at the end?

Sorry I am googling this as well but at the moment would like a bit of advice regarding my actual query rather than what I have found so far.


thanks in advance!
Kind Regards
Dale Scriven

Twitter:dscriven
Blog: vhorizon.co.uk

Comments

  • dalesdales Member Posts: 225
    Ah right if I do this then I get the whole file changed to upper.

    $a = get-content "test.txt"
    ( "$a" ).ToUpper()
    Kind Regards
    Dale Scriven

    Twitter:dscriven
    Blog: vhorizon.co.uk
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Yeah, you should need to save the file at the end. Have you already completed that?
    Good luck to all!
  • dalesdales Member Posts: 225
    Yes managed that ok

    script now looks like this:

    $a = "c:\documents and settings\user\desktop"
    $b = get-content "$a\test.txt"
    ( "$b" ).ToUpper() | out-file -filepath "$a\new.txt"

    So I've got the basics working but would like to change case of only certain words or text between certain character positions (say line 1-100 char 200-250 on each line).
    Kind Regards
    Dale Scriven

    Twitter:dscriven
    Blog: vhorizon.co.uk
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    What type of file is this? Do you have any example text and what you would like it to be at the end?

    Also, I just wanted to mention there is a lot that you can do with this. For example, if your file is dumped into a folder you can run a script that gets all the files and runs them through you code via a foreach loop.

    Here is one I have done that executes all the scripts in a specific folder.
    $scripts = Get-ChildItem C:\scripts\Dashboard_Scripts
    foreach($script in $scripts)
    {
     if($script.Name -notcontains ".csv")
     {
      C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\scripts\Dashboard_Scripts\$script
     }
    }
    

    One of the scripts puts output to a CSV so I exclude it via if -notcontains.

    EDIT -notcontains should actually be -notmatch. I copied an old version of the script. Actually instructive as -notcontains is used with arrays where as -notmatch is good for a simple comparisson.
  • dalesdales Member Posts: 225
    The file is just a standard text file with account details in it, I'll see if I can give an example tomorrow. generally text looks a bit random but there are key words for payment options like "rat" however our app throughs a fit if you import rat it would rather have RAT, there are loads of other keywords but as a rule the amount of text to be corrected is quite small and is only one word per line every now and again.

    However because of the amount of payments we received and the file is downloaded every day its needs to be scripted.
    Kind Regards
    Dale Scriven

    Twitter:dscriven
    Blog: vhorizon.co.uk
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Given that information, I would say it might be best just to do what you have done and use ToUpper on the entire file. IMO, it is generally best to keep things simple and if that works it's certainly the simpler method.
  • MishraMishra Member Posts: 2,468 ■■■■□□□□□□
    Is there a cleaner method to this?

    $read = Read-Host "Hello"; $read = $read.ToUpper()


    I would love to be able to just type something like

    $read.ToUpper() = Read-Host "Hello"

    That doesn't work because of operational order.
    My blog http://www.calegp.com

    You may learn something!
  • kalebkspkalebksp Member Posts: 1,033 ■■■■■□□□□□
    (Read-Host "Hello").ToUpper()
  • MishraMishra Member Posts: 2,468 ■■■■□□□□□□
    $read = (Read-Host "Hello").ToUpper()

    Thanks!
    My blog http://www.calegp.com

    You may learn something!
Sign In or Register to comment.