Jump to content
Sign in to follow this  
YoannMorl

Find a variable string in a text file, modify it and replace it.

Recommended Posts

YoannMorl

Hi,

I'm working on a script to merge txt files on a csv file.

My csv file is generated with autoit like this :

FIRSTNAME Lastname;BAT. T;225;XXX 034 458;50XXXXX;Dell Optiplex 790...

FIRSTNAME Lastname;BAT. T;225;XXX 034 458;50XXXXX;Dell Optiplex 790...

FIRSTNAME Lastname;BAT. T;225;XXX 034 458;50XXXXX;Dell Optiplex 790...

FIRSTNAME Lastname;BAT. T;225;XXX 034 458;50XXXXX;Dell Optiplex 790...

etc...

Each line data is different but the general formatting is the same (Name;Localisation;Room;Computer;Serial;Model)

The computer name is always like this : ABC 012 345

Is there a way to remove spaces in each computer name of the file ? From ABC 012 345 to ABC012345 ?

Each computer name always start with GRE. Maybe by searching string like ";GRE ".

But I don't know how to do.

Thanks for your help.

Regards.

Edited by YoannMorl

Share this post


Link to post
Share on other sites
Melba23

YoannMorl,

Read the file into a variable with FileRead and then run a RegEx on it like this:

$sFile = "FIRSTNAME Lastname;BAT. T;225;XXX034458;50XXXXX;Dell Optiplex 790..." & @CRLF & _
         "FIRSTNAME Lastname;BAT. T;225;XXX034458;50XXXXX;Dell Optiplex 790..." & @CRLF & _
         "FIRSTNAME Lastname;BAT. T;225;XXX034458;50XXXXX;Dell Optiplex 790..." & @CRLF & _
         "FIRSTNAME Lastname;BAT. T;225;XXX034458;50XXXXX;Dell Optiplex 790..."

$sNewFile = StringRegExpReplace($sFile, "(.*;)(\w{3})\s(\d{3})\s(\d{3})(;.*)", "$1$2$3$4$5")

MsgBox(0, "New File", $sNewFile)

Finally rewrite the file with FileWrite. :)

SRE decode:

(.*;)     - Capture everything up to a semi-colon followed by
(\w{3})   - A group of 3 letters which we capture followed by
\s        - A space which we do not capture,
(\d{3})   - A group of 3 digits which we do,
\s        - Another space which we ignore,
(\d{3})   - And another group of 3 digits which we capture, all followed by
(;.*)     - the rest of the line until we find another match.

$1$2$3$4$5 - And we rewrite everythign except teh spaces we ignored

There might be a simpler SRE, but this one works! :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
YoannMorl

Thanks. Didn't know that is possible ^^

But this i my script, and it only write a "1" in the file. Don't see where's my mistake.

Func _Merge()
Global $DirToScan = @ScriptDir & "\ScanParc"
Global $FileList, $hEachCSVFileHandle, $sEachCSVFileContents
Global $Merge = IniRead(@ScriptDir & "\System\config.ini","CONFIG","MERGE","" & @ScriptDir & "\")
Global $sPathOfMasterCSV = $Merge & "ScanParcAll" & "-" & @MDAY & @MON & @YEAR & ".csv"
Global $FileList = _RecFileListToArray($DirToScan, "*.txt", 1, 1, 1)
$hMasterCSVFileHandle = FileOpen($sPathOfMasterCSV, 2)
FileWrite($hMasterCSVFileHandle, "Utilisateur;Bâtiment;Pièce;GRE Poste;SN Poste;Modèle Poste;OS;Adresse MAC;GRE Ecran; Modèle Ecran;SN Ecran;GRE Périph1;Modèle P1;SN P1;GRE Périph2;Modèle P2;SN P2" & @CRLF)
For $i = 1 to $FileList[0]
$hEachCSVFileHandle = FileOpen($DirToScan & "\" & $FileList[$i], 0)
If $hEachCSVFileHandle = -1 Then
  MsgBox(0, "Erreur", "Impossible d'ouvrir le fichier : """ & $DirToScan & "\" & $FileList[$i] & """ pour lecture.")
  Exit
EndIf
$sEachCSVFileContents = FileRead($hEachCSVFileHandle)
FileWrite($hMasterCSVFileHandle, $sEachCSVFileContents & @CRLF)
FileClose($hEachCSVFileHandle)
Next
FileClose($hMasterCSVFileHandle)
$sFile = FileOpen($sPathOfMasterCSV, 2)
$sNewFile = StringRegExpReplace($sFile, "(.*;)(\w{3})\s(\d{3})\s(\d{3})(;.*)", "$1$2$3$4$5")
FileWrite($sPathOfMasterCSV, $sNewFile)
;~ MsgBox(0,"","Done. Press Ok to open log file.")
Run("notepad.exe " & @ScriptDir & "\ScanParcAll" & "-" & @MDAY & @MON & @YEAR & ".csv")
Exit
EndFunc

Share this post


Link to post
Share on other sites
YoannMorl

I've found. Thanks ^^

A last thing. On each line, i have a second computer name. How to do for it ?

FIRSTNAME Lastname;BAT. T;225;XXX034458;50XXXXX;Dell Optiplex 790;aaa;aaa;aaa;XXX 034458"

Edited by YoannMorl

Share this post


Link to post
Share on other sites
Melba23

YoannMorl,

A last thing. On each line, i have a second computer name. How to do for it ?

One thing that really annoys me on this forum is the number of people who change their requirements halfway through a thread when they know full well from the very beginning that the initial question is not the one they actually need need to ask! :mad:

So let us try to establish exactly what it is you want before I waste any more of my time writing code that does not do what you want:

- Is this second computer name the same as the first or is it entirely different?

- Is it currently formatted "XXX 123 123" as the first name or "XXX 123123" as you have posted above?

- Is it always the very last thing on each line?

Answer those in sufficient detail and I will see what I can do. ;)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
YoannMorl

Sorry if i've not say all i needs. It's just to force me to work a little to find awsners before asking them here.

The same as the first.

Each line is presented like this :

NAME First;X;0;GRE 012 345;50XXXXX;Dell Optiplex 790;Windows 7;00:00:00:00:00:00;GRE 056 789;Dell 19';CN0NJ91XXXXX2RYB;Gre 000 000;Clavier;Inconnu;Gre 000 001;Souris;Inconnu

Edited by YoannMorl

Share this post


Link to post
Share on other sites
Melba23

YoannMorl,

<replaced corrupted text>

Any more surprises as that looks nothing like the original line?

M23

Edited by Melba23
All text corrupted - replaced

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
YoannMorl

No no more surprise. i promise.

Each Asset number of device are GRE 000 000.

I was just thinking how to apply your part of code to all the txt file.

Edited by YoannMorl

Share this post


Link to post
Share on other sites
Melba23

YoannMorl,

No no more surprise. i promise

Good! :thumbsup:

If all your lines are like that one then this should work on all the "names":

$sFile = "NAME First;X;0;GRE 012 345;50XXXXX;Dell Optiplex 790;Windows 7;00:00:00:00:00:00;GRE 056 789;Dell 19';CN0NJ91XXXXX2RYB;Gre 000 000;Clavier;Inconnu;Gre 000 001;Souris;Inconnu" & @CRLF & _
         "NAME First;X;0;GRE 012 345;50XXXXX;Dell Optiplex 790;Windows 7;00:00:00:00:00:00;GRE 056 789;Dell 19';CN0NJ91XXXXX2RYB;Gre 000 000;Clavier;Inconnu;Gre 000 001;Souris;Inconnu" & @CRLF & _
         "NAME First;X;0;GRE 012 345;50XXXXX;Dell Optiplex 790;Windows 7;00:00:00:00:00:00;GRE 056 789;Dell 19';CN0NJ91XXXXX2RYB;Gre 000 000;Clavier;Inconnu;Gre 000 001;Souris;Inconnu" & @CRLF & _
         "NAME First;X;0;GRE 012 345;50XXXXX;Dell Optiplex 790;Windows 7;00:00:00:00:00:00;GRE 056 789;Dell 19';CN0NJ91XXXXX2RYB;Gre 000 000;Clavier;Inconnu;Gre 000 001;Souris;Inconnu"

$sNewFile = StringRegExpReplace($sFile, ";(\w{3})\s(\d{3})\s(\d{3});", ";$1$2$3;")

ConsoleWrite($sNewFile & @CRLF)

I am off to play golf now - if there are any problems I will be back this evening. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×