Author Posts

March 1, 2017 at 9:24 am

Hey,

i would like to change a txt File .
can you help me find the right Commands for my actions

First of all i check if the txt file is in the path he should be
if its not there he should copy my file from a shared source.
If it is there he should search in the txt file, if there is a string called "xxx"
if there is a string called "xxx" i want him to change the string to "yyy"
if there is a string called "zzz" i want him to change the string to "yyy"
so 2 different scenarios he need to check if there is a txt file already.

if there is no such string "xxx" or "zzz" i want him to add the string "yyy"

after that he should save the changes to the txt file.
I would be very happy if you can help me 😀

March 1, 2017 at 10:38 am

Would you like us to do it for you or do you like to do it by yourself? 😉

First of all i check if the txt file is in the path he should be

Test-Path

if its not there he should copy my file from a shared source.

Copy-Item

If it is there he should search in the txt file, if there is a string called "xxx"

Get-Content
Select-String or -match a reguklar expression

March 1, 2017 at 10:39 am

... unfortunately I had to devide my post in two parts ...

if there is a string called "xxx" i want him to change the string to "yyy"
if there is a string called "zzz" i want him to change the string to "yyy"
so 2 different scenarios he need to check if there is a txt file already.

-replace

if there is no such string "xxx" or "zzz" i want him to add the string "yyy"

Add-Content

after that he should save the changes to the txt file.

Out-File
Here you can find information about how to run code conditionally about_comparison_operators
In general you should read about get-help and get-command

Have a lot of fun!

March 1, 2017 at 12:54 pm

hey , thx vor the quick respond, i managed to do it by myself.
I thought off placing a wildcard instead of using different scenarios to replace.

I want the file to be checked so i can insert a string.
the original content of the file is
"defaultTheme=xminddefaultthemeid
eclipse.preferences.version=1"

i want to change the first row so the original string should be "defaulttheme=*" i want a * since the themecode can vary, and every themecode that is not "xxx" i want powershell to change for me.
replacestring is defaultheme=xxx
so if the string isnt already defaultheme=xxx i want him to change every other themecode after defaultheme= to mine....
he wont do that unfortanetly

Here is the full code to check on

Set-StrictMode -Version "2.0"
Clear-Host
# PowerShell Checks If a File Exists
$WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
$FileExists = Test-Path $WantFile
If ($FileExists -eq $True)
{

$Path="D:\Xmind\org.xmind.ui.mindmap.pref"
#Professionell, Default
$OriginalString="defaultTheme=*"
#Klassisch 3
$ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
#Auslesen
$AllText = [IO.File]::ReadAllText($Path)
#Ersetzen
$AllTextNew=$AllText.Replace($OriginalString,$ReplaceString)
#Ausgangsdatei ersetzen
$AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"

}

If ($FileExists -eq $False)
{New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
}

any tipps or suggestions

March 1, 2017 at 2:31 pm

See below. Comments made on changes to explain why.

Set-StrictMode -Version "2.0"
Clear-Host
# PowerShell Checks If a File Exists
$WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
$FileExists = Test-Path $WantFile
If ($FileExists -eq $True)
{

# Removed this because you already have your file path defined in $WantFile.
# This is also the path you validated so it should be the one you use.
#$Path="D:\Xmind\org.xmind.ui.mindmap.pref"

#Professionell, Default
# Changed this to a RegEx search pattern to be used with -replace
$OriginalString="defaultTheme=.*"
#Klassisch 3
$ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"

#Auslesen
# Changed to use $WantFile since this path was validated
# Changed to use Get-Content so that output file maintains its original formating
#$AllText = [IO.File]::ReadAllText($Path)
$AllText = Get-Content $WantFile

#Ersetzen
# Changed to use -replace which uses RegEx pattern matching to find the data to replace
#$AllTextNew=$AllText.Replace($OriginalString,$ReplaceString)
$AllTextNew=$AllText -Replace $OriginalString, $ReplaceString

#Ausgangsdatei ersetzen
$AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"

}

If ($FileExists -eq $False)
{# Added the carriage return character to the value, `r, so that is shows up normally in notepad
 # with the values on different lines
#New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `r`neclipse.preferences.version=1"
}

March 1, 2017 at 2:43 pm

So even a little bit more "streamliined" it could be like this:

$WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
If (Test-Path $WantFile){
    $OriginalString="defaultTheme=.*"
    $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
    $AllText = Get-Content $WantFile
    $AllTextNew=$AllText -Replace $OriginalString, $ReplaceString
    $AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"
}
Else{
    New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
}

March 1, 2017 at 2:44 pm

Thanks very much!
In my Script it would have copied the replace string over and over again and it didnt accept the wildcard *
So i would have 1 more scenario to solve
for example the txt file is already in the folder but it doesnt have the string "defaultTheme=23bcqg7bfvg262t8lg70ta186f"
there is only other information.
i would like to add the string into the first row of the document.
how can i easily manage that?

The Problem with the different Defaulttheme entrys we solved by replacing but what can I do to check if there is any entry like that to then create on.

Thanks in advance!

March 1, 2017 at 3:03 pm

If I got you right ...

$DefaultConfig = @"
defaultTheme=23bcqg7bfvg262t8lg70ta186f
eclipse.preferences.version=1
"@

$WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
If (Test-Path $WantFile){
    $CurrentContent = Get-Content $WantFile
    If($CurrentContent -match 'defaultTheme=23bcqg7bfvg262t8lg70ta186f'){
        $newContent = $CurrentContent | Foreach-Object {$_ -replace '(?< =defaultTheme=).*(?=$}','23bcqg7bfvg262t8lg70ta186f'}
        $newContent | Out-File -FilePath  $WantFile
    }
    Else{
        $newContent = $DefaultConfig + $CurrentContent
        $newContent | Out-File -FilePath  $WantFile
    }
}
Else{
    New-Item $WantFile -ItemType file -value $DefaultConfig
}

March 1, 2017 at 7:05 pm

Olaf, you are also adding eclipse.preferences.version=1 which is not the desired results.

$File = "D:\Xmind\org.xmind.ui.mindmap.pref"
If (Test-Path $File)
{
    $RegExPatt="defaultTheme=.*"
    $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
    $FileContent = Get-Content $File
    If ($FileContent -match $RegExPatt) {
        $FileContent -Replace $RegExPatt, $ReplaceString | Set-Content -Path $File
    } Else {
        "defaultTheme=23bcqg7bfvg262t8lg70ta186f",$FileContent | Set-Content -Path $File
    }
} Else {
    "defaultTheme=23bcqg7bfvg262t8lg70ta186f","eclipse.preferences.version=1" | Set-Content -Path $File
}

March 1, 2017 at 10:14 pm

You're right. I wanted to avoid the backtick and ended up beyond the target. Thanks for correcting me. 😉

March 2, 2017 at 10:21 am

Thanks a lot for your help guys.

I would have 1 more Task to solve,

i want to copy a file in specific folders
the folder name can change due to different versions of the software.
org.xmind.ui.resources_*VERSION*
org.xmind.ui.resources_3.7.0.201612151837

in every of this Folders org.xmind.ui.resources_*
i want my file to be copied.
Is there an easy way to make this possible
Thanks in advance.

Basically i want him to check all Folders that start like that, and copy a file into a subfolder
org.xmind.ui.resources_*\templates\myfile

so powershell should check for folders with that name to then copy my file into the template subfolder of those he found.

i tried with get child item and includ templates.xml that way i can find all templates but thats the wrong way to go