Jump to content

Recommended Posts

Posted

Can anyone help with writing a script that will find a file in any directory, rename that file with extention .old, and copy an updated version to the location the file is located.

The issue is that the directory is unknown or there is no consistency on what the directory is named and this needs to be performed on all network computers.

Example \\server1\c$\folder1\filename.txt \\server2\d$\folderx\folder123\filename.txt (These represent the inconsistency to where the filename.txt resides on any server.)

Filename.txt needs to be renamed to filename.txt.old and a new filename.txt need to be copied to that location.

Can anyone help me. I am stuck.

Thanks,

KL


Posted

what is the actual filename? Is it going to be the same on every machine and will there be only one of them?

If the folder is unique from machine to machine your choices would be to search the machine for the filename though you may find more than one, then what will you do. Or place a known file or registry setting on each machine with path to the file.

Which route do you want to go, or is there another way you could find the desired file. WHat do you have so far.

Posted
what is the actual filename? Is it going to be the same on every machine and will there be only one of them?

If the folder is unique from machine to machine your choices would be to search the machine for the filename though you may find more than one, then what will you do. Or place a known file or registry setting on each machine with path to the file.

Which route do you want to go, or is there another way you could find the desired file. WHat do you have so far.

Iceman,

The servers are all webservers. Unfortunally the website names are all different and the directory structure maybe be different. For instance, some websites may reside on the D drive and some may reside on the C drive However the file name is the same on all the servers. (i.e C:\webapp\site\sitename1\folder1\filename.txt C:\webapp\site\sitename2\folder1\filename.txt D:\webapp\site\sitename3\folder1\filename.txt)

There is no consistancy on the webservers which is the problem. The file in question needs to be renamed with a .old extention, and an new file needs to be copied to that same location. The file doesnt reside anywhere else but in a subdirectory under the sitename.

This is what I have so far and you can see the issue. The script reads from a textfile that has all the webserver IP addresses. The first issue is that the "Trainingsite" directory in the string is always different on every webserver. Also the file needs to be renamed not copied over and an new file need to be copied to that location. Whew! I hope that was descriptive enough. Do you have any ideas or an easier way of doing this.

Const ForReading = 1

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\webservers.txt")

Do Until objFile.AtEndOfStream

strComputer = objFile.ReadLine

strRenameFile = "\\" & strComputer & "\c$\WebAPP\Sites\Trainingsite\Train\SharedControls\test.txt"

objFSO.CopyFile "C:\Scripts\test.txt", strRemoteFile, OverwriteExisting

Loop

Posted

if the file will always reside somewhere within \WebApp on some hard drive, I would perform a search of that directory on each machine for the desired file. First search the drive for the webapp folder and if it exists the search that folder structure for your desired file.

You can do one of two things to rename the file either use the FileSystemObject.MoveFile to move it within the same directory but to a different name, or CopyFile to a new name and then overwrite the old file with the new.

Posted
if the file will always reside somewhere within \WebApp on some hard drive, I would perform a search of that directory on each machine for the desired file. First search the drive for the webapp folder and if it exists the search that folder structure for your desired file.

You can do one of two things to rename the file either use the FileSystemObject.MoveFile to move it within the same directory but to a different name, or CopyFile to a new name and then overwrite the old file with the new.

Im pretty new a vbscript. Can you give some insight on the search command for that file. Once the search is complete and it finds the file, how can I tell it to rename or Movefile from the search results?

Everytime I try it errors out.

Posted

Try this script in a test, I have tested it on my computer, and it works.

I have no network so you will have to modified the top part of the script

too get it to work on a network.

Save As ReplaceFile.vbs

Dim strComputer
'/-> Set To Run On A Local Computer Add a Ip Address Or Computer Name
'/-> On The Network To Get It To Run On The Target Computer
'/-> EG : strComputer = "123.456.321.654"
'/-> EG : strComputer = "My Network Computer Name"
strComputer = "."
'/-> Objects To Work With
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim WMI :Set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'/-> Varibles For The File Copy Place Path Here
Dim CopyThis, CT1
CopyThis = "Testing.vbs"
'/-> Check To Make Sure Replacement File Exists
If Fso.FileExists(CopyThis) Then
Set CT1 = Fso.GetFile(CopyThis)
Work()
Else
MsgBox "Missing The Replacement File",4128,"Error Missing File"
WScript.Quit
End If
'/-> Varibles Used By The WMI Collection
Dim F1, F2, F3
Dim colFiles, objFile
'/->
Function Work()
'/-> Change This FileName = 'test' and Extension = 'vbs'" To What You Need
Set colFiles = WMI.ExecQuery _
("Select * From CIM_DataFile Where FileName = 'test' and Extension = 'vbs'")
'/-> Loop To Search All HD On The Target Computer
For Each objFile in colFiles
'/-> These Are To Show What You Can Use From This WMI Class
'/-> Varibles As Objects
F1 = objFile.Drive & objFile.Path
F2 = objFile.FileName & "." & objFile.Extension
F3 = objFile.Name
'/-> To Build A Path To Server
If InStr(F1,"\") Then F1 = Replace(F1,"\","\\")
If InStr(F3,"\") Then F3 = Replace(F3,"\","\\")
'/-> Rename By Copy
objFile.Copy(F1 & objFile.FileName & ".old")
'/-> Then Delete The Old
objFile.Delete()
'/-> Copy The Replacement File
CT1.Copy(F1 & "test.vbs")
Next
End Function

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...