473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using VBS to copy a folder?

283 100+
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3. Dim strSourceDir, strDestinDir, FSO
  4. const OverwriteExisting = False
  5.  
  6. strSourceDir = "I:\TestFolder1\*.*"
  7. strDestinDir = "I:\TestFolder2\"
  8.  
  9. on error resume next
  10. set FSO = CreateObject("Scripting.FileSystemObject") 
  11. FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
  12.  
Jun 14 '10 #1
6 8032
Guido Geurs
767 Expert 512MB
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" !

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim strDestinDir As String
  3. Dim FSO As New FileSystemObject
  4. Dim THE_FILE As File
  5. Dim START_FOLDER As Folder
  6.    strDestinDir = "c:\xfer\"
  7.    Set START_FOLDER = FSO.GetFolder("c:\temp\")
  8.    On Error Resume Next
  9.    For Each THE_FILE In START_FOLDER.Files
  10.       If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then
  11.          MsgBox ("File exist")
  12.       Else
  13.          FSO.CopyFile THE_FILE, strDestinDir
  14.       End If
  15.    Next
  16. End Sub
Jun 15 '10 #2
slenish
283 100+
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!

Expand|Select|Wrap|Line Numbers
  1. Option Explicit 
  2. Dim strSourceDir, strDestinDir, FSO 
  3. Dim THE_FILE  
  4. Dim START_FOLDER 
  5.  
  6. THE_FILE = ".txt"
  7. START_FOLDER = "*.Folder"
  8.  
  9.  
  10. const OverwriteExisting = False 
  11.  
  12. strSourceDir = "I:\TestFolder1\*.*" 
  13. strDestinDir = "I:\TestFolder2\" 
  14.  
  15. on error resume next 
  16. set FSO = CreateObject("Scripting.FileSystemObject")  
  17. FSO.CopyFolder strSourceDir , strDestinDir , OverWriteExisting
  18.  
  19. For Each THE_FILE In START_FOLDER.Files 
  20.       If FSO.FileExists(strDestinDir & Mid(THE_FILE, InStrRev(THE_FILE, "\") + 1)) Then 
  21.          MsgBox ("File exist") 
  22.       Else 
  23.          FSO.CopyFile THE_FILE, strDestinDir 
  24.       End If 
  25.    Next 
  26.  
  27.  
Jun 23 '10 #3
Guido Geurs
767 Expert 512MB
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. "
Jun 24 '10 #4
Guido Geurs
767 Expert 512MB
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.
Attached Files
File Type: zip Using VBS to copy a folder_v4.zip (6.2 KB, 449 views)
Jun 24 '10 #5
slenish
283 100+
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 :)
Jul 9 '10 #6
Guido Geurs
767 Expert 512MB
I hope this attached Doc or PDF will help you in understanding the workflow.
Attached Files
File Type: zip Using VBS to copy a folder_Flowchart_3.zip (16.9 KB, 308 views)
Jul 16 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
3
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...
3
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...
1
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
2
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...
2
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...
1
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...
1
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.
3
by: wish | last post by:
Hi, How to copy folder in linux thru network?
3
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.