Jump to content
Sign in to follow this  
BenitoB

Modify a sring in a .bat file in multiple shares in a specific subfolder\file

Recommended Posts

BenitoB

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 on

I need help on how to get the script to go from one usershare to the other.

Thanks

Share this post


Link to post
Share on other sites
BenitoB

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.

#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 by BenitoB
Added code tags

Share this post


Link to post
Share on other sites
Melba23

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


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
BenitoB

Sorry. I was trying to do that but I did not know how. Thanks

Share this post


Link to post
Share on other sites
BenitoB

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 by BenitoB

Share this post


Link to post
Share on other sites
BenitoB

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 by BenitoB

Share this post


Link to post
Share on other sites
BrewManNH

$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 Gude
How 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

Share this post


Link to post
Share on other sites
BenitoB

Excellent. Is there one process faster than the other? Is there a prefered way to do it?

Thanks

Share this post


Link to post
Share on other sites
BrewManNH

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 Gude
How 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

Share this post


Link to post
Share on other sites
BenitoB

Thank you for your pointers BrewManNH. My script works perfectly now.

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  

×