I have a file that i edit with notepad, example of some of the file content :
ionTrigger_1_Deactivated":false,"bWL1TwoSwitchesArrived":true,"E1_WL1_HangingBedsA_M.TheWorld:PersistentLevel.GppInteractionTrigger_0_Deactivated":false,"E1_WL1_HangingBedsA_M.TheWorld:PersistentLevel.GppInteractionTrigger_1_Deactivated":false,"bHangingBedsASaved":true} ! m_WhatLevelPlayerIsAtEncodedJson ArrayProperty T L {"currentLevelName":"E1_WL1_HangingBedsA_M","currentLevelEntryDirection":8} & m_WhatCheckPointPlayerIsAtEncodedJson ArrayProperty { s {"hasCheckPoint":true,"LocX":19873.1816,"LocY":2330.6631,"LocZ":-299.1126,"RotPitch":0,"RotYaw":81508,"RotRoll":0} None m_SubtitlesDisplayed BoolProperty None
what i want to extract as a string is this part :
currentLevelName":"E1_WL1_HangingBedsA_M"
the problems are how to extract it and that in each file the currentLevelName is different. in this file the currentLevelName is E1_WL1_HangingBedsA_M but in another file for example the name is : E1_WL1_HallwayDragonFace_M
i want to loop over all the files and in each file to extract that part. and when extracting the currentLevelName i want to change the current file name to that level name for example if the first file in the loop is : savedGame001
then in the loop the first file name should be renamed to : Current Level Name_E1_WL1_HangingBedsA_M
CodePudding user response:
If the text in your file is in JSON format i suggest use the Newtonsoft.Json package from nuget. You can deserialize the json to a dynamic object using
dynamic myJsonObject = JsonConvert.DeserializeObject("filename.json");
CodePudding user response:
To get the specific string pattern in a non-JSON format data string
Use the regex
to get the stirng and operate it will be good I thought.
By using the regex: "currentLevelName":"\w "
In your example code, your will get: "currentLevelName":"E1_WL1_HangingBedsA_M"
Then use the result to create or replace your file name.
the code below will get the savedGame001.txt
's content and extract the currentLevelName block, then create a new file whcih the name is in this format: [filename]_[theCurrentLevelName]
using System.Text.RegularExpressions;
// your file path
string filePath = @"C:\Users\a0204\Downloads";
// your file name
string fileName = @"savedGame001.txt";
// read file content
string stringContent = string.Empty;
stringContent = System.IO.File.ReadAllText(filePath "\\" fileName);
// Get the mathced string By regex => "currentLevelName":"\w "
var regex = new Regex("\"currentLevelName\":\"\\w \"");
Match matched = regex.Match(stringContent);
string matchedString = matched.Value;
// Get the string below the colon
int colonPosition = matchedString.IndexOf(":");
string value = matchedString.Substring(colonPosition 1);
value = value.Replace("\"", string.Empty);
// remove the .txt and add the matched string to file name
fileName = fileName.Remove(fileName.Length - 4, 4);
string newFileName = fileName "_" value;
// check the new file name
Console.WriteLine(newFileName);
// write content to new file name
FileStream fileStream = File.Create(filePath "\\" newFileName);
fileStream.Dispose();
File.WriteAllText(filePath "\\" newFileName, stringContent);
Console.ReadLine();