YoannMorl Posted January 30, 2013 Posted January 30, 2013 (edited) 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 January 30, 2013 by YoannMorl
Moderators Melba23 Posted January 30, 2013 Moderators Posted January 30, 2013 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 ignoredThere might be a simpler SRE, but this one works! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
YoannMorl Posted January 30, 2013 Author Posted January 30, 2013 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
YoannMorl Posted January 30, 2013 Author Posted January 30, 2013 (edited) 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 January 30, 2013 by YoannMorl
Moderators Melba23 Posted January 30, 2013 Moderators Posted January 30, 2013 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! 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
YoannMorl Posted January 30, 2013 Author Posted January 30, 2013 (edited) 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 January 30, 2013 by YoannMorl
Moderators Melba23 Posted January 30, 2013 Moderators Posted January 30, 2013 (edited) YoannMorl, <replaced corrupted text> Any more surprises as that looks nothing like the original line? M23 Edited January 30, 2013 by Melba23 All text corrupted - replaced 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
YoannMorl Posted January 30, 2013 Author Posted January 30, 2013 (edited) 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 January 30, 2013 by YoannMorl
Moderators Melba23 Posted January 30, 2013 Moderators Posted January 30, 2013 YoannMorl, No no more surprise. i promiseGood! 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now