Friday 22 August 2008

Batch programming tip #17: Moving/Copying hidden/system folders and files

A recent question about renaming a folder (on computing.net) brought to my attention the fact that move (which I usually use to rename folders) and also rename won't work on hidden/system files.

To avoid this 'issue' you must change the folder's system attributes.
First we define our original and destination folders.

set oldp=[define original folder here]
set newp=[define destination folder here]

Then we set the h (hidden) and s (system) attributes off (-h -s), move the folder, and switch the attributes back on again (+h +s).

attrib -h -s %oldp%
move %oldp% %newp%
attrib +h +s %newp%

You can also change all the file attributes within the folder, by using the /S modifier. And all (sub-)directories by using /D.

For instance:

attrib -h -s %oldp% /S /D


Will switch off the hidden and system attributes for (sub)folders and files in the %oldp% folder.

The same sort of thing happens with xcopy.
If you want to use xcopy on hidden/system files you must add the correct modifiers like so:

set oldp=[define original path here]
set newp=[define destination here]
xcopy %oldp% %newp% /H /S

Or only "visible" files will get copied.

I would love any additional input you may have about this.

Thoughts?

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory