batch:获取文件夹所有者的名称

时间:2013-04-04 19:17:56

标签: batch-file dir file-ownership

我知道在批处理脚本中获取文件夹所有者的唯一命令是:

dir %foldername% /q 

这有两个问题:

  1. 它在列表所有 foldername的内容中显示,所以我只读 第一行(包含< DIR> “。”)或者您要运行

    dir%parent_of_foldername%/ q | findstr“%foldername%”

  2. 如果所有者有一个长名称(例如:TrustedInstaller),则会对其进行修剪

  3. 这是我的c:\

    13/03/2013  18.33    <DIR>          NT SERVICE\TrustedInstaProgram Files
    
    13/03/2013  18.33    <DIR>          NT SERVICE\TrustedInstaProgram Files (x86)
    

    那么如何在批处理脚本中获取文件夹的所有者名称?

3 个答案:

答案 0 :(得分:2)

你可以试试这个:

@echo off &setlocal
set "foldername=c:\temp"
set "owner="

for /f "tokens=3*" %%i in ('dir /q %foldername%^|findstr "<DIR>"') do if not defined owner set "owner=%%j"
echo %owner%

..和没有计算机名称的所有者:

@echo off &setlocal
set "foldername=c:\temp"
set "owner="

for /f "tokens=1*delims=\" %%i in ('dir /q %foldername%^|findstr "<DIR>"') do if not defined owner set "owner=%%j"
echo %owner%

第三种方法(在我的机器上返回全名):

@echo off &setlocal
set "foldername=c:\temp"
set "owner="

for /f "tokens=2delims=\:" %%i in ('cacls "%foldername%" ^| find "%computername%"') do set "owner=%%i"
echo %owner%

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
SET "owner="
FOR /f "tokens=2delims=\" %%i IN ('dir /q "%~1"' ) DO IF NOT DEFINED owner CALL :setown %%i
ECHO owner of "%~1" is %owner%

GOTO :eof

:setown
SET owner=%*
:setownl
SET $2=%2
IF defined $2 shift&GOTO setownl
IF NOT "%1"=="." SET "owner="&GOTO :eof
SET owner=%owner:~0,-2%
GOTO :eof

是我的解决方案。对我有用......

答案 2 :(得分:0)

http://msinfluentials.com/blogs/jesper/archive/2007/07/02/show-the-owner-of-a-file.aspx的作者Jesper Johansson感叹除了dir /q之外没有内置命令,但列出了一些可行的命令行工具。