这是个打乱txt内容的代码,可是为什么不能用呢?我的目的是把txt里面经过排序的内容按行打乱,按行打乱哦,可不是全乱套了!大家有什么办法么?以下是这个vbs程序的代码,大家看哪里不对:sub daluan
Const ForReading = 1
Const ForWriting = 2
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject") '要处理的文件路径,写全路径
Set objFile = objFSO.OpenTextFile("C:\Users\Administrator\Desktop\yahoo.cn(0-10w)\新建文件夹\test.txt", ForReading)
i = -1
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
objDictionary.Add strLine, strLine
i = i + 1
Loop
objFile.Close
Dim arrWords()
Redim arrWords(i)
intWordsLeft = i
z = 0
Do While intWordsLeft >= 0
Randomize
rndWord = Int((intWordsLeft - 0 + 1) * Rnd + 0)
intWordsLeft = intWordsLeft - 1
colItems = objDictionary.Items
strText = colItems(rndWord)
arrWords(z) = strText
z = z + 1
objDictionary.Remove(strText)
Loop
'文件打乱顺序后,要保存的文件位置,写全路径
Set objFile = objFSO.OpenTextFile("C:\Users\Administrator\Desktop\yahoo.cn(0-10w)\新建文件夹\words.txt", ForWriting)
For Each strItem in arrWords
objFile.WriteLine strItem
Next
objFile.Close end sub
Const ForReading = 1
Const ForWriting = 2
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject") '要处理的文件路径,写全路径
Set objFile = objFSO.OpenTextFile("C:\Users\Administrator\Desktop\yahoo.cn(0-10w)\新建文件夹\test.txt", ForReading)
i = -1
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
objDictionary.Add strLine, strLine
i = i + 1
Loop
objFile.Close
Dim arrWords()
Redim arrWords(i)
intWordsLeft = i
z = 0
Do While intWordsLeft >= 0
Randomize
rndWord = Int((intWordsLeft - 0 + 1) * Rnd + 0)
intWordsLeft = intWordsLeft - 1
colItems = objDictionary.Items
strText = colItems(rndWord)
arrWords(z) = strText
z = z + 1
objDictionary.Remove(strText)
Loop
'文件打乱顺序后,要保存的文件位置,写全路径
Set objFile = objFSO.OpenTextFile("C:\Users\Administrator\Desktop\yahoo.cn(0-10w)\新建文件夹\words.txt", ForWriting)
For Each strItem in arrWords
objFile.WriteLine strItem
Next
objFile.Close end sub
Const ForReading = 1
Const ForWriting = 2
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject") '要处理的文件路径,写全路径
Set objFile = objFSO.OpenTextFile("C:\1.txt", ForReading)
i = -1
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
objDictionary.Add strLine, strLine
i = i + 1
Loop
objFile.Close
Dim arrWords()
Redim arrWords(i)
intWordsLeft = i
z = 0
Do While intWordsLeft >= 0
Randomize
rndWord = Int((intWordsLeft - 0 + 1) * Rnd + 0)
intWordsLeft = intWordsLeft - 1
colItems = objDictionary.Items
strText = colItems(rndWord)
arrWords(z) = strText
z = z + 1
objDictionary.Remove(strText)
Loop
'文件打乱顺序后,要保存的文件位置,写全路径
Set objFile = objFSO.OpenTextFile("C:\1$.txt", ForWriting)
For Each strItem in arrWords
objFile.WriteLine strItem
Next
objFile.Close end subdaluan '调用过程的语句不能少
另外test.txt和words.txt(内容为空)都要求已经存在。
sub daluan
Const ForReading = 1
Const ForWriting = 2
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject") '要处理的文件路径,写全路径
Set objFile = objFSO.OpenTextFile("C:\1.txt", ForReading)
i = -1
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
objDictionary.Add strLine, strLine
i = i + 1
Loop
objFile.Close
Dim arrWords()
Redim arrWords(i)
intWordsLeft = i
z = 0
Do While intWordsLeft >= 0
Randomize
rndWord = Int((intWordsLeft - 0 + 1) * Rnd + 0)
intWordsLeft = intWordsLeft - 1
colItems = objDictionary.Items
strText = colItems(rndWord)
arrWords(z) = strText
z = z + 1
objDictionary.Remove(strText)
Loop
'文件打乱顺序后,要保存的文件位置,写全路径
Set objFile = objFSO.OpenTextFile("C:\1$.txt", ForWriting)
For Each strItem in arrWords
objFile.WriteLine strItem
Next
objFile.Close end subdaluan '调用过程的语句不能少
路径我都写对了啊!.
.
.
.
'要处理的文件路径,写全路径
Set objFile = objFSO.OpenTextFile("C:\test.txt", ForReading)
.
.
.
'文件打乱顺序后,要保存的文件位置,写全路径
Set objFile = objFSO.OpenTextFile("C:\testok.txt", ForWriting)
.
.
.