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
… 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.
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
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"
}
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.
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