Finally! A command-line tool for changing file ownership

Now and then I need to change ownership of a file that is on an NTFS drive. I can do that with the GUI through My Computer or Windows Explorer (select the file, right-click it, choose Properties from the resulting context menu, choose the Security tab, and click the Ownership button), but as is so often the case, I'd like to be able to do this task from the command line as well.

Windows NT resource kits have long included the Chown utility for changing ownership, but I've never been able to make it work. So, I was excited to find fileman.vbs tucked among the dozens of VBScript routines in the Microsoft Windows NT Server 4.0 Resource Kit Supplement 4.

Fileman.vbs lets you copy, delete, rename, or seize ownership of a file. Command-line tools already exist to copy, delete, and rename files, but I was glad to finally have a command-line ownership tool. The tool is a mite cumbersome, as you'll see. But because it's a script file, you've got the source code, so enterprising readers can address some of fileman.vbs' flaws.

To change a file's ownership, you invoke fileman.vbs:

cscript <scriptpath>\fileman.vbs /T <fullyqualifiedfilename>

The syntax looks straightforward, but kinks exist. First, notice the scriptpath part—you feed the name of the VBScript file to cscript, so you need to provide fileman.vbs' qualified name and location. Because my resource kit files are in C:\ntreskit, my command would begin with cscript c:\ntreskit\fileman.vbs. Second, the /T option is case sensitive. Third, you must specify the fully qualified name of the file that you want to seize ownership of. Typically, when you execute a command from within the same drive and directory as the file on which the command acts, you can omit the file's directory and drive letter. Not here! To take ownership of the names.txt file on D:\myfiles, I'd need to type

cscript c:\ntreskit\fileman.vbs /T d:\myfiles\names.txt

Finally, you can't feed fileman.vbs wildcards—you can't simply tell fileman.vbs to change ownership on *.* and walk away secure in the knowledge that fileman.vbs will take ownership of all the files in a directory. You can work around that limitation, however, with a fairly long For statement.

For those who've not used it, the For command lets you tell NT to repeat a specified command for a group of files. Simplified, the For command looks like

for %f in (*.*) do
<somecommand> %f

This command causes NT to find all files that meet the criterion in the parentheses—I used *.*, but you could use anything—and to replace %f with each file's name, one at a time. With each replacement, the For command executes the specified command, again replacing %f with the current filename. For example, to tell fileman.vbs to take ownership of every file in a directory named C:\soontobemine, I'd type

for %f in (c:\soontobemine\*.*) do cscript c:\ntreskitfileman.vbs /T %f

Because I specify a drive and directory name in the parentheses, each execution of the specified command includes that drive and directory name, so I don't need to type the drive and directory in the right-hand part of the command. If I wanted to execute the For command from inside C:\soontobemine, then I could have a shorter term in parentheses, but I'd need a longer right-hand portion:

for %f in (*.*) do cscript c:\ntreskit\fileman.vbs /T c:\soontobemine\%f

One final note: If you want to include a For command in a batch file, you must change %f to %%f, as in

for %%f in (c:\soontobemine\*.*) do cscript c:\ntreskit\fileman.vbs /T %%f

Now, readers, what clever soul will modify fileman.vbs script so that it will work on an entire drive, rather than just a directory? One of you, I hope!