我知道在批处理脚本中获取文件夹所有者的唯一命令是:
dir %foldername% /q
这有两个问题:
它在列表所有 foldername的内容中显示,所以我只读 第一行(包含< DIR> 和“。”)或者您要运行
dir%parent_of_foldername%/ q | findstr“%foldername%”
如果所有者有一个长名称(例如:TrustedInstaller),则会对其进行修剪
这是我的c:\
13/03/2013 18.33 <DIR> NT SERVICE\TrustedInstaProgram Files
13/03/2013 18.33 <DIR> NT SERVICE\TrustedInstaProgram Files (x86)
那么如何在批处理脚本中获取文件夹的所有者名称?
答案 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)
dir /q
之外没有内置命令,但列出了一些可行的命令行工具。