QQ网名大全

VB,如何随机开发一个文件?

dim iFolderCount as integer
dim sFolderPath as string
dim iFileName as string
dim lTargetFileCount as long
dim lFileCount as long
dim sTargetFileName as string

Randomize'初始化随机数

'-----------

iFolderCount=Int((3 * Rnd) + 1)'从3个文件夹中随机选择一个序号
'如果你的文件夹命名没有什么规则的话,用这种方法
select case iFolderCount
case 1
sFolderPath=[第一个文件夹名称]
case 2
sFolderPath=[第二个文件夹名称]
case 3
sFolderPath=[第三个文件夹名称]
end select

'有规则的话更省事,用下边这一句就好,比如说文件夹命名规则为:文件夹1,文件夹2,文件夹3...文件夹N
'sFolderPath="文件夹" & cstr(iFolderCount)

sFolderPath=[三个目标文件夹存放目录] & "\" & sFolderPath'写入目标文件夹全路径

'-----------
'下边获取文件
'文件没有命名规则的话,用下边的方法,有的话,参照上边选文件夹的方法
lTargetFileCount=Int(([目标文件夹下文件总数] * Rnd) + 1)'从文件中随机选择一个序号,文件总数的获取自己尝试下吧,^^

sTargetFileName =Dir(sFolderPath)'使用Dir

for lFileCount = 2 to lTargetFileCount
sTargetFileName =Dir
next
'目标文件名称获取完成

'-----------

[打开] replace(sFolderPath & "\" & sTargetFileName,"\\","\")'用你自己的方法调用选定的文件,replace用来规范字符串,防止路径中连续出现两个"\\"
佚名
2024-05-29 15:45:22
最佳回答
类似问题(10)