1. 程式人生 > >VBA-基本的檔案操作語句與檔案加密(加密dog)

VBA-基本的檔案操作語句與檔案加密(加密dog)

1.基本的檔案操作,如重新命名,移動,刪除,新建,檔案大小,具體程式碼如下所示

Option Explicit
 Sub 演示()
  Dim strpath As String, filename As String
  strpath = ThisWorkbook.path & "\"
  'filename = "text.txt"
  'MsgBox FileLen(strpath & filename) '檔案大小
  filename = "text.txt"
'  Name strpath & filename As strpath & "text_命名.txt" '重新命名或者移動路徑
'  Name strpath & "\1" As strpath & "\2" '重新命名資料夾
'filname = "text.txt"
'FileCopy strpath & filename, strpath & "tast_beifeng.text" '複製檔案
  'Kill strpath & "\test.txt" '刪除檔案
'   MkDir strpath & 1 '建立資料夾
'   RmDir strpath & 1 '刪除資料夾,(僅限空資料夾)
  
 
 End Sub

2.檔案加密(就是將檔案的每一個字元用dog的每一個字元取它的ASCII值,再顯示出來。)

Option Explicit
Sub 檔案加密()
 Dim filename As String
 filename = ThisWorkbook.path & "\text.txt"
 Open filename For Binary As #1
 Dim i As Integer, ch As Byte, dog As String, j As Integer
 dog = "asddfx"
 For i = 1 To LOF(1)
    Get #1, , ch
    '把ch 與dog的每一位進行一次異或
    For j = 1 To Len(dog)
    ch = ch Xor Asc(Mid(dog, j, 1))
    Next j
    Put #1, , ch
Next i
Close #1

End Sub