舉例:
指定路徑檔案夾下n個CSV檔案(資料各不相同)
特征列為D列
B.CSV,C.CSV的D列存在空值
A.CSV
A B C D E
1 2 3 4 5
a b c d e
6 7 8 9 10
B.CSV
A B C D E
1 2 3 4 5
a b c e
6 7 8 9 10
C.CSV
A B C D E
1 2 3 4 5
a b c d e
6 7 8 10
f g h i j
...........
n.CSV
A B C D E
1 2 3 4 5
a b c d e
6 7 8 9 10
如何洗掉B.CSV和C.CSV這類檔案,即需要洗掉檔案夾下特定列存在空值的檔案
感謝各位老師~
uj5u.com熱心網友回復:
除去行首和行尾的空格,只要一行中包含三個空格就是需要洗掉的檔案:下面是vbs示例代碼:
Set fso = createobject("scripting.filesystemobject")
curdir = fso.getparentfoldername(wscript.scriptfullname)
Set reg = createobject("vbscript.regexp")
reg.Global = True
reg.multiline = True
reg.ignorecase = True
reg.pattern = "[ ]{3,}"
For Each objfile In fso.getfolder(curdir).files
If LCase(fso.getextensionname(objfile.name)) = "txt" Then
Set stream = fso.opentextfile(objfile.path,1,False)
blnfound = False
Do Until stream.atendofstream
strline = Trim(stream.readline())
If reg.test(strline) Then
blnfound = True
Exit Do
End If
Loop
stream.close
If blnfound Then
msgbox objfile.path
'objfile.delete True
End If
End If
Next
msgbox "done!"
運行示例:


下載地址:
鏈接:https://pan.baidu.com/s/1zV_f6cmBLsNz4VjJnatH0A
提取碼:3lbk
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/28663.html
標籤:VBA
下一篇:這段代碼哪里錯了
