kapilpatry
asked on
vbscript to read config file and copy files
Hi, I need help in this vbscript. Purpose is to read a config.ini file which has some info like:
[GROUP1]
server1
server2
server3
[GROUP2]
server1
server2
server3
[GROUP3]
server1
server2
server3
The script should read each GROUP seperately in the .ini file and copy the logs from say: d:\logs to \\BACKUPServer\o$\logs. The requirement is to copy only the new files and not all the files that exist in the source path. Also, the script should create seperate GROUP folders and server folders on the destination path.
I am able to copy the files from source to destination using FileSystemObject but I am stuck at how to read each GROUP in the .ini file and do the recursive task of creating the respective GROUP,server folders and copy only the new logs.
Any help / suggestions / ideas is much appreciated.
[GROUP1]
server1
server2
server3
[GROUP2]
server1
server2
server3
[GROUP3]
server1
server2
server3
The script should read each GROUP seperately in the .ini file and copy the logs from say: d:\logs to \\BACKUPServer\o$\logs. The requirement is to copy only the new files and not all the files that exist in the source path. Also, the script should create seperate GROUP folders and server folders on the destination path.
I am able to copy the files from source to destination using FileSystemObject but I am stuck at how to read each GROUP in the .ini file and do the recursive task of creating the respective GROUP,server folders and copy only the new logs.
Any help / suggestions / ideas is much appreciated.
i don't understand what the config file does specify at this moment. Could you please explain little bit more detailed. Where's the script executed...
You repeated Server1, Server2, Server3 across all three groups. Do you really mean that any given server will be in at most one group?
ASKER
The config file would exist on the same server from where the script is executing from. In our env., the servers are divided into groups. Each group will have different servers with unique server names in each GROUP.
[GROUP1]
111
222
333
[GROUP2]
444
555
666
[GROUP3]
777
888
999
For each GROUP server, the script should copy over the logs to the BACKUP server with the exact GROUP foldername, exact server folder name and should only copy the new files. Hope this is more clear to understand my problem. Please let me know if you need more info.
Thanks.
[GROUP1]
111
222
333
[GROUP2]
444
555
666
[GROUP3]
777
888
999
For each GROUP server, the script should copy over the logs to the BACKUP server with the exact GROUP foldername, exact server folder name and should only copy the new files. Hope this is more clear to understand my problem. Please let me know if you need more info.
Thanks.
The below listed script should be executed on the backup server. If you do not want this you have to customize the strBackupFolder.
It does the following.
1. Opens the ini files located on D:\
2. Loop throught the hole file
3. If the current line contains "[" Then it notes this is a group. Then it checks if that group folder is existing. If not it will create it
4. If the current line contains not a "[" then it notes its a server. Then it checks if that server folder is existing. If not it will create it
5. After this it runs robocopy /MIR which does only copy new files from the server directory \\<server>\d$\Logs to D:\Backup\<Group>\<Server>
Check it out and please give feedback if it works, as i cannot test it
It does the following.
1. Opens the ini files located on D:\
2. Loop throught the hole file
3. If the current line contains "[" Then it notes this is a group. Then it checks if that group folder is existing. If not it will create it
4. If the current line contains not a "[" then it notes its a server. Then it checks if that server folder is existing. If not it will create it
5. After this it runs robocopy /MIR which does only copy new files from the server directory \\<server>\d$\Logs to D:\Backup\<Group>\<Server>
Check it out and please give feedback if it works, as i cannot test it
set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")
set objINI = objFSO.OpenTextFile("D:\input.ini")
strBackupFolder = "D:\Backup"
Do while not objINI.AtEndOfStream
strCurrentLine = objINI.ReadLine
If Instr(strCurrentLine,"[") Then
strCurrentGroup = Replace(Replace(strCurrentLine,"[",""),"]","")
strGroupFolder = strBackupFolder &"\" &strCurrentGroup
If Not objFSO.FolderExists(strGroupFolder) Then objFSO.CreateFolder(strGroupFolder)
ElseIf strCurrentLine <> "" Then
strCurrentServer = strCurrentLine
strServerFolder = strGroupFolder &"\" &strCurrentServer
If Not objFSO.FolderExists(strServerFolder) Then objFSO.CreateFolder(strServerFolder)
strSourceFolder = "\\" &strCurrentServer &"\d$\Logs"
objShell.Run "robocopy.exe " &strSourceFolder &" " &strServerFolder &" /MIR /LOG+:D:\Copy.log",0,true
Else
'Do Nothing
End If
Loop
ASKER
Thanks merowinger. I will test this out and let you know the results shortly.
ASKER
merowinger.. I am having problems in getting the vbscript to detect the robocopy.
I placed robocopy.exe in c:\windows\system32 on the server and also in the folder from where the script is executing. But the script errors out that its unable to find the file specified robocopy.exe
Thanks for your help.
I placed robocopy.exe in c:\windows\system32 on the server and also in the folder from where the script is executing. But the script errors out that its unable to find the file specified robocopy.exe
Thanks for your help.
ASKER
Please ignore my previous question. I got Robocopy to work. I am still testing with slight modifications to the script. Problem is there must be atleast 40 servers and from each there must be hundreds of logs that need to be moved. Do you forsee any problems in the script execution time or do you recommend any sleep time within the script for each iteration.
Please suggest some best practices.
Thanks.
Please suggest some best practices.
Thanks.
ypu can also direct the robocopy.exe to a path like this:
objShell.Run "C:\windows\system32\roboc opy.exe " &strSourceFolder &" " &strServerFolder &" /MIR /LOG+:D:\Copy.log",0,true
Could you please post your current version and the line and error which occurs
objShell.Run "C:\windows\system32\roboc
Could you please post your current version and the line and error which occurs
The folder-creation part should not cause any problems, but if one server is not available the robocopy job will fail. Better would be to first check connection to the server and then continue.
I've attached a customized script which will first check the connection to the computer and then copies the file
Note:The true at the end of the following line means that the vbscipt will wait until the robocopy job has stopped and will then continue. If you do not want the script to stop, you can set it to false. This will cause the process to running faster, but can also make some problems.
objShell.Run "C:\windows\system32\roboc opy.exe " &strSourceFolder &" " &strServerFolder &" /MIR /LOG+:D:\Copy.log",0,true
I've attached a customized script which will first check the connection to the computer and then copies the file
Note:The true at the end of the following line means that the vbscipt will wait until the robocopy job has stopped and will then continue. If you do not want the script to stop, you can set it to false. This will cause the process to running faster, but can also make some problems.
objShell.Run "C:\windows\system32\roboc
set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")
set objINI = objFSO.OpenTextFile("D:\input.ini")
strBackupFolder = "D:\Backup"
Do while not objINI.AtEndOfStream
strCurrentLine = objINI.ReadLine
If Instr(strCurrentLine,"[") Then
strCurrentGroup = Replace(Replace(strCurrentLine,"[",""),"]","")
strGroupFolder = strBackupFolder &"\" &strCurrentGroup
If Not objFSO.FolderExists(strGroupFolder) Then objFSO.CreateFolder(strGroupFolder)
ElseIf strCurrentLine <> "" Then
strCurrentServer = strCurrentLine
strServerFolder = strGroupFolder &"\" &strCurrentServer
If Not objFSO.FolderExists(strServerFolder) Then objFSO.CreateFolder(strServerFolder)
strSourceFolder = "\\" &strCurrentServer &"\d$\Logs"
'First check connection to the server
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
Set colPing = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" &strCurrentServer &"'")
For each objPing in colPing
If objPing.StatusCode = 0 Then
On Error Resume Next
objShell.Run "robocopy.exe " &strSourceFolder &" " &strServerFolder &" /MIR /LOG+:D:\Copy.log",0,true
On Error Goto 0
Else
Wscript.echo "Server not available"
End if
Next
End If
Loop
ASKER
merowinger. Thanks for the ping check. Do you think I should remove /MIR and keep /XO switch? The reason for this is, on all the servers, the logs are located in d$\Logfiles\
and I want to use /XO so that I only want the new files to copy from source to destination.
Thanks.
and I want to use /XO so that I only want the new files to copy from source to destination.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Merowinger.
The script worked out perfectly as required!!
The script worked out perfectly as required!!