希望使用可以接受通配符的Windows shell API调用来以编程方式更改属性。非常感谢/祝福任何/所有想法。
中的代码RUN / N ATTRIB + H“c:\ test.txt”&&隐藏导致dos窗口噪音本身 RUN / N ATTRIB -H“c:\ test.txt”&&取消隐藏
汤姆回忆起这个......但它不需要外卡: * CHAR ------------ ------- HEX ------------------ BIN ------ --- NUM
* READONLY R 0x00000001 00000000 00000001 1
* HIDDEN H 0x00000002 00000000 00000010 2
* SYSTEM S 0x00000004 00000000 00000100 4
* DIRECTORY D 0x00000010 00000000 00010000 16
* ARCHIVE A 0x00000020 00000000 00100000 32
* NORMAL N 0x00000080 00000000 10000000 128
* TEMPORARY T 0x00000100 00000001 00000000 256
* COMPRESS C 0x00000800 00001000 00000000 2048
* NOINDEX I 0x00002000 00100000 00000000 8192
* CHIPHER P 0x00004000 01000000 00000000 16384
* ERROR 0xFFFFFFFF REPL(“1”,32)4294967295
* ------------------------------------------------- ---------------------
LPARAMETER vFilename为String,vNewAttribute为String LOCAL liFlag as Integer,llResult,liAttributes,lnNewAttribute,cDummy,nBitPos,cBitMap
DECLARE INTEGER SetFileAttributes在Win32API STRING,INTEGER中 在Win32API STRING中DECLARE INTEGER GetFileAttributes
llResult = .F。
IF!EMPTY(vFilename)
IF VARTYPE(vNewAttribute) = [C]
lnNewAttribute = 0
* 1234567890123456
cBitMap = [RHS DA NT C IP ]
FOR i = 1 TO LEN(vNewAttribute)
cDummy = SUBSTR(vNewAttribute,i,1)
nBitPos = AT(cDummy,cBitMap)
IF nBitPos > 0
lnNewAttribute = BITSET(lnNewAttribute, nBitPos -1 )
ENDIF
ENDFOR
ELSE
lnNewAttribute = vNewAttribute
ENDIF
liAttributes = GetFileAttributes(vFilename)
IF (liAttributes # -1)
lnNewAttribute = BITXOR(liAttributes, lnNewAttribute)
llResult = (SetFileAttributes(vFilename, lnNewAttribute) = 1 )
ENDIF
ENDIF
RETURN llResult
答案 0 :(得分:1)
当然RUN ......不应该是你完成一个你根本不需要对DOS进行shell的程序的选择之一。
你的问题有两个:
对于这个,你可以使用一堆替代品,如FileSystemObject,Adir()或Filer.dll,它们附带VFP或更多。在这里,我将使用Filer.dll(它也是HOME()+' tools \ filer \ filer.scx')中使用的DLL进行采样。这是使用filer的一个增强的通配符匹配:
*GetTree.prg
Lparameters tcStartDir,tcSkeleton,tcCursorName,;
tlSubfolders,;
tlWholeWords,tlIgnoreCase,tlSearchAnd,tcSearch1,tcSearch2,tcSearch3
Create Cursor (m.tcCursorName) ;
(filepath m, filename m, ;
FileSize i, fattr c(8), createtime T, lastacc T, lastwrite T)
Local oFiler, lnFound
oFiler = Createobject('filer.fileutil')
With m.oFiler
.SearchPath = m.tcStartDir
.FileExpression = m.tcSkeleton && Search for skeleton
.Subfolder = Iif(m.tlSubfolders,1,0) && Check subfolders
.IgnoreCase = Iif(m.tlIgnoreCase,1,0)
.WholeWords = Iif(m.tlWholeWords,1,0)
.SearchAnd = Iif(m.tlSearchAnd,1,0)
.SearchText1 = Iif(Empty(m.tcSearch1),"",m.tcSearch1)
.SearchText2 = Iif(Empty(m.tcSearch2),"",m.tcSearch2)
.SearchText3 = Iif(Empty(m.tcSearch3),"",m.tcSearch3)
lnFound = .Find(0)
For ix=1 To m.lnFound
With .Files(m.ix)
If !(Bittest(.Attr,4) And .Name = '.')
Insert Into (m.tcCursorName) ;
(filepath, filename, FileSize, fattr, createtime, lastacc, lastwrite) ;
values ;
(.Path, .Name, .Size, Attr2Char(.Attr), ;
Num2Time(.Datetime), Num2Time(.LastAccessTime), Num2Time(.LastWriteTime))
Endif
Endwith
Endfor
Endwith
Return m.lnFound
Function Num2Time
Lparameters tnFloat
Return Dtot({^1899/12/30}+Int(m.tnFloat))+86400*(m.tnFloat-Int(m.tnFloat))
Function Attr2Char
Lparameters tnAttr
Return ;
IIF(Bittest(m.tnAttr,0),'RO','RW')+;
IIF(Bittest(m.tnAttr,1),'H','_')+;
IIF(Bittest(m.tnAttr,2),'S','_')+;
IIF(Bittest(m.tnAttr,4),'D','_')+;
IIF(Bittest(m.tnAttr,5),'A','_')+;
IIF(Bittest(m.tnAttr,6),'E','_')+;
IIF(Bittest(m.tnAttr,7),'N','_')
如果你不喜欢那些很少使用的花哨属性,这就是我为自己写的功能:
*SetFAttributes.prg
lparameters tcFileName, tlReadOnly, tlHidden, tlSystem
#define FILE_ATTRIBUTE_READONLY 0x00000001
#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
local lnNewAttr
lnNewAttr = iif(m.tlReadonly,FILE_ATTRIBUTE_READONLY,0)+;
iif(m.tlHidden,FILE_ATTRIBUTE_HIDDEN,0)+;
iif(m.tlSystem,FILE_ATTRIBUTE_SYSTEM,0)
declare integer SetFileAttributes in Win32API ;
string @ lpFileName, integer dwFileAttributes
declare integer GetFileAttributes in Win32API ;
string @ lpFileName
return ( SetFileAttributes(@tcFilename, ;
bitor(bitand(GetFileAttributes(@tcFilename),0xFFFFFFF8),m.lnNewAttr)) = 1)
手头有上面的prg文件,假设你想要将c:\ MyFolder及其子文件夹下的所有.txt文件设置为readonly,(不是隐藏,不是系统)你会这样做:
Local lcFileName
GetTree('c:\MyFolder','*.txt', 'myCursor', .T.)
Select myCursor
scan for Atc('D',fAttr) = 0
lcFileName = Addbs(Trim(FilePath))+Trim(FileName)
SetFAttributes(m.lcFileName, .T., .F., .F.)
endscan