Ruby:Dir.glob不返回任何文件 [英] Ruby: Dir.glob not returning any files
问题描述
我试图将文件从一个文件夹复制到另一个文件夹,然后记录所有复制到日志文件中的文件的名称。两个文件夹都在同一个目录中,我通过命令行使用该路径。在我的程序中,当前是通过
I am trying to copy files from one folder into another folder, and then record the names of all the files that were copied into a log file. Both folders are in the same directory and I take that path via the command line. In my program currently that happens through
argument3 = ARGV[2] + "\\"
成功成为 c:\user\alexander\desktop\。
然后我将台式机上的文件复制到已有的台式机文件夹中,
which successfully becomes 'c:\user\alexander\desktop\'. Then I copy the files on my desktop into a folder already on the desktop with
system "copy #{argument3}*.* #{argument3}TestFolder"
这也成功完成了我从cmd输出验证的并检查文件夹本身。最后,我试图将文件名保存到路径为 c:\user\alexander\desktop\log.txt的日志文件中。我尝试通过使用
This also successfully completes which I verified from the cmd output and from checking the folder itself. Finally I am trying to save the filenames to a log file with the path 'c:\user\alexander\desktop\log.txt'. I tried to do this by using
logFile = "c:\\user\\alexander\\desktop\\log.txt"
Dir.glob(argument3 + "*.*").each do |fileName|
File.open(logFile, 'a') {|file| file << "\n" + fileName}
end
c $ c> logFile 为空。我试图通过将 File.open
选项更改为 w而不是 a来解决此问题,但仍然没有执行任何操作。然后我以为我一定是错误地实现了它,所以我尝试只放
This does nothing and leaves my logFile
empty. I tried to fix this by changing the File.open
option to 'w' instead of 'a' but that still did nothing. Then I thought I must be implementing it incorrectly so I tried just putting
Dir.glob(argument3 + "*.*").each do |fileName|
puts fileName
end
作为健全性检查,但这也不会输出。我要完成的目标有可能吗?如果是,那我在做什么错了,如果不是,那是另一种方法吗?
as a sanity check, but this also outputs nothing. Is what I am trying to accomplish possible? If it is, what am I doing wrong, and if its not is there another way to do this?
编辑1:
更改 Dir.glob
的参数
Dir.glob("*")
使其将当前目录文件写入日志。我以为那意味着我的参数3变量中必须有一些看不见的错字,但是当我这样做的时候
is making it write the current directories files to the log. I thought that meant I must have some unseen typo in my argument3 variable but when I did
puts argument3 + "*"
其输出c:\user\alexander\desktop *我在通过之前尝试连接文件路径用作 Dir.glob
的参数
its outputting c:\user\alexander\desktop* I tried concatenating the file path before I passed it as an argument to Dir.glob
as
filePath = argument3 + "*"
Dir.glob(argument3 + "*.*").each do |fileName|
File.open(logFile, 'a') {|file| file << "\n" + fileName}
end
而且它仍然无法正常工作。将确切的文件路径传递到 Dir.glob
有什么技巧吗?我不能保证该程序始终位于运行时需要复制的目录中。
And its still not working as I expected. Is there some trick to passing in the exact file path to Dir.glob
? I can't guarantee that the program will always be in the directory it needs to copy from when its run.
推荐答案
Dir.glob
不带反斜杠。甚至在Windows上也将它们替换为正斜杠:
Dir.glob
doesn't take backslashes. Substitute them for forward slashes, even on Windows:
logFile = "c:\\user\\alexander\\desktop\\log.txt"
Dir.glob(argument3.gsub('\\','/') + "*.*").each do |fileName|
File.open(logFile, 'a') {|file| file << "\n" + fileName}
end
这篇关于Ruby:Dir.glob不返回任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!