Foxtable(狐表)用户栏目专家坐堂 → [求助]如何利用递归函数对文件夹下所有文件加密


  共有1426人关注过本帖树形打印复制链接

主题:[求助]如何利用递归函数对文件夹下所有文件加密

帅哥哟,离线,有人找我吗?
洮沙
  11楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:778 积分:6603 威望:0 精华:0 注册:2017/8/31 12:07:00
  发帖心情 Post By:2023/5/16 14:53:00 [只看该作者]

全部代码合适了,整理如下:

此主题相关图片如下:微信截图_20230516150434.png
按此在新窗口浏览图片

全局代码:
Public FLn As Integer = 0
Public FLold As String
Public FLnew As String

“文件计数”内部函数代码:
Dim dir As String = args(0)
For Each fl As String In FileSys.GetFiles(dir)
       FLn = FLn + 1
Next
For Each dir2 As String In FileSys.GetDirectories(dir) '如果有子目录递归
        Functions.Execute("文件计数", dir2)
Next

“文件加密”内部函数代码:
Dim dir As String = args(0)
For Each fl As String In FileSys.GetFiles(dir)  '如果有子目录递归
    EncryptFile(fl, fl.Replace(FLold, FLnew), "Qtkj")    '加密文件
    FLn = FLn + 1
Next
For Each dir2 As String In FileSys.GetDirectories(dir)  '如果有子目录
    If FileSys.DirectoryExists(dir2.Replace(FLold, FLnew)) Then '如果加密后目录不存在则创建之
    Else
        FileSys.CreateDirectory(dir2.Replace(FLold, FLnew)) 
    End If
    Functions.Execute("文件加密", dir2)
Next

按钮代码:
Dim dxg1 As WinForm.TextBox = e.Form.Controls("TextBox1")
Dim dxg2 As WinForm.TextBox = e.Form.Controls("TextBox2")
If dxg1.Text = Nothing Then
    MessageBox.Show("请先选择【源文件夹】!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    dxg1.Select() '焦点控件
    Return 
End If
If dxg2.Text = Nothing Then
    MessageBox.Show("请先选择【加密后文件夹】!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    dxg2.Select() '焦点控件
    Return 
End If
'全局代码赋值
FLn = 0
FLold = dxg1.Text
FLnew = dxg2.Text
’先对源文件夹目录下文件计数
For Each fl As String In FileSys.GetFiles(FLold)
    FLn = FLn + 1
Next
'执行文件计数递归函数
For Each dir2 As String In FileSys.GetDirectories(FLold) 
    Functions.Execute("文件计数", dir2)
Next
If FLn < 1 Then
    MessageBox.Show("【源文件夹】无文件,请重新选择!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    dxg1.Select() '焦点控件
    Return 
End If
Dim Result As DialogResult
Result = MessageBox.Show("需要加密【" & FLn & "】个文件,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.NO Then
    Return 
End If
Dim Floldn As Integer = FLn  '变换变量

'全局代码初始化
FLn = 0
’先对源文件夹目录下文件加密
For Each fl As String In FileSys.GetFiles(FLold)
    EncryptFile(fl, fl.Replace(FLold, FLnew), "Qtkj")
    FLn = FLn + 1
Next
'执行文件加密递归函数
For Each dir2 As String In FileSys.GetDirectories(FLold)
    If FileSys.DirectoryExists(dir2.Replace(FLold, FLnew)) Then '如果加密后目录不存在则创建之
    Else
        FileSys.CreateDirectory(dir2.Replace(FLold, FLnew)) 
    End If
    Functions.Execute("文件加密", dir2)
Next
MessageBox.Show("需要加密【" & Floldn & "】个文件,加密成功【" & FLn & "】个文件,加密失败【" & Floldn - FLn & "】个文件!", "提示",MessageBoxButtons.OK)
'恢复全局代码初始值
FLold = ""
FLnew = ""
FLn = 0
'打开加密后文件夹
Dim proc As New Process
proc.File = dxg2.Text
proc.Start

'代码结束
[此贴子已经被作者于2023/5/16 15:32:01编辑过]

 回到顶部
帅哥,在线噢!
有点蓝
  12楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:105960 积分:538875 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2023/5/16 14:58:00 [只看该作者]

调试

For Each dir2 As String In FileSys.GetDirectories(dir) '如果有子目录
msgbox(dir2 )
msgbox(dir2.Replace(FLold, FLnew))
    If FileSys.DirectoryExists(dir2.Replace(FLold, FLnew)) Then '如果目录不存在
msgbox("已存在")
    Else
        FileSys.CreateDirectory(dir2.Replace(FLold, FLnew))                        '没有执行           
msgbox("已创建")      
    End If
    Functions.Execute("文件加密", dir2) 
Next

 回到顶部
总数 12 上一页 1 2