如何检查字符串是否是使用R的Windows的有效文件名?

时间:2021-09-12 21:22:49

I've been writing a program in R that outputs randomization schemes for a research project I'm working on with a few other people this summer, and I'm done with the majority of it, except for one feature. Part of what I've been doing is making it really user friendly, so that the program will prompt the user for certain pieces of information, and therefore know what needs to be randomized. I have it set up to check every piece of user input to make sure it's a valid input, and give an error message/prompt the user again if it's not. The only thing I can't quite figure out is how to get it to check whether or not the file name for the .csv output is valid. Does anyone know if there is a way to get R to check if a string makes a valid windows file name? Thanks!

我一直在为R编写一个程序,为今年夏天我和其他几个人一起研究的项目输出随机化方案,除了一个功能外,我已经完成了大部分工作。我一直在做的部分工作是让它真正用户友好,这样程序就会提示用户提供某些信息,因此知道需要随机化的内容。我已将其设置为检查每个用户输入以确保它是有效输入,并且如果不是则给出错误消息/再次提示用户。我唯一不能弄清楚的是如何让它来检查.csv输出的文件名是否有效。有没有人知道是否有办法让R检查一个字符串是否有效的Windows文件名?谢谢!

1 个解决方案

#1


1  

These characters aren't allowed: /\:*?"<>|. So warn the user if it contains any of those.

不允许使用这些字符:/ \:*?“<> |。如果用户包含其中任何一个,则警告用户。

Some other names are also disallowed: COM, AUX, NUL, COM1 to COM9, LPT1 to LPT9.

其他一些名称也是不允许的:COM,AUX,NUL,COM1到COM9,LPT1到LPT9。

You probably want to check that the filename is valid using a regular expression. See this other answer for a Java example that should take minimal tweaking to work in R. https://*.com/a/6804755/134830

您可能希望使用正则表达式检查文件名是否有效。请参阅另一个答案,了解一个Java示例,该示例应该在R中进行最少的调整.https://*.com/a/6804755/134830

You may also want to check the filename length (260 characters for maximum portability, though longer names are allowed on some systems).

您可能还需要检查文件名长度(260个字符以获得最大的可移植性,但某些系统允许使用更长的名称)。

Finally, in R, if you try to create a file in a directory that doesn't exist, it will still fail, so you need to split the name up into the filename and directory name (using basename and dirname) and try to create the directory first, if necessary.

最后,在R中,如果您尝试在不存在的目录中创建文件,它仍然会失败,因此您需要将名称拆分为文件名和目录名(使用basename和dirname)并尝试创建如果需要,首先是目录。

That said, David Heffernan gives good advice in his comment to let Windows do the wok in deciding whether or not it can create the file: you don't want to erroneously tell the user that a filename is invalid.

也就是说,David Heffernan在评论中给出了很好的建议,让Windows在决定是否可以创建文件时执行操作:您不想错误地告诉用户文件名无效。

You want something a little like this:

你想要一些像这样的东西:

nice_file_create <- function(filename)
{
  directory_name <- dirname(filename)
  if(!file.exists(directory_name))
  {
    ok <- dir.create(directory_name)
    if(!ok)
    {
      warning("The directory of that path could not be created.")
      return(invisible())
    }
  }
  tryCatch(
    file.create(filename),
    error = function(e)
    {
      warning("The file could not be created.")
    }
  )
}

But test it thoroughly first! There are all sorts of edge cases where things can fall over: try UNC network path names, "~", and paths with "." and ".." in them.

但首先要彻底测试它!事情可能会出现各种各样的边缘情况:尝试使用UNC网络路径名“〜”和带有“。”的路径。和“..”在其中。

#1


1  

These characters aren't allowed: /\:*?"<>|. So warn the user if it contains any of those.

不允许使用这些字符:/ \:*?“<> |。如果用户包含其中任何一个,则警告用户。

Some other names are also disallowed: COM, AUX, NUL, COM1 to COM9, LPT1 to LPT9.

其他一些名称也是不允许的:COM,AUX,NUL,COM1到COM9,LPT1到LPT9。

You probably want to check that the filename is valid using a regular expression. See this other answer for a Java example that should take minimal tweaking to work in R. https://*.com/a/6804755/134830

您可能希望使用正则表达式检查文件名是否有效。请参阅另一个答案,了解一个Java示例,该示例应该在R中进行最少的调整.https://*.com/a/6804755/134830

You may also want to check the filename length (260 characters for maximum portability, though longer names are allowed on some systems).

您可能还需要检查文件名长度(260个字符以获得最大的可移植性,但某些系统允许使用更长的名称)。

Finally, in R, if you try to create a file in a directory that doesn't exist, it will still fail, so you need to split the name up into the filename and directory name (using basename and dirname) and try to create the directory first, if necessary.

最后,在R中,如果您尝试在不存在的目录中创建文件,它仍然会失败,因此您需要将名称拆分为文件名和目录名(使用basename和dirname)并尝试创建如果需要,首先是目录。

That said, David Heffernan gives good advice in his comment to let Windows do the wok in deciding whether or not it can create the file: you don't want to erroneously tell the user that a filename is invalid.

也就是说,David Heffernan在评论中给出了很好的建议,让Windows在决定是否可以创建文件时执行操作:您不想错误地告诉用户文件名无效。

You want something a little like this:

你想要一些像这样的东西:

nice_file_create <- function(filename)
{
  directory_name <- dirname(filename)
  if(!file.exists(directory_name))
  {
    ok <- dir.create(directory_name)
    if(!ok)
    {
      warning("The directory of that path could not be created.")
      return(invisible())
    }
  }
  tryCatch(
    file.create(filename),
    error = function(e)
    {
      warning("The file could not be created.")
    }
  )
}

But test it thoroughly first! There are all sorts of edge cases where things can fall over: try UNC network path names, "~", and paths with "." and ".." in them.

但首先要彻底测试它!事情可能会出现各种各样的边缘情况:尝试使用UNC网络路径名“〜”和带有“。”的路径。和“..”在其中。