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


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

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

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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望:0 精华:0 注册:2017/8/31 12:07:00
[求助]如何利用递归函数对文件夹下所有文件加密  发帖心情 Post By:2023/5/16 11:23:00 [显示全部帖子]

老师,如何实现对文件夹A(包含若干子文件夹及文件)中所有文件进行加密后创建同结构文件夹B,知道用递归函数,看了半天,无从下手。麻烦老师写一下代码,谢谢!

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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望:0 精华:0 注册:2017/8/31 12:07:00
  发帖心情 Post By:2023/5/16 11:40:00 [显示全部帖子]

感谢老师回复,再麻烦问一下如何将加密后文件创建同结构文件夹?

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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望:0 精华:0 注册:2017/8/31 12:07:00
  发帖心情 Post By:2023/5/16 11:58:00 [显示全部帖子]

老师,测试二楼代码,出现以下错误:
.NET Framework 版本:4.0.30319.42000
Foxtable 版本:2022.8.18.1
错误所在事件:自定义函数,abc
详细错误信息:
调用的目标发生了异常。
值不能为 null。
参数名: path


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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望:0 精华:0 注册:2017/8/31 12:07:00
  发帖心情 Post By:2023/5/16 14:33:00 [显示全部帖子]


老师,参照您的解答,编写以下代码,以下红色部分没有执行,再次麻烦您修改以下,感谢!

全局代码:
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)
    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
'全局代码赋值
FLold = dxg1.Text
FLnew = dxg2.Text

'执行文件加密递归函数
For Each dir2 As String In FileSys.GetDirectories(FLold) 
    Functions.Execute("文件加密", dir2)
Next
MessageBox.Show("加密成功【" & FLn & "】个文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)      ’FLn计数为4不正确,应该是5
'恢复全局代码初始值
FLold = ""
FLnew = ""
FLn = 0
'打开加密后文件夹
Dim proc As New Process
proc.File = dxg2.Text
proc.Start

源文件夹结构为:

图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20230516143059.png
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20230516143111.png
图片点击可在新窗口打开查看

[此贴子已经被作者于2023/5/16 14:42:12编辑过]

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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望:0 精华:0 注册:2017/8/31 12:07:00
  发帖心情 Post By:2023/5/16 14:45:00 [显示全部帖子]

EncryptFile自己疏忽了,另外问一下老师,递归函数中怎么动态增加文件夹?

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


加好友 发短信
等级:三尾狐 帖子:782 积分:6699 威望: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编辑过]

 回到顶部