【问题标题】:Perl: Why do I get error "The file name, directory name, or volume label syntax is incorrect."Perl:为什么我会收到错误“文件名、目录名或卷标语法不正确。”
【发布时间】:2016-06-09 22:41:39
【问题描述】:

我正在尝试从 Windows 批处理文件运行以下 perl 代码,但出现错误 The file name, directory name, or volume label syntax is incorrect. 该脚本在 Eclipse 中运行良好。我的最终目标是使用 Windows 任务调度程序定期运行这个 perl 脚本,从而从批处理文件中运行它。

有没有其他方法可以实现我在 Windows 上定期运行 perl 脚本的目标?

我希望我的脚本能够跨平台运行,因为我也计划在 Mac 上运行它。

  use strict;
  use warnings;
  use Data::Dumper;
  use File::Find::Rule;

 my $basedir="G:\/My_Workspaces";
 my @exclude_dirs= qw(.foo);

#Fetching all the workspaces under base dir excluding the ones in @exclude_dirs
my @subdirs =
              File::Find::Rule
              ->mindepth(1)
              ->maxdepth(1)
              ->not_name(@exclude_dirs)
              ->directory
              ->in($basedir);

#Formating list of workspaces by removing the full path
s{^\Q$basedir\E/}{} for @subdirs;

【问题讨论】:

  • 除非此文件中的内容比您显示的更多,否则它是 Perl 源文件,而不是 Windows 批处理文件。这正是你所拥有的吗?另外请注意,只有 Windows 支持像 G:\My_Workspaces 这样的卷号
  • @Borodin :变量 $basedir 的值将在 mac 上发生变化。这是我的 perl 代码中唯一使用目录名称的部分。知道为什么我会收到错误吗?谢谢!
  • 不要转义/,这是一个正常的字符。 X:/foo/bar 应该可以在 Windows 上运行。
  • 该错误消息是来自 Perl 还是来自 Windows?此外,您无需从批处理文件调用脚本即可通过 Windows 计划任务调用它。

标签: windows perl batch-file


【解决方案1】:

如果这正是您文件的内容,那么您是在要求 Windows 的命令解释器处理 Perl 源代码,而它不能这样做

如果您确实需要创建一个嵌入了您的 Perl 代码的批处理文件,那么请查看 pl2bat 实用程序,它可以做到这一点

类似的命令

pl2bat myperl.pl

将创建一个文件myperl.bat,该文件将在 Windows 命令行上运行,并将您的 Perl 源代码嵌入其中。但该文件不可移植,因为它使用了 Mac 或 Linux 平台无法识别的 Windows 命令

【讨论】:

  • 它就像魅力一样。感谢您今天让我知道这个新事物pl2bat
【解决方案2】:

要么不知道如何执行你的 Perl 脚本,要么你的 Perl 脚本被 perl 以外的其他东西解释。

这可能是由于您的文件关联存在问题(或缺少关联)。确定确切原因需要更多信息。

无论如何,使用您的脚本作为参数执行perl 而不是直接执行脚本应该可以解决问题。

也就是说,执行

perl script.pl

而不是

script.pl

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多