急 vbscript 编程 关于写入文件时提示没有权限
代码如下filepath="d:\musictxt\melidy.txt"setfso=CreateObject("Scripting.FileSystemObject"...
代码如下
filepath = "d
:\musictxt\melidy.txt"
set fso = CreateObject("Scripting.FileSystemObject")
set out = fso.CreateTextFile(filepath,True,True)
set out = fso.OpenTextFile(filepath,2)
set sh1 = createObject("WScript.Shell")
musicfolder =sh1.ExpandEnvironmentStrings("%USERPROFILE%\Music")
if not fso.folderexists(musicfolder) then
musicfolder = fso.BuildPath(sh1.SpecialFolders("MyDocuments"),"My Music")
end if
wscript.echo"Searching",musicfolder,"for MP3 and WHA file ..."
nfiles = 0
listdir""
out.Close
wscript.echo "Listed",nfiles,"files"
'the main purpose is to retrieve the
sub listdir ( path)
dim fldr, file, folder, extn
if fso.folderexists(fso.BuildPath( musicfolder, path)) then
set fldr = fso.GetFolder(fso.BuildPath( musicfolder, path))
for each file in fldr.Files
extn = ucase(fso.GetExtensionName(file.name))
if extn = "MP3" or extn = "WMA" then
out.WriteLine path & vbTab & file.name & vbTab & file.Size
nfiles = nfiles + 1
end if
next
end if
for each folder in fldr.Subfolders
listdir fso.BuildPath(path, folder.Name)
next
end sub 展开
filepath = "d
:\musictxt\melidy.txt"
set fso = CreateObject("Scripting.FileSystemObject")
set out = fso.CreateTextFile(filepath,True,True)
set out = fso.OpenTextFile(filepath,2)
set sh1 = createObject("WScript.Shell")
musicfolder =sh1.ExpandEnvironmentStrings("%USERPROFILE%\Music")
if not fso.folderexists(musicfolder) then
musicfolder = fso.BuildPath(sh1.SpecialFolders("MyDocuments"),"My Music")
end if
wscript.echo"Searching",musicfolder,"for MP3 and WHA file ..."
nfiles = 0
listdir""
out.Close
wscript.echo "Listed",nfiles,"files"
'the main purpose is to retrieve the
sub listdir ( path)
dim fldr, file, folder, extn
if fso.folderexists(fso.BuildPath( musicfolder, path)) then
set fldr = fso.GetFolder(fso.BuildPath( musicfolder, path))
for each file in fldr.Files
extn = ucase(fso.GetExtensionName(file.name))
if extn = "MP3" or extn = "WMA" then
out.WriteLine path & vbTab & file.name & vbTab & file.Size
nfiles = nfiles + 1
end if
next
end if
for each folder in fldr.Subfolders
listdir fso.BuildPath(path, folder.Name)
next
end sub 展开
1个回答
展开全部
set out = fso.CreateTextFile(filepath,True,True) 这行删掉
set out = fso.OpenTextFile(filepath,2)
改为
set out = fso.OpenTextFile(filepath,2,true)
set out = fso.OpenTextFile(filepath,2)
改为
set out = fso.OpenTextFile(filepath,2,true)
追问
这是为什么?
追答
这是因为CreateTextFile建立文件的同时新文件也自动处于打开状态了,如果再用OpenTextFile去打开这个文件就会出错了。另一方面OpenTextFile如果把第三个参数设为True,那么它在打开文件的时候如果发现文件不存在会自动建立的。通常都是用OpenTextFile来建立文件的,比较灵活,很少用CreateTextFile的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询