VBScript Help - Copy Files!

Jay_2

2[H]4U
Joined
Mar 20, 2006
Messages
3,583
I have a situation where I need to replace files on a PC only if they exist.

eg

we have a program (old access program accessing SQL DBs!) that has login permissions controlled by the actual startup file. If you need more permissions you need a different startup file.

These files have the DB name hardcoded and we have now decided to move the DB to a new server and give it a more relivent name but this means changing every single startup file on every persons PC. The issue here is that depending on what permissions they have depends on what file they need replacing.

I need a script that can read from a text file a list of file names, and then only if the user already has that file copy the new one over the top.

up to now I have managed to sort the If the file already exists copy over the top if not do nothing side of the script and created a file to check for to stop the script applying over and over again on each login

Code:
'Set Dimension
DIM fso

'Set Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Create Condition
If (fso.FileExists("c:\destfolder\donotdelete.txt")) Then
	WScript.Quit()
	Else

Const DestinationFile = "c:\destfolder\anyfile.txt"
Const SourceFile = "c:\sourcefolder\anyfile.txt"
	Set fso = CreateObject("Scripting.FileSystemObject")
    'Check to see if the file already exists in the destination folder
    If fso.FileExists(DestinationFile) Then
        'Check to see if the file is read-only
        If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
                'The file exists and is not read-only.  Safe to replace the file.
                fso.CopyFile SourceFile, "C:\destfolder\", True
				Set filesys = CreateObject("Scripting.FileSystemObject")
				Set filetxt = filesys.CreateTextFile("c:\destfolder\donotdelete.txt", True)
        Else 
                'The file exists and is read-only.
                'Remove the read-only attribute
                fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
                'Replace the file
                fso.CopyFile SourceFile, "C:\destfolder\", True
                'Reapply the read-only attribute
                fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
				Set filesys = CreateObject("Scripting.FileSystemObject")
				Set filetxt = filesys.CreateTextFile("c:\destfolder\donotdelete.txt", True)
        End If
    Else
    End If
Set fso = Nothing
end if

But the above code only works for single files. Can any one help me intergrate and read from text file for file name into this?

Thanks
 
Last edited:
So is the only issue they need to be pointed to the new SQL server? Nothing else in the file changes except for the server name? If that's the case you can probably just use cliconfg to make an alias for the SQL client and be done.

But in regards to your script you can go two routes. The route your thinking of, using a text file with the possible files that might exist that need replacing, you will need to open the text file to read, read through each line and set your files to the value in the row. It would look something like:

Set readingTextfile = objFSO.OpenTextFile("yourtextfile.txt",1)

Do Until readingTextFile.AtEndOfStream
Set variables
Use the logic you already have
Loop

The other route would be just toss the file names in your script by adding them to an array and loop through the array. This would make it a little concise since you're not going to another file to read in the names and you don't have to open a file stream to get the names. Hope that helps
 
Thanks for your help, in the end I just did this

@echo off

if exist c:\destfolder\donotdelete.txt goto _END

xcopy.exe /U C:\sourcefolder C:\destfolder /Y

echo files moved to this directory on %date% at %time% >> c:\destfolder\donotdelete.txt

REM END OF SCRIPT
:_END
 
Back
Top