Jump to content
Sign in to follow this  
bimini07

Copy one to many then rename using reference txt file

Recommended Posts

bimini07

Hi guys,

I am wondering if this is possible with Auto IT and if so how? Any direction would be appreciated:

The problem is two fold... I have a vnc config file that I need copied several times. The new name for the copied files need to be pulled from a text file and the files renamed as such. For example if you have a single config file named lets say AA.cfg this file needs to be copied lets say 1000 times, but the new names for the files need to be pulled from a txt file. So if the text file has the following entries:

BB

CC

DD

EE

The new files will be BB.CFG, CC.CFG etc..

Second is a search and replace scenario on the same files. After the renaming has been done... then execute an AutoIT script that checks the file name against another text file that containts two entries or two columns. The first column lists the name of the file such as BB, CC, DD and the second column lists the value that needs to be changed within these files... for example the first line of the text file would look like this:

BB, 192.168.0.1

CC, 192.168.1.1

DD, 192.168.1.2

So the script would look at the first line of the text file AA.. then open the file AA.CFG search for an entry that starts with HOST= and replaces that entry with the new IP Address. And do this till it's finished with all the entries in the text file...

Is this possible with AutoIT? and if so how?

Thanks,

Share this post


Link to post
Share on other sites
BrettF

Hi guys,

I am wondering if this is possible with Auto IT and if so how? Any direction would be appreciated:

The problem is two fold... I have a vnc config file that I need copied several times. The new name for the copied files need to be pulled from a text file and the files renamed as such. For example if you have a single config file named lets say AA.cfg this file needs to be copied lets say 1000 times, but the new names for the files need to be pulled from a txt file. So if the text file has the following entries:

BB

CC

DD

EE

The new files will be BB.CFG, CC.CFG etc..

Second is a search and replace scenario on the same files. After the renaming has been done... then execute an AutoIT script that checks the file name against another text file that containts two entries or two columns. The first column lists the name of the file such as BB, CC, DD and the second column lists the value that needs to be changed within these files... for example the first line of the text file would look like this:

BB, 192.168.0.1

CC, 192.168.1.1

DD, 192.168.1.2

So the script would look at the first line of the text file AA.. then open the file AA.CFG search for an entry that starts with HOST= and replaces that entry with the new IP Address. And do this till it's finished with all the entries in the text file...

Is this possible with AutoIT? and if so how?

Thanks,

Are you able to post the contents of the file or attach one? That would help. But why do you need it copied so many times?

Share this post


Link to post
Share on other sites
bimini07

Are you able to post the contents of the file or attach one? That would help. But why do you need it copied so many times?

I am attaching FileRename.txt and FileStringReplace.txt.... this is basically sample data.

The FileRename.txt will be used to rename the copied files, and the FileStringReplace.txt will be used to replace the HOST= value in the same config file.

We have 2000 machines that have VNC server installed on them. In order to avoid the end user lookup the machine name then search to find it's ip and then enter its password... and in the process of eliminating a ton of support calls. I want to be able to create a vnc config file for each machine that these users can double click and launch vnc.

The graphical end of this piece is already kinda sort of working. This is the next piece of puzzle that I need to get working.

Thanks,

FileRename.txt

FileReplaceString.txt

Share this post


Link to post
Share on other sites
covaks

Try this. However, I don't have vnc installed on this laptop, so I couldn't check what's in the .cfg files. So be aware that this script assumes that the "HOST=" line in each .cfg file is the only line that has "HOST=" in it..

#include <File.au3>

; Read FileRename.txt and make copies
$f = FileOpen("FileRename.txt",0)

While 1
    $line = FileReadLine($f)
    If @error = -1 Then ExitLoop
    FileCopy("AA.cfg",$line)
WEnd

FileClose($f)

$f2 = FileOpen("FileReplaceString.txt",0)
Dim $aTemp

While 1
    ; Read FileReplaceString and get the Filename and IP we want to work with
    $line = FileReadLine($f2)
    If @error = -1 Then ExitLoop
    $sTemp = StringSplit($line,",")

    ;Read each cfg file
    _FileReadToArray($sTemp[1],$aTemp)
    
    ; Find the line that has the HOST= in it, and replace whatever is there with our new HOST= line. 
    For $x = 1 to $aTemp[0]
        If StringInStr($aTemp[$x],"HOST=") Then
            _FileWriteToLine($sTemp[1],$x,"HOST=" & StringStripWS($sTemp[2],3),1)
        EndIf
    Next
WEnd

Share this post


Link to post
Share on other sites
bimini07

Try this. However, I don't have vnc installed on this laptop, so I couldn't check what's in the .cfg files. So be aware that this script assumes that the "HOST=" line in each .cfg file is the only line that has "HOST=" in it..

#include <File.au3>

; Read FileRename.txt and make copies
$f = FileOpen("FileRename.txt",0)

While 1
    $line = FileReadLine($f)
    If @error = -1 Then ExitLoop
    FileCopy("AA.cfg",$line)
WEnd

FileClose($f)

$f2 = FileOpen("FileReplaceString.txt",0)
Dim $aTemp

While 1
    ; Read FileReplaceString and get the Filename and IP we want to work with
    $line = FileReadLine($f2)
    If @error = -1 Then ExitLoop
    $sTemp = StringSplit($line,",")

    ;Read each cfg file
    _FileReadToArray($sTemp[1],$aTemp)
    
    ; Find the line that has the HOST= in it, and replace whatever is there with our new HOST= line. 
    For $x = 1 to $aTemp[0]
        If StringInStr($aTemp[$x],"HOST=") Then
            _FileWriteToLine($sTemp[1],$x,"HOST=" & StringStripWS($sTemp[2],3),1)
        EndIf
    Next
WEnd
Thanks covaks.. the first part works like a champ but... the second part it errors out at line 27 "For $x =1 to $aTemp[0]

saying error:Subscript used with non-Array variable...

What does this mean? I am assuming it is looking for an Array at $aTemp[0]?

Thanks

Share this post


Link to post
Share on other sites
covaks

Hmm, looks like it's not finding the file that should be in $sTemp[1] when it tries to _FileLoadToArray it into $aTemp. Could happen from blank lines or mis-spellings in the FileRenameString.txt file I guess.

Try this for now, it should make sure $aTemp is actually an array before processing it:

#include <File.au3>

; Read FileRename.txt and make copies
$f = FileOpen("FileRename.txt",0)

While 1
    $line = FileReadLine($f)
    If @error = -1 Then ExitLoop
    FileCopy("AA.cfg",$line)
WEnd

FileClose($f)

$f2 = FileOpen("FileReplaceString.txt",0)
Dim $aTemp


While 1
    ; Read FileReplaceString and get the Filename and IP we want to work with
    $line = FileReadLine($f2)
    If @error = -1 Then ExitLoop
    $sTemp = StringSplit($line,",")

    ;Read each cfg file
    _FileReadToArray($sTemp[1],$aTemp)

    ; Find the line that has the HOST= in it, and replace whatever is there with our new HOST= line. 
    If IsArray($aTemp) Then
        For $x = 1 to $aTemp[0]
            If StringInStr($aTemp[$x],"HOST=") Then
                _FileWriteToLine($sTemp[1],$x,"HOST=" & StringStripWS($sTemp[2],3),1)
            EndIf
        Next
    EndIf
    
    $aTemp = ""

WEnd

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  

×