vb.net - Visual Basic path asterisk -
i'm trying figure out how replace char (random) char in path example, don't need check multiple times (exemple : *:/program files/abc/*.cfg). in exemple, delete .cfg files in folder on every disk drives.
what i've tried :
if my.computer.filesystem.fileexists(dir & "\abc\?\?.cfg") my.computer.filesystem.deletefile(dir & "\abc\?\?.cfg") end if
i don't think it's possible use wildcard disk drive (at least couldn't find way it), can quite drives on computer , run loop, using driveinfo.getdrives method:
for each drive driveinfo in driveinfo.getdrives() dim filepath string = string.format("{0}test1.txt", drive.name) if file.exists(filepath) console.writeline(convert.tostring("exists: ") & filepath) end if next update
in order use wildcards, must use directory.getdirectories , directory.getfiles instead of file.exists:
dim wildcardpath string = "abc\*" dim wildcardfilename string = "*.cfg" each drive driveinfo in driveinfo.getdrives() try dim dirs string() = directory.getdirectories(drive.name, wildcardpath) each dir string in dirs dim files string() = directory.getfiles(dir, wildcardfilename) each filename string in files if file.exists(filename) console.writeline(filename) end if next next catch ex exception console.writeline(ex.message) end try next note: exceptions when attempting read dvd drive if no disk inside, , when attempting find directories doesn't exists (i.e if have z drive , no z:\abc directory exception "could not find part of path z:\abc").
Comments
Post a Comment