Hello everyone,
I hope I am posting this question in the write forum. I looked through all the other ones and this seemed like the best place. If not i appologize in advance.
I have a code that I wrote in VBS to copy contents from one folder to another. I made it so that it wont overwrite the folder if it already exists, but now i want to expand on this. I want to make it so that it will copy folders from one to the other but it will update the contents inside the folder. If a file already exisits in the folder dont overwrite but if a file does not exisit then add that file.
Appreciate any help with this
thanks!
Here is what i have so far. -
-
Option Explicit
-
Dim strSourceDir, strDestinDir, FSO
-
const OverwriteExisting = False
-
-
strSourceDir = "I:\TestFolder1\*.*"
-
strDestinDir = "I:\TestFolder2\"
-
-
on error resume next
-
set FSO = CreateObject("Scripting.FileSystemObject")
-
FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
-
6 8032
This code checks if a file exists in the destination folder.
if not, it copys the file.
PS: you have to add the Reference "Microsoft scripting runtime" for the definition of the type "File" and "Folder" and "FileSystemObject" ! - Private Sub Command1_Click()
-
Dim strDestinDir As String
-
Dim FSO As New FileSystemObject
-
Dim THE_FILE As File
-
Dim START_FOLDER As Folder
-
strDestinDir = "c:\xfer\"
-
Set START_FOLDER = FSO.GetFolder("c:\temp\")
-
On Error Resume Next
-
For Each THE_FILE In START_FOLDER.Files
-
If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then
-
MsgBox ("File exist")
-
Else
-
FSO.CopyFile THE_FILE, strDestinDir
-
End If
-
Next
-
End Sub
Hi ggeu,
Sorry it took me a while to write back to you on this. I have been working on other things and I just got back around to working on this again.
Appreciate the help on this. I took what you changed and pluged it in and so far im having no luck. I made some adjustments and it will still copy the folder if it does not exist but if a folder is missing a file it still will not copy the new files in to the folder. Any ideas?
Thanks again helping me with this! -
Option Explicit
-
Dim strSourceDir, strDestinDir, FSO
-
Dim THE_FILE
-
Dim START_FOLDER
-
-
THE_FILE = ".txt"
-
START_FOLDER = "*.Folder"
-
-
-
const OverwriteExisting = False
-
-
strSourceDir = "I:\TestFolder1\*.*"
-
strDestinDir = "I:\TestFolder2\"
-
-
on error resume next
-
set FSO = CreateObject("Scripting.FileSystemObject")
-
FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
-
-
For Each THE_FILE In START_FOLDER.Files
-
If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then
-
MsgBox ("File exist")
-
Else
-
FSO.CopyFile THE_FILE, strDestinDir
-
End If
-
Next
-
-
I have a question: has the main folder also sub-folders? or are there only files?
If there are only files, then we can use my code.
If there are also sub-folders, then we have to analyze each folder and there files because the function "copyfolder" copy's only FOLDERS and it's CONTENTS in block.
The const "OverwriteExisting" checks only if the FOLDER exists !!
If the main folder exists and OverwriteExisting=False then no sub-folders or files will be copied
The VB help is:
"overwrite: Optional. Boolean value that indicates if existing folders are to be overwritten. If True, files are overwritten; if False, they are not. The default is True. "
If there are subfolders: this is the code to check each subfolder and file (see attachment).
how it works:
command1 calls the function for checking the sourcedir on files and subfolders.
- if there are files, it checks if the files exists in the destindir.
If not, it copies the file.
- if there are subfolders: it checks if the subfolder exists in the destindir, if not, it will be created.
AND it calls the SAME function for all the files and subfolders in this subfolder.
Hi ggeu,
I keep coming back around to this just takes me a little bit. Well to let you know there are sub folders in the main folder, then in the sub folders are all of the files. So when i used the orginal code I wrote it would just copy all the sub folders from the main folder to another folder and all the files, but if a file in one of the sub folders was say updated, deleted or added, it would not copy this change over.
I am looking at the files you had in the zip, wow, that is a lot of stuff. Thank you very much for putting that together. Only question i have now is how does it work?? I cant seem to get it to work so far.
Thanks again :)
I hope this attached Doc or PDF will help you in understanding the workflow.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: News |
last post by:
Is it possible to delete a file by copying it to the "bit bucket" or
"null device"?
Back in my youth when I live in VMS-land you could delete a file by
copying it to NL:
==========
I have...
|
by: Johnny |
last post by:
Hi,
I have created an ASP.NET application (let's call it FooBar) with VS.NET on
my local machine, residing in http://localhost/FooBar. Deploying it to
another folder on my machine works well...
|
by: Simon Abolnar |
last post by:
I am working on problem of coping folder.
I am using Windows XP/VB.Net 2003
I know how to do it with FileSystemObject, but I would like to have copy
dialog between operation like it is in...
|
by: magmo |
last post by:
Hi
I need to have a folder with some files in it copied to the clients
computer. How can I configure this when I deploy the application I've
developed?
Regards
|
by: Pascal Polleunus |
last post by:
Hi,
I need to synchronize some tables from a database (master) to another
one (slave).
Both servers are running Debian Woody with PostgreSQL 7.2.1 (postgresql
7.2.1-2woody4).
The databases are...
|
by: al_johnson222 |
last post by:
>From any page, I want to be able to call a JS function that will do the
equivelant of select all, and copy. This data will then be posted to a
page that will log it.
This would be easy using...
|
by: job.noam |
last post by:
Hello,
I have very weird problem on my server, when I run this function:
Sub TransferFiles(departTemp_id,depart_id)
'Copy the files to the depart from the template
'--
Dim objFolder
Dim...
|
by: fanoftvb |
last post by:
Hi, i need to copy a folder to another folder. Can someone show me how to do it? I'm using vb 2005 Thanks.
|
by: wish |
last post by:
Hi, How to copy folder in linux thru network?
|
by: usman |
last post by:
Hi
I have a windows service that backups a folder onto another location on the
same computer. The service is written in C#. The size of the original folder
is large i.e. over 8 GB. Also the...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |