gpt4 book ai didi

windows - 在 Windows 上使用 Perl,如何确保在 chdir 之后获得正确大小写的路径?

转载 作者:可可西里 更新时间:2023-11-01 13:51:32 24 4
gpt4 key购买 nike

考虑以下代码:

print cwd . "\n";
$str= "../source"; # note the lower case 's'
chdir($str);
print cwd . "\n";

如果我的当前目录是 c:\parentdir\Source(注意大写的“S”),则输出将是:

c:/parentdir/Sourcec:/parentdir/source

这会导致我的子程序出现问题,该子程序关心文件夹名称的正确大小写。 $str 被传递到我的子例程中,所以我无法提前知道它是否有正确的大小写。如何确定匹配 $str 的路径的大小写正确名称?

更多细节在这里:

  • 我意识到 ../source 是一个病态的例子,但它有助于说明问题。即使 $str 请求一个当前文件夹以外的文件夹。
  • 我尝试了很多选项,包括 rel2abs,一个 glob 搜索$str 等,但它们似乎都返回“source”而不是“来源”。
  • 我可以在 $str/.. 中搜索所有目录,将它们全部转换为绝对路径并将它们与 $str 的绝对路径版本进行比较,但这似乎是一个黑客。我希望有更优雅的东西。

最佳答案

#!/usr/bin/perl

use warnings; use strict;
use Cwd;
use File::Spec::Functions qw( canonpath );
use Win32;

print canonpath( cwd ), "\n";

chdir '../source';

print canonpath( cwd ), "\n";

print canonpath( Win32::GetLongPathName( cwd ) ), "\n";
C:\DOCUME~1\...\LOCALS~1\Temp\t\Source> tC:\DOCUME~1\...\LOCALS~1\Temp\t\SourceC:\DOCUME~1\...\LOCALS~1\Temp\t\sourceC:\Documents and Settings\...\Local Settings\Temp\t\Source

关于windows - 在 Windows 上使用 Perl,如何确保在 chdir 之后获得正确大小写的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7995113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com