如何处理带有特殊字符的文件路径? [英] How to handle file paths with special characters?

查看:288
本文介绍了如何处理带有特殊字符的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,该脚本需要能够以较高的凭据从同一目录中调用另一个PS脚本.由于脚本需要具有可移植性,因此脚本的文件路径不会提前知道,并且需要能够处理文件路径中的空格和特殊字符而不会失败.

I've got a PowerShell script that needs to be able to call another PS script from within the same directory with elevated credentials. Because the scripts need to be portable, the file path of the scripts isn't known ahead of time and needs to be able to handle spaces and special characters in the file path without failing.

由于提升PS会话将导致当前工作目录更改为"C:\ Windows \ System32",因此我需要能够将当前目录作为参数传递给脚本.现在,只要目录路径中没有特殊字符,我当前的脚本(下面)就可以很好地处理这种情况.

Since elevating a PS session will cause the current working directory to change to "C:\Windows\System32", I need to be able to pass the current directory to the script as a parameter. Now, my current script (below) is handling this fine as long as there aren't special characters in the directory path.

这是我当前的脚本(根据@ mklement0的建议更新):

Here's my current script (updated per @mklement0's suggestion):

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

Start-Process Powershell -Verb runas -ArgumentList "-ExecutionPolicy","Bypass","-File","`"$dir\elevated.ps1`"","`"$dir`""

我尝试转义一些特殊字符,如下所示:

I've tried escaping some of the special characters, like so:

$dir = $dir.Replace('&','`"&`"')

,但是对于Windows允许在文件夹/文件名中使用的所有不同字符,这不能始终如一地工作.也不是所有特殊字符都需要转义,但是我需要确保这适用于那些特殊字符.至少那些可能出现在文件路径中的文件.

but that's not working consistently for all the different characters that Windows allows in a folder/filename. Not all special characters need to be escaped either, but I need to make sure this works for the ones that do; at least those that are likely to show up in a filepath.

我主要关注的特殊字符(尽管有时也需要解决外来字符):!@#$%^_+-=.,{}`~&()[]

Special characters I'm mainly concerned with (though foreign characters might also need to be addressed at some point): !@#$%^_+-=.,{}`~&()[]

我正在测试的示例目录路径:

Example directory path I'm testing with:


C:\Users\Administrator\Desktop\Test Folder\badname-!@#$%^_+-=.,{}`~&()[]

尝试从上述位置运行我的脚本会返回以下错误:

Trying to run my script from the above location returns the following error:


The specified wildcard character pattern is not valid:
badname-!@#$%^_+-=.,{}\~&()[]
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RuntimeException

那么,让脚本动态处理目录路径中的特殊字符的最佳方法是什么?

So, what's the best way to have the script dynamically handle special characters in the directory path?

推荐答案

因此,只要方括号不为空,我就能找出一种可行的解决方案 [].

So, I was able to figure out a solution that works as long as the brackets aren't empty [].

这就是我所拥有的:


Start-Process Powershell -Verb runas -ArgumentList "-ExecutionPolicy","Bypass","-File","`"$(Get-Item -LiteralPath $PSScriptRoot\elevated.ps1)`""

使用它作为测试路径:

C:\Users\Administrator\Desktop\Test Folder\badname-!@#$%^_+-=.,{}`~[&]()

带有方括号[]的路径仍然无效,但不会返回错误.我假设这意味着在将方括号仍解释为通配符时,无法在预期的路径中找到文件.除了可能在运行并抛出错误之前检查路径外,不确定是否确实有什么方法可以解决此问题.

A path with empty brackets [] still doesn't work, but doesn't return an error. I'm assuming this means that it wasn't able to find a file in the expected path while interpreting the brackets as a wildcard still. Not sure if there's really anything that will fix that except maybe checking the path before running and throwing an error.

这篇关于如何处理带有特殊字符的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆