BenitoB Posted January 23, 2013 Share Posted January 23, 2013 Good Day all,I created a script which will search for a servername in a drive mapping (NET USE command) in a specific folder and file on every user share and if found it will replace the old servername with the new servername. It works perfectly if I specify the \\servername\sharename\common folder\common file. I searched the forum and found a few pointers but I just can't get around it.Here is the structure I am working with:The script will read the source file Sharelist.txt until the EOF. Here is the structure of the file:\\servername\rootsharename1$\\servername\rootsharename2$\\servername\rootsharename3$ and so on.Under the rootsharename are the usershares with the \common folder\common file.ex:\\servername\rootsharename1$\JohnD\NET\mapdrive.bat\\servername\rootsharename1$\JoeB\NET\mapdrive.bat\\servername\rootsharename1$\SamP\NET\mapdrive.bat\\servername\rootsharename2$\BobT\NET\mapdrive.bat\\servername\rootsharename2$\RickP\NET\mapdrive.bat and so onI need help on how to get the script to go from one usershare to the other.Thanks Link to comment Share on other sites More sharing options...
JohnOne Posted January 23, 2013 Share Posted January 23, 2013 You need to loop through the usershares, either as you read them (FileReadLine) or from an array which you have previously read from file. Look at_filereadtoarray function. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
BenitoB Posted January 25, 2013 Author Share Posted January 25, 2013 (edited) Ok. Here is what I have so far which works fine if I give the direct path to the client's home share and add at the end Netmapdrive.bat. That's the specific folder and file I need to find and replace the servername. I just don't know how to create the loop to read down from one user to the other or the array. expandcollapse popup#include <GUIConstantsEx.au3> #include <ProgressConstants.au3> #include <SendMessage.au3> Local $source = FileOpen("Sharelist.txt", 0) Local $result = FileOpen("Result.txt", 1) Local $find, $replace ; Check if file opened for reading OK If $source = -1 Then MsgBox(0, "Error", "Unable to open source file.") Exit EndIf While $find = "" $find = InputBox("Find", "Old servername?", "", "", 300, 100) If $find <> "" Then ExitLoop ElseIf @error = 0 Then MsgBox(16, "Error", "Value can't be blank.") ElseIf @error = 1 Then Exit EndIf WEnd While $replace = "" $replace = InputBox("Replace with", "New servername?", "", "", 300, 100) If $replace <> "" Then ExitLoop ElseIf @error = 0 Then MsgBox(16, "Error", "Value can't be blank.") ElseIf @error = 1 Then Exit EndIf WEnd $confirm = MsgBox(65, "Find and replace servernae", "You are about to change every instance of " & $find & " by " & $replace & " in the clients mapdrive.bat") MsgBox(0,"",MsgBox) If @error = 2 Then Exit ; Create marquee status bar $hGUI = GUICreate("Please wait...gathering info", 420, 50) GUICtrlCreateProgress(10, 10, 400, 20, $PBS_MARQUEE) _SendMessage(GUICtrlGetHandle(-1), $PBM_SETMARQUEE, True, 50) GUISetState() ; Read lines of text until EOF is reached While 1 Local $line = FileReadLine($source) & "\Net\mapdrive.bat" If @error = -1 Then ExitLoop Local $fileContent = FileRead($line, FileGetSize($line)) ; Verify if share is reachable If FileExists($line) Then $fileContent = StringReplace($fileContent, $find, $replace) FileDelete($line) FileWrite($line, $fileContent) Else FileWriteLine($result, $line & @TAB & "Path not found or no access") EndIf WEnd ; Close all opened files FileClose($source) FileClose($result) GUIDelete() MsgBox(0, "Find and replace servername", "Execution completed") Exit Edited January 25, 2013 by BenitoB Added code tags Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 25, 2013 Moderators Share Posted January 25, 2013 BenitoB,When you post code please use Code tags - put [autoit] before and [/autoit] after your posted code. Then you get a scrolling box as you can see above now I have added the tags. 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 Link to comment Share on other sites More sharing options...
BenitoB Posted January 25, 2013 Author Share Posted January 25, 2013 Sorry. I was trying to do that but I did not know how. Thanks Link to comment Share on other sites More sharing options...
BenitoB Posted January 25, 2013 Author Share Posted January 25, 2013 (edited) So basically I know the servername and the root share from Sharelist.txt, I do not know the user folder/sharename (the variable) and I know the folder and the file name to search for (Netmapdrive.bat). So instead of doing a search trought all the directories I want to go directly to each client mapdrive.bat file. This will cut the execution time a lot. ex: server1rootshare1user1 (which is uknown)NetNetlogon.bat user2 (which is uknown)NetNetlogon.bat user3 (which is uknown)NetNetlogon.bat server1rootshare2user1 (which is uknown)NetNetlogon.bat user2 (which is uknown)NetNetlogon.bat user3 (which is uknown)NetNetlogon.bat Edited January 25, 2013 by BenitoB Link to comment Share on other sites More sharing options...
BenitoB Posted January 28, 2013 Author Share Posted January 28, 2013 (edited) Quick question: With _FileListToArray function I can't get it to read the variable $Path which is a list of shares. If I replace $Path with "server1share1$" it works like a charm but that creates an array for only one share at a time. The InitSharelist.txt file can have 10 or more root shares. What am I doing wrong? I am almost done by the way. That is the only part not working. #include<File.au3> #include <Array.au3> Local $Path = FileOpen("InitSharelist.txt", 0) Local $result = FileOpen("InitResult.txt", 1) Local $userresult = FileOpen("UserResult.txt", 1) ;$aArray = _FileListToArray($Path, "*", 2) (Does not work) $aArray = _FileListToArray("\\server1\share1$\", "*", 2) ;(Works fine) ; Write full array to file by string file name _FileWriteFromArray($result, $aArray, 1) FileClose($result) Local $result = FileOpen("InitResult.txt", 0) While 1 Local $users = FileReadLine($result) If @error = -1 Then ExitLoop Local $line = "\\server1\share1$\" & $users & "\Net\mapdrive.bat" Local $fileContent = FileRead($line, FileGetSize($line)) ; Verify if share is reachable If FileExists($line) Then FileWriteLine($userresult, $line & @TAB & "mapdrive file found") Else FileWriteLine($userresult, $line & @TAB & "Path not found or no access") EndIf WEnd FileClose($Path) FileClose($result) FileClose($userresult) Edited January 28, 2013 by BenitoB Link to comment Share on other sites More sharing options...
BrewManNH Posted January 28, 2013 Share Posted January 28, 2013 $Path contains the handle to the file you opened, not it's contents. You need to read the file, either one line at a time with FileReadLine, or the whole file with either FileRead or _FileReadToArray. Then you can use its contents to populate the _FileListToArray.See the help file for the above commands. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
BenitoB Posted January 28, 2013 Author Share Posted January 28, 2013 Excellent. Is there one process faster than the other? Is there a prefered way to do it? Thanks Link to comment Share on other sites More sharing options...
BrewManNH Posted January 28, 2013 Share Posted January 28, 2013 If it's a large file, and if every path entry is on it's own line, _FileReadToArray would probably be the easiest to use. Then you can use a For loop to go through each entry in the array one path at a time. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
BenitoB Posted January 31, 2013 Author Share Posted January 31, 2013 Thank you for your pointers BrewManNH. My script works perfectly now. Link to comment Share on other sites More sharing options...
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