Jump to content

How can I copy files and folders keeping source attributes?


Recommended Posts

I have XP-Pro SP3 with Power Shell installed.

I need a script I can drag and drop files and folders onto that will copy them maintaining the attributes of the source.

Specifically most of my folders have distributable custom icons. Each folder contains both a "desktop.ini" file and an associated "FolderMarker.ico" the "desktop.ini" file points to. Both of these files have "system" and "hidden" attributes. In order for the folders to display the custom icons when copied to another computer these files have to keep the "system" attribute and I want the "hidden" attribute maintained as well.

I haven't been able to do this using VBscript. I have to use XXcopy which doesn't allow me to copy by just dropping the folders on the script. (VBscript does allow me to copy by dropping items on the script but the copied folder icons are not distributable because the "system" and "hidden" attributes of the "desktop.ini" and "FolderMarker.ico" are lost when copied by that method.

I would really appreciate a script to accomplish this. Below is the VBscript to which I refer. Thanks.

On Error Resume Next
Const OverwriteExisting = True
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objArgs.Count < 1 Then
WScript.Quit
End If
For I = 0 to objArgs.Count - 1

Arg = objArgs(I)

objFSO.CopyFile Arg, "C:\Program Files\", OverwriteExisting
objFSO.CopyFile Arg, "D:\0 aAPPS\00 a Install First\Program Files\", OverwriteExisting
objFSO.CopyFolder Arg, "C:\Program Files\", OverwriteExisting
objFSO.CopyFolder Arg, "D:\0 aAPPS\00 a Install First\Program Files\", OverwriteExisting

Next
WScript.Quit

Link to comment
Share on other sites


Not that I would write the script this way (it tries to copy every arg passed both as a file and a folder), but it does preserve S & H attributes on files such as desktop.ini.

However, that isn't enough for the OS to actually make use of the desktop.ini file i.e. custom icons are missing even if the proper file is copied in there (still with S & R set), even if it was copied by other means. You actually have to set the read only attribute on the folder for desktop.ini files to work, and that isn't copied over.

There is no perfect solution to this. You could copy everything over, and then going on a hunt, parsing every single folder, enumerating files, looking for anything named desktop.ini (and other similar names) and if found setting the R attribute on its parent folder. Or you could blindly set it on every folder and all of its sub-folders. Or if you know there are no sub-folders with custom icons, then you can simply check the attributes of the folder you're copying and then set that after on the newly created folder in the destination, kinda like this:


Option Explicit
Dim fso, folder, attr, arg, i

Const dst = "C:\Where\To\" 'must have the trailing backslash

Set fso = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count < 1 Then WScript.Quit
For i = 0 to WScript.Arguments.Count - 1
arg = WScript.Arguments(i)
If (fso.FileExists(arg)) Then
fso.CopyFile arg, dst, true
Else
Set folder = fso.GetFolder(arg)
attr = folder.Attributes
fso.CopyFolder arg, dst, true
Set folder = fso.GetFolder(dst + folder.Name)
folder.Attributes = attr
End If
Next

Link to comment
Share on other sites

I had forgotten my "Folder Marker.BAT" file makes the folder a system folder.

How would your script be written to copy a folder to multiple directories setting the attributes of the copied folder in each directory?

This is how I assign distributable custom icons to folders

First I create the custom icon and copy it to a folder named "Folder Marker" where I have a system file "desktop.ini" file and a "Folder Marker.BAT file.

FolderMarker.BAT

@ECHO OFF
echo "Enter FolderPath"
set /p FolderPath=


ATTRIB -h -s "%FolderPath%\FolderMarker.ico"
DEL /F /Q "%FolderPath%\FolderMarker.ico"

ATTRIB -h -s "%FolderPath%\Desktop.ini"
DEL /F /Q "%FolderPath%\Desktop.ini"

ren *ico FolderMarker.ico

xxcopy Desktop.ini "%FolderPath%\" /H /YY
xxcopy FolderMarker.ico "%FolderPath%\" /H /YY
ATTRIB +s "%FolderPath%"
ATTRIB +h +s "%FolderPath%\FolderMarker.ico"

DEL /F /Q FolderMarker.ico
exit

"desktop.ini"

[.ShellClassInfo]
IconFile=FolderMarker.ico
IconIndex=0

When I run the batch file it asks for the path of the folder for the custom icon, removes the attributes of the "desktop.ini" and "FolderMarker.ico", deletes them, renames my chosen icon as "FolderMarker.ico", copies both the renamed icon and the "desktop.ini" in the "Folder Marker" folder to the chosen folder, sets the attributes of the chosen folder to "s",sets the attributes of the copied icon in the chosen folder to "s" and "h", and then deletes the "FolderMarker.ico" remaining in the "Folder Marker" folder.

Since most of the folders I copy are system folders with custom icons, would making every folder and subfolder copied a system folder have an adverse effect if I used it for only copying folders with at the most 3 subfolders.

Thank you for taking the time to help me.

Link to comment
Share on other sites

I had forgotten my "Folder Marker.BAT" file makes the folder a system folder.

I'm just wondering why you would do this -- have a batch file along with the vbscript. The vbscript can easily take care of everything the batch file does (renaming, deleting, changing attributes...)

How would your script be written to copy a folder to multiple directories setting the attributes of the copied folder in each directory?

This stuff is trivial. At this point, what we need is to understand what gets copied where and so on properly -- basically what you're trying to accomplish.

Right now I'm more or less lost as to where the icons and desktop.ini files are coming from and going to. There seems to be some working directory, a "folder marker" one, then the other destination folders... You copy some files manually in one of them, then tell a batch file to get them from there and then it copies them into another place (instead of just saving the icon there in the first place). Then you run the vbscript to recopy it again elsewhere? I just can't seem to grasp how it all works.

I would help, but it's hard writing a script that does something you don't understand. You have made far more of an effort to describe it than most people normally do around here though, sorry I just can't make sense out of it.

Since most of the folders I copy are system folders with custom icons, would making every folder and subfolder copied a system folder have an adverse effect if I used it for only copying folders with at the most 3 subfolders.

Setting that attribute shouldn't harm anything.

Link to comment
Share on other sites

My Problem was solved on another forum and the script provided there worked perfectly!

There's many ways to skin a cat (it's just another way to copy files than I used, and there are some more too e.g. using WMI). I still think there would be a better way (not involving a batch + a vbscript), but seemingly you're happy with this anyway, and since I still don't know what it's meant to do (as in, the big picture) I can't really come up with anything else either.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...