0

我需要保存运行 ICACLS 命令的结果,更改它们,然后恢复更改的权限。我使用 vbscript 来做到这一点。问题是从保存的读取行或整个数据会给我一个字符和一堆空格。但是,如果我尝试读取由 ICACLS 逐个字符上传并即时打印的结果数据的内容,它会显示正确的内容。

Const ForReading=1 'I/O modes
dim FolderToSetPermissions,ShellObj,FSO,EffectivePermissionsFile,CurrentLine,ParentFolderName,ParentFolderObj
FolderToSetPermissions="c:\test"
Set FSO = CreateObject("Scripting.FileSystemObject")
ParentFolderName=FSO.GetParentFolderName(wscript.ScriptFullName)
Set ParentFolderObj =fso.GetFolder(ParentFolderName)    
Set ShellObj = WScript.CreateObject("WScript.Shell")
ShellObj.run "icacls.exe """&FolderToSetPermissions&"""\* /save " & ParentFolderObj.path & "\EffectivePermissions.txt /t", 0, True
set EffectivePermissionsFile =FSO.OpenTextFile(ParentFolderObj.path & "\EffectivePermissions.txt",ForReading,False)
Do Until EffectivePermissionsFile.AtEndOfStream
'CurrentLine=EffectivePermissionsFile.Read(1) ' works fine if prints right after 
 'reading from file. Concatenating all the chars in file causes the same result as 
 'reading the file by lines or with .ReadAll function of FSO
 CurrentLine= EffectivePermissionsFile.ReadLine
 WScript.Echo CurrentLine
Loop
EffectivePermissionsFile.Close

我怀疑,问题出在 ICACLS 本身:它不会编写真正的文本文件。我找到了另一个遇到这种情况的人(https://windowssecrets.com/forums/showthread.php/150866-Win7-ICACLS-redirected-output-and-NOTEPAD-don-t-play-well-together)。

PS:CACLS 写入正确的文本文件,读取没有问题。但不幸的是,它不能在整个文件夹中进行递归。

4

1 回答 1

0

阅读了数百页,我终于找到了解决方案!icacls <file or folder> /save <filename> /T此函数将由UCS-2 代码页创建的文本转换为 UTF-8 之一:

Sub UTFConvert(filename)
  Set fso = CreateObject("Scripting.FileSystemObject")
  txt = fso.OpenTextFile(filename, 1, False, -1).ReadAll

  Set stream = CreateObject("ADODB.Stream")
  stream.Open
  stream.Type     = 2 'text
  stream.Position = 0
  stream.Charset  = "utf-8"
  stream.WriteText txt
  stream.SaveToFile filename, 2
  stream.Close
End Sub

生成的文本之后可以使用 VBScript 读取。

从这里得到答案:UCS-2 Little Endian to UTF-8 conversion leave file with many不需要的字符

于 2017-12-07T11:31:18.663 回答