Jump to content

VBScript to check if folder exists, then if it does copy a file into it


Recommended Posts

Posted

Hi there,

 

I am trying a write a VB Script to do the following:

 

Check if a folder exists - if it doesn't END

- If it does check if two files exist within it

- if they do END

- if they don't, copy the two files

 

Basically I want to check that two files exist within a folder of some software that might be installed. I want to copy the two files if the software is installed, but not if the software isnt installed.

 

Hope that makes sense, and any help is greatly appreciated.

 

Many thanks! :confused:

Posted (edited)

along the lines of............

 

folderPath = "D:\FolderPath" 'Path of the folder to check (Do not add a \ at the end off the variable)
file1 = "file1.ext" 'Name and extension of 1st file to check
file2 = "file2.ext" 'Name and extension of 2nd file to check
copyLocation = "D:\CopyLocation" 'location of files to copy from if they dont exist (Do not add a \ at the end off the variable)

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(folderPath) Then
'Check to see if the files were found and if not copy the one that wasnt found (Or both)
If objFSO.FileExists(folderPath & "\" & file1) = False Then
	objFSO.CopyFile copyLocation & "\" & file1, folderPath & "\" & file1, false
End If

If objFSO.FileExists(folderPath & "\" & file2) = False Then
	objFSO.CopyFile copyLocation & "\" & file2, folderPath & "\" & file2, false
End If
End If

 

.........?

 

I have very quickly tested it using local paths. Network paths should also work though.

 

It might not be the neatest script in the world but I just threw it together quickly while I wait for a VM to install.

Edited by iMash
Alot shorter code :-)

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



×
×
  • Create New...