|
-
Jan 11th, 2005, 06:19 AM
#1
Thread Starter
Junior Member
Code to copy a folder
Hello all,
I am wanting to copy a folder from a server to a target PC and rename it, so far I can verify that the folder exists on the server and isn't already on the target PC but I cannot find any code that will copy and rename the folder.
I am using VB6 an really need some help, if this is not possible in VB can you tell me haw to call a dos command through VB as I know the code for this in DOS.
Thanks
-
Jan 11th, 2005, 06:24 AM
#2
Addicted Member
Re: Code to copy a folder
I am not sure about the vb code for copying the folder, but just shell a batch file that has your code in it.
Sherminator ~ I'll be back.
-
Jan 11th, 2005, 06:39 AM
#3
Lively Member
Re: Code to copy a folder
Shell may return before the task is done (asynchronus!), so be sure all files are copied before you continue
A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.
-
Jan 11th, 2005, 06:44 AM
#4
Addicted Member
Re: Code to copy a folder
Does anyone know of a function, or anything for that matter, that would allow you to know when the operation is completed? I have no specific need to know, but the code may become useful later...
Sherminator ~ I'll be back.
-
Jan 11th, 2005, 07:04 AM
#5
Lively Member
Re: Code to copy a folder
Brute force: before starting the batch, create a file somewhere. can be empty.
towards the end of the batch, delete that file out of the batch.
the VB app can occasionally look if the file is still there. Once it's gone, we know the batch is done. Along these lines, might be a good idea to pipe the echos from the batch into a file which can be verified by the vb app to make sure the batch performed as desired.
one could also try to set an environment variable, but I believe VB can only read them, therefore we couldn't clean up unless another batch would do that...
A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.
-
Jan 11th, 2005, 07:15 AM
#6
Re: Code to copy a folder
You could always use the CopyFolder method of the FileSystemObject within the Microsoft Scripting Runtime, and say something like
VB Code:
Private Sub cmdMakeItHappen_Click()
Dim fso As FileSystemObject
Dim target_folder As Folder
Set fso = New FileSystemObject
fso.CopyFolder "H:\MyFolder\", "C:\MyFolder\"
MsgBox "I am done"
End Sub
-
Jan 11th, 2005, 08:50 AM
#7
Thread Starter
Junior Member
Re: Code to copy a folder
Thanks Hack that works a charm, I now i pushing it but how would I change the folder security to give users write and modify access to the folder?
-
Jan 11th, 2005, 08:56 AM
#8
Re: Code to copy a folder
VB Code:
SetAttr "c:\myfolder\", vbNormal
-
Jan 11th, 2005, 09:15 AM
#9
Thread Starter
Junior Member
Re: Code to copy a folder
I have tried the above but the user folder permissions are still read only [we only give users read access to the C:\ we setup folders with write and modify if it is needed] any help will be appriciated.
In DOS you can change the CACLS on a folder:
source = c:\myfolder
cacls source /t /e /g users:c
would using the shell command be the best way around this? and how would I do this [I am new to VB]
-
Jan 11th, 2005, 09:21 AM
#10
Re: Code to copy a folder
SetAttr has always worked for me. How are you using it?
-
Jan 11th, 2005, 09:31 AM
#11
Thread Starter
Junior Member
Re: Code to copy a folder
I just added it to the end of your code tike this:
Private Sub cmdMakeItHappen_Click()
Dim fso As FileSystemObject
Dim target_folder As Folder
Dim Source = c:\myfolder
Set fso = New FileSystemObject
fso.CopyFolder "H:\MyFolder\", Source
MsgBox "I am done"
SetAttr Source, vbNormal
End Sub
-
Jan 11th, 2005, 09:35 AM
#12
Re: Code to copy a folder
You are getting a little ahead of yourself.
Modify your code ever so slightly as follows:
VB Code:
'not
Dim Source = c:\myfolder
'but
Dim Source As String
Source = "C:\myfolder"
Then try it.
-
Jan 11th, 2005, 09:51 AM
#13
Thread Starter
Junior Member
Re: Code to copy a folder
Its still not working, where does SetAttr Source, vbNormal get its permissons from? if it looks at the root directory in this case [C:\] then it will keep them as read only. I am not permitted to alter the permission of the directory as a whole only folders which need write and modify can be changed [the domain admins stop me from doing this]
-
Jan 11th, 2005, 09:59 AM
#14
Re: Code to copy a folder
Just for the sake of having alternative method (and also because I am not a big fan of FSO) here is another approach that uses Windows API:
VB Code:
Option Explicit
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Const FOF_MULTIDESTFILES = &H1
Private Const FOF_CONFIRMMOUSE = &H2
Private Const FOF_SILENT = &H4
Private Const FOF_RENAMEONCOLLISION = &H8
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_WANTMAPPINGHANDLE = &H20
Private Const FOF_CREATEPROGRESSDLG = &H0
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_FILESONLY = &H80
Private Const FOF_SIMPLEPROGRESS = &H100
Private Const FOF_NOCONFIRMMKDIR = &H200
Private Const FO_MOVE = 1
Private Const FO_COPY = 2
Private Const FO_DELETE = 3
Private Const FO_RENAME = 4
Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
'==========================================================================================
Dim varFOS As SHFILEOPSTRUCT
With varFOS
.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
.wFunc = FO_COPY
.pFrom = strSource
.pTo = strDest
End With
Call SHFileOperation(varFOS)
CopyFolder = (varFOS.fAnyOperationsAborted = 0)
End Function
Private Sub Command1_Click()
CopyFolder "c:\temp\test1", "c:\temp\test2"
End Sub
-
Jan 11th, 2005, 10:05 AM
#15
Re: Code to copy a folder
 Originally Posted by AndyW
Its still not working, where does SetAttr Source, vbNormal get its permissons from? if it looks at the root directory in this case [C:\] then it will keep them as read only. I am not permitted to alter the permission of the directory as a whole only folders which need write and modify can be changed [the domain admins stop me from doing this]
If you can modify the attributes from DOS, you should be able to modify the attributes from VB. Can you modify the attributes from DOS?
(RhinoBull: I'm not a big fan of FSO either, however, it does have the one redeeming factor of being brief and easy to understand)
-
Jan 11th, 2005, 10:08 AM
#16
Thread Starter
Junior Member
Re: Code to copy a folder
Yes it works fine when using dos
-
Jan 11th, 2005, 10:08 AM
#17
Re: Code to copy a folder
 Originally Posted by Hack
... (RhinoBull: I'm not a big fan of FSO either, however, it does have the one redeeming factor of being brief and easy to understand)
Indeed.
-
Jan 11th, 2005, 10:20 AM
#18
Re: Code to copy a folder
 Originally Posted by AndyW
Yes it works fine when using dos
Well, I'm not sure what to tell you Andy. I have issue whatsoever using SetAttr on my PC to manipulate either folder or file attributes.
I also have no idea what this means
In DOS you can change the CACLS on a folder:
source = c:\myfolder
cacls source /t /e /g users:c
In my old DOS days, I managed attributes using the DOS Attrib command, so maybe these are things that have been setup for you to do this kind of thing that VB can't get around.
-
Jan 11th, 2005, 10:39 AM
#19
Lively Member
Re: Code to copy a folder
Originally Posted by Hack
In my old DOS days, I managed attributes using the DOS Attrib command, so maybe these are things that have been setup for you to do this kind of thing that VB can't get around.
Well, at least at that time I was quite confident I understood what was happening inside that gray box, to a fair degree anyways. To be honest, now it's not only a few wild guesses... well, that's advantage for you...
Dinosaur's talk, I know, but to all you youngsters out there:
There was an existing universum prior to Windows!!!
A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.
-
Jan 11th, 2005, 11:02 AM
#20
Thread Starter
Junior Member
Re: Code to copy a folder
In Dos I would have this set in a batch file
set copyEnv=
set /p copyEnv=Please enter the environment you want to copy
cacls p:\%copyEnv% /t /e /g users:c
this would then change the requested folder's permissions to give users modify access
How would this be done in shell, when i try
dim copyEnv as string
copyEnv = "P:\" & textbox1.text
shell "cacls copyEnv /t /e /g users:c
dos opens and closes without running the command, what am i doing worng
-
Jan 11th, 2005, 11:29 AM
#21
Lively Member
Re: Code to copy a folder
you have to manually / programmatically create the batch file like
[Highlight=VB]
Dim i as Long
Dim Batchline(5) as String
'writing the batch in memory
Batchline(0)="REM This batch will perform the following: blah blah"
Batchline(1)= 'first command of batch
Batchline(2)= '2nd command of batch
'...etc..
'writing the batch to file
Open "C:\bopybat.bat" for output as #1
for i=0 to UBound(Batchline)
Print #1, Batchline(i)
Next
Close #1
'execute the batch
Shell "C:\copybat.bat"
A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.
-
Jan 12th, 2005, 03:44 AM
#22
Thread Starter
Junior Member
Re: Code to copy a folder
So using you code how can I get the batchfile to pickup a variable from the VB code:
Dim i as Long
Dim Batchline(5) as String
Dim copyEnv as string
copyEnv = "program files"
'writing the batch in memory
Batchline(0)="REM This batch will perform the following: blah blah"
Batchline(1)= "cacls p:\%copyEnv% /t /e /g users:c"
'writing the batch to file
Open "C:\copybat.bat" for output as #1
for i=0 to UBound(Batchline)
Print #1, Batchline(i)
Next
Close #1
'execute the batch
Shell "C:\copybat.bat"
so in this case I want to give a user modify access to program files is it possible.
Thanks for all your help so far guys
-
Jan 12th, 2005, 04:19 AM
#23
Lively Member
Re: Code to copy a folder
not sure I understand what exactly you mean
if the Evironment variable %CopyEnv% exists, you can read it
VB Code:
Dim SomeVar As String
SomeVar=Environ("copyEnv")
for the other parameters (i.e. /t /e /g users:c), put some checkboxes and/or option buttons on your form, where the user can choose their settings.
When they hit "OK", you will have to build a string according to the selections.
for instance, if you have a checkbox reading "include SubDirs":
VB Code:
If ChkSubDirs.Value=1 Then
Batchline(x) = BatchLine(x)+ "/T "
End If
Likely you will have to check that the parameters choosen make sense (not mutually exclusive etc).
I couldn't be more specific, this cacls business is absolutely new for me.
(as I mentioned earlier, I'm a dinosaur, in the good old days we had Attrib and even something from Novell Netware, I forgot...)
A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.
-
Jan 12th, 2005, 04:20 AM
#24
Re: Code to copy a folder & Attributes
the vb FILECOPY command will copy a folder. but, as long as you are using the FSO, you can use it's Attributes property.
use:
fso.attributes = fso.attributes +/- [new attributes]
fso.attributes = fso.attributes - 1
0 Normal
1 Read Only R/W
2 Hidden R/W
4 System R/W
8 Volume Disk Drive Volume Label Read only
16 Directory Folder or Directory Read only
32 Archive Has changed since last backup R/W
64 Alias Link or Shortcut Read only
128 Compressed Read only
Don't use batch files. They aren't necessary nowadays. I've had it drilled into my head recently.
-
Jan 12th, 2005, 04:37 AM
#25
Thread Starter
Junior Member
Re: Code to copy a folder
This is starting to go over my head [first time i have used vb]
CACLS changes folder permissions so with the dos command /T says change the permissions on the folder and subfolders /E just edit the permissions [like you would by right clicking and choosing securty] /G grant it to the users setting, users:c give them write access
all of these will not be optional [the guys using the program only need to enter the PC they want to copy to and the folder they want to copy]
The problem is I have no idea how to get this working with VB, I have a batch file that does it but apparently "its not user friendly".
How does this fso.attributes work with the code I have to copy the folder?
Sorry for all the questions but i am a noooooooooob
-
Jan 12th, 2005, 04:44 AM
#26
Re: Code to copy a folder
you should be able to use the SHELL command to use CACLS if it runs.
just use SHELL "c:\windows\cacls ..."
the way that you normally would.
after you pick an object in fso, you can then set its attributes. I don't think that is what you want though. You want the permissions.
Here is an overview:
Displays or modifies access control lists (ACLs) of files
CACLS filename [/T] [/E] [/C] [/G user:perm] [/R user [...]]
[/P user:perm [...]] [/D user [...]]
filename Displays ACLs.
/T Changes ACLs of specified files in
the current directory and all subdirectories.
/E Edit ACL instead of replacing it.
/C Continue on access denied errors.
/G user:perm Grant specified user access rights.
Perm can be: R Read
W Write
C Change (write)
F Full control
/R user Revoke specified user's access rights (only valid with /E).
/P user:perm Replace specified user's access rights.
Perm can be: N None
R Read
W Write
C Change (write)
F Full control
/D user Deny specified user access.
Wildcards can be used to specify more that one file in a command.
You can specify more than one user in a command.
Abbreviations:
CI - Container Inherit.
The ACE will be inherited by directories.
OI - Object Inherit.
The ACE will be inherited by files.
IO - Inherit Only.
The ACE does not apply to the current file/directory.
-
Jan 14th, 2005, 08:54 AM
#27
Thread Starter
Junior Member
Re: Code to copy a folder
As I could't work out how to pass a VB variable though shell I sorted this another way. I created this batch file:
p:
for /d %%f in ("ski*") do cacls %%f /t /e /g users:c
for /d %%f in ("bur*") do cacls %%f /t /e /g users:c
for /d %%f in ("hml*") do cacls %%f /t /e /g users:c
for /d %%f in ("sml*") do cacls %%f /t /e /g users:c
As the folders that can be copied will start with either of the above 3 letters I just make it alter the permissions on all folders it finds meeting the criteria.
Thanks for all you time and help
-
Jan 14th, 2005, 09:26 AM
#28
Re: Code to copy a folder
Out of curiosity: did you try that sample I posted in reply #14?
-
Jan 14th, 2005, 10:16 AM
#29
Thread Starter
Junior Member
Re: Code to copy a folder
Sorry I didn't only because I don't understand it [dont see the point in just copying code if you cannot follow what it is doing] I can follow the function and sub but nearly everything before that is greek to me from what i see it will copy a folder from source to destination. can you get to to set folder permissions aswell??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|