【问题标题】:Create a hidden folder for logging information in Ruby在 Ruby 中为记录信息创建一个隐藏文件夹
【发布时间】:2016-06-03 17:27:35
【问题描述】:

我想创建一个隐藏文件夹来记录 Ruby 中的信息,有没有办法可以创建一个隐藏文件夹,并用密码锁定它,同时将信息记录到其中的文件中?

例子:

module LogEmail 
  def log(email)
    username = Etc.getlogin
    Dir.mkdir <hidden-dir> unless File.Exists?(<hidden-dir>)

    separator = "[#{Date.today} #{Time.now.strftime('%T')}] ----------"
    File.open("c:/users/#{username}/<hidden-folder>/<log>", 'a+') { |s| s.puts(separator, email) }
  end
end

这可能吗?

【问题讨论】:

  • 您似乎在 Windows 上。 Ruby 的 Dir 类可以创建文件夹,您可以设置文件夹的属性。您需要弄清楚触发操作系统使用密码部分锁定所需的权限,因为这不是 Ruby 会做的事情。
  • @theTinMan 我没有办法从脚本本身设置属性吗?
  • 是的,我就是这么说的。你也可以阅读文档,它也是这么说的。
  • @theTinMan 不正确,它可能不是ruby,但如果你调用一个shell命令是可能的..
  • 我说Ruby CAN 为文件夹设置属性,我们一直在*nix 上这样做。 Windows 会影响语言可以做什么,但它仍然应该可以通过 Ruby 完成,而不需要 shell 命令。除非您编写代码来执行此操作,否则 Ruby 不会管理查看文件夹并要求输入密码,然后通过停止该过程很容易击败。需要密码才能访问文件夹是操作系统级别的任务。

标签: ruby directory hidden-files


【解决方案1】:

我使用 shell 命令成功创建了一个隐藏文件夹。

module LogEmail 
  def log(email)
    username = Etc.getlogin
    dir = "c:/users/#{username}/log"
    if File.exists?(dir)
      separator = "[#{Date.today} #{Time.now.strftime('%T')}] ----------"
      File.open("#{dir}/email_log.LOG", 'a+') { |s| s.puts(separator, email) }
    else
      Dir.mkdir(dir)
      `attrib +h #{dir}` #<= Creates a hidden folder.
      separator = "[#{Date.today} #{Time.now.strftime('%T')}] ----------"
      File.open("#{dir}/email_log.LOG", 'a+') { |s| s.puts(separator, email) }
    end  
  end
end

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多