vbs搜索文件名或者得到目录列表
程序员文章站
2022-04-29 09:13:44
'把网上的一个小程序改得方便了点,这个搜索次效率很好。 复制代码 代码如下:on error resume next dim&nb...
'把网上的一个小程序改得方便了点,这个搜索次效率很好。
on error resume next
dim keyword, dirtotal, timespend, filetotal, fso, outfile, txtresult, txtpath, spath
const my_computer = &h11&
const window_handle = 0
const options = 0
set objshell = createobject("shell.application")
set objfolder = objshell.namespace(my_computer)
set objfolderitem = objfolder.self
strpath = objfolderitem.path
set objfolder = objshell.browseforfolder(window_handle, "选择你要搜索的文件夹:", options, strpath)
if objfolder is nothing then
msgbox "您没有选择任何有效目录!"
wscript.quit
else
set objfolderitem = objfolder.self
spath = objfolderitem.path
txtpath=spath
set fso = wscript.createobject("scripting.filesystemobject")
filetotal = 0
dirtotal = 0
'spath = left(wscript.scriptfullname,len(wscript.scriptfullname)-len(wscript.scriptname))
'txtpath = trim(inputbox("你选的目录是"&spath,"文件搜索",spath))
keyword = lcase(inputbox("请输入搜索关键字点cancel的话会得到目录列表:","文件搜索","mp3"))
set outfile = fso.createtextfile(spath & "\searchresult.txt")
outfile.writeline "开始搜索..."
outfile.writeline "起启目录:" & txtpath
timespend = timer
myfind txtpath
timespend = round(timer - timespend,2)
txtresult = "搜索完成!" & vbcrlf & "共找到文件:" & filetotal & "个." & vbcrlf & "共搜索目录:" & dirtotal & "个." & vbcrlf & "用时:" & timespend & "秒."
outfile.write txtresult
msgbox txtresult &"结果保存在"&spath &"\searchresult.txt"
outfile.close
set outfile = nothing
set fso = nothing
sub myfind(byval thepath)
dim fso, myfolder, myfile, curfolder
set fso = wscript.createobject("scripting.filesystemobject")
set curfolders = fso.getfolder(thepath)
dirtotal = dirtotal + 1
if curfolders.files.count > 0 then
for each myfile in curfolders.files
if instr(1, lcase(myfile.name), keyword) > 0 then
outfile.writeline formatpath(thepath) & "\" & myfile.name
filetotal = filetotal + 1
end if
next
end if
if curfolders.subfolders.count > 0 then
for each myfolder in curfolders.subfolders
myfind formatpath(thepath) & "\" & myfolder.name
next
end if
end sub
function formatpath(byval thepath)
thepath = trim(thepath)
formatpath = thepath
if right(thepath, 1) = "\" then formatpath = mid(thepath, 1, len(thepath) - 1)
end function
end if
=======================================================================
附件:关于打开目录的方面:
private const csidl_desktop = &h0 ' <desktop>
private const csidl_internet = &h1 ' internet explorer (icon on desktop)
private const csidl_programs = &h2 ' start menu\programs
private const csidl_controls = &h3 ' my computer\control panel
private const csidl_printers = &h4 ' my computer\printers
private const csidl_personal = &h5 ' my documents
private const csidl_favorites = &h6 ' <user name>\favorites
private const csidl_startup = &h7 ' start menu\programs\startup
private const csidl_recent = &h8 ' <user name>\recent
private const csidl_sendto = &h9 ' <user name>\sendto
private const csidl_bitbucket = &ha ' <desktop>\recycle bin
private const csidl_startmenu = &hb ' <user name>\start menu
private const csidl_mydocuments = &hc ' logical "my documents" desktop icon
private const csidl_mymusic = &hd ' "my music" folder
private const csidl_myvideo = &he ' "my videos" folder
private const csidl_desktopdirectory = &h10 ' <user name>\desktop
private const csidl_drives = &h11 ' my computer
private const csidl_network = &h12 ' network neighborhood (my network places)
private const csidl_nethood = &h13 ' <user name>\nethood
private const csidl_fonts = &h14 ' windows\fonts
private const csidl_templates = &h15
private const csidl_common_startmenu = &h16 ' all users\start menu
private const csidl_common_programs = &h17 ' all users\start menu\programs
private const csidl_common_startup = &h18 ' all users\startup
private const csidl_common_desktopdirectory = &h19 ' all users\desktop
private const csidl_appdata = &h1a ' <user name>\application data
private const csidl_printhood = &h1b ' <user name>\printhood
private const csidl_local_appdata = &h1c ' <user name>\local settings\applicaiton data (non roaming)
private const csidl_altstartup = &h1d ' non localized startup
private const csidl_common_altstartup = &h1e ' non localized common startup
private const csidl_common_favorites = &h1f
private const csidl_internet_cache = &h20 'temporary internet files
private const csidl_cookies = &h21
private const csidl_history = &h22
private const csidl_common_appdata = &h23 ' all users\application data
private const csidl_windows = &h24 ' getwindowsdirectory()
private const csidl_system = &h25 ' getsystemdirectory()
private const csidl_program_files = &h26 ' c:\program files
private const csidl_mypictures = &h27 ' c:\program files\my pictures
private const csidl_profile = &h28 ' userprofile
private const csidl_systemx86 = &h29 ' x86 system directory on risc
private const csidl_program_filesx86 = &h2a ' x86 c:\program files on risc
private const csidl_program_files_common = &h2b ' c:\program files\common
private const csidl_program_files_commonx86 = &h2c ' x86 program files\common on risc
private const csidl_common_templates = &h2d ' all users\templates_
private const csidl_common_documents = &h2e ' all users\documents
private const csidl_common_admintools = &h2f ' all users\start menu\programs\administrative tools
private const csidl_admintools = &h30 ' <user name>\start menu\programs\administrative tools
private const csidl_connections = &h31 ' network and dial-up connections
private const csidl_common_music = &h35 ' all users\my music
private const csidl_common_pictures = &h36 ' all users\my pictures
private const csidl_common_video = &h37 ' all users\my video
private const csidl_resources = &h38 ' resource direcotry
private const csidl_resources_localized = &h39 ' localized resource direcotry
private const csidl_common_oem_links = &h3a ' links to all users oem specific apps
private const csidl_cdburn_area = &h3b ' userprofile\local settings\application data\microsoft\cd burning
private const csidl_computersnearme = &h3d ' computers near me (computered from workgroup membership)
private const csidl_flag_create = &h8000 ' combine with csidl_ value to force folder creation in shgetfolderpath()
private const csidl_flag_dont_verify = &h4000 ' combine with csidl_ value to return an unverified folder path
private const csidl_flag_no_alias = &h1000 ' combine with csidl_ value to insure non-alias versions of the pidl
private const csidl_flag_per_user_init = &h800 ' combine with csidl_ value to indicate per-user init (eg. upgrade)
private const csidl_flag_mask = &hff00 ' mask for all possible flag values
复制代码 代码如下:
on error resume next
dim keyword, dirtotal, timespend, filetotal, fso, outfile, txtresult, txtpath, spath
const my_computer = &h11&
const window_handle = 0
const options = 0
set objshell = createobject("shell.application")
set objfolder = objshell.namespace(my_computer)
set objfolderitem = objfolder.self
strpath = objfolderitem.path
set objfolder = objshell.browseforfolder(window_handle, "选择你要搜索的文件夹:", options, strpath)
if objfolder is nothing then
msgbox "您没有选择任何有效目录!"
wscript.quit
else
set objfolderitem = objfolder.self
spath = objfolderitem.path
txtpath=spath
set fso = wscript.createobject("scripting.filesystemobject")
filetotal = 0
dirtotal = 0
'spath = left(wscript.scriptfullname,len(wscript.scriptfullname)-len(wscript.scriptname))
'txtpath = trim(inputbox("你选的目录是"&spath,"文件搜索",spath))
keyword = lcase(inputbox("请输入搜索关键字点cancel的话会得到目录列表:","文件搜索","mp3"))
set outfile = fso.createtextfile(spath & "\searchresult.txt")
outfile.writeline "开始搜索..."
outfile.writeline "起启目录:" & txtpath
timespend = timer
myfind txtpath
timespend = round(timer - timespend,2)
txtresult = "搜索完成!" & vbcrlf & "共找到文件:" & filetotal & "个." & vbcrlf & "共搜索目录:" & dirtotal & "个." & vbcrlf & "用时:" & timespend & "秒."
outfile.write txtresult
msgbox txtresult &"结果保存在"&spath &"\searchresult.txt"
outfile.close
set outfile = nothing
set fso = nothing
sub myfind(byval thepath)
dim fso, myfolder, myfile, curfolder
set fso = wscript.createobject("scripting.filesystemobject")
set curfolders = fso.getfolder(thepath)
dirtotal = dirtotal + 1
if curfolders.files.count > 0 then
for each myfile in curfolders.files
if instr(1, lcase(myfile.name), keyword) > 0 then
outfile.writeline formatpath(thepath) & "\" & myfile.name
filetotal = filetotal + 1
end if
next
end if
if curfolders.subfolders.count > 0 then
for each myfolder in curfolders.subfolders
myfind formatpath(thepath) & "\" & myfolder.name
next
end if
end sub
function formatpath(byval thepath)
thepath = trim(thepath)
formatpath = thepath
if right(thepath, 1) = "\" then formatpath = mid(thepath, 1, len(thepath) - 1)
end function
end if
=======================================================================
附件:关于打开目录的方面:
private const csidl_desktop = &h0 ' <desktop>
private const csidl_internet = &h1 ' internet explorer (icon on desktop)
private const csidl_programs = &h2 ' start menu\programs
private const csidl_controls = &h3 ' my computer\control panel
private const csidl_printers = &h4 ' my computer\printers
private const csidl_personal = &h5 ' my documents
private const csidl_favorites = &h6 ' <user name>\favorites
private const csidl_startup = &h7 ' start menu\programs\startup
private const csidl_recent = &h8 ' <user name>\recent
private const csidl_sendto = &h9 ' <user name>\sendto
private const csidl_bitbucket = &ha ' <desktop>\recycle bin
private const csidl_startmenu = &hb ' <user name>\start menu
private const csidl_mydocuments = &hc ' logical "my documents" desktop icon
private const csidl_mymusic = &hd ' "my music" folder
private const csidl_myvideo = &he ' "my videos" folder
private const csidl_desktopdirectory = &h10 ' <user name>\desktop
private const csidl_drives = &h11 ' my computer
private const csidl_network = &h12 ' network neighborhood (my network places)
private const csidl_nethood = &h13 ' <user name>\nethood
private const csidl_fonts = &h14 ' windows\fonts
private const csidl_templates = &h15
private const csidl_common_startmenu = &h16 ' all users\start menu
private const csidl_common_programs = &h17 ' all users\start menu\programs
private const csidl_common_startup = &h18 ' all users\startup
private const csidl_common_desktopdirectory = &h19 ' all users\desktop
private const csidl_appdata = &h1a ' <user name>\application data
private const csidl_printhood = &h1b ' <user name>\printhood
private const csidl_local_appdata = &h1c ' <user name>\local settings\applicaiton data (non roaming)
private const csidl_altstartup = &h1d ' non localized startup
private const csidl_common_altstartup = &h1e ' non localized common startup
private const csidl_common_favorites = &h1f
private const csidl_internet_cache = &h20 'temporary internet files
private const csidl_cookies = &h21
private const csidl_history = &h22
private const csidl_common_appdata = &h23 ' all users\application data
private const csidl_windows = &h24 ' getwindowsdirectory()
private const csidl_system = &h25 ' getsystemdirectory()
private const csidl_program_files = &h26 ' c:\program files
private const csidl_mypictures = &h27 ' c:\program files\my pictures
private const csidl_profile = &h28 ' userprofile
private const csidl_systemx86 = &h29 ' x86 system directory on risc
private const csidl_program_filesx86 = &h2a ' x86 c:\program files on risc
private const csidl_program_files_common = &h2b ' c:\program files\common
private const csidl_program_files_commonx86 = &h2c ' x86 program files\common on risc
private const csidl_common_templates = &h2d ' all users\templates_
private const csidl_common_documents = &h2e ' all users\documents
private const csidl_common_admintools = &h2f ' all users\start menu\programs\administrative tools
private const csidl_admintools = &h30 ' <user name>\start menu\programs\administrative tools
private const csidl_connections = &h31 ' network and dial-up connections
private const csidl_common_music = &h35 ' all users\my music
private const csidl_common_pictures = &h36 ' all users\my pictures
private const csidl_common_video = &h37 ' all users\my video
private const csidl_resources = &h38 ' resource direcotry
private const csidl_resources_localized = &h39 ' localized resource direcotry
private const csidl_common_oem_links = &h3a ' links to all users oem specific apps
private const csidl_cdburn_area = &h3b ' userprofile\local settings\application data\microsoft\cd burning
private const csidl_computersnearme = &h3d ' computers near me (computered from workgroup membership)
private const csidl_flag_create = &h8000 ' combine with csidl_ value to force folder creation in shgetfolderpath()
private const csidl_flag_dont_verify = &h4000 ' combine with csidl_ value to return an unverified folder path
private const csidl_flag_no_alias = &h1000 ' combine with csidl_ value to insure non-alias versions of the pidl
private const csidl_flag_per_user_init = &h800 ' combine with csidl_ value to indicate per-user init (eg. upgrade)
private const csidl_flag_mask = &hff00 ' mask for all possible flag values
下一篇: VBS基础编程教程 (第4篇)