【问题标题】:Using wildcards in file path在文件路径中使用通配符
【发布时间】:2019-05-15 23:11:19
【问题描述】:

我需要在服务器上搜索一些文件...但相关文件隐藏在用户 ID 下的文件夹中。所以我正在尝试使用通配符 %username% 来访问我要搜索的文件夹。

厌倦了使用 %username% 和 * 来通配上层文件夹以进入子文件夹。

$Path="\\server\profiles\%username%\Data\"
$filename='notes.ini'

Get-ChildItem -Path $Path -Recurse -Filter $filename -ErrorAction SilentlyContinue

我希望只在数据文件夹中搜索 notes.ini 文件,而不必搜索其间的所有文件夹。

【问题讨论】:

  • 您是要搜索每个用户的文件夹还是特定用户?

标签: powershell


【解决方案1】:

如果不知道用户名,可以在路径中使用通配符星号*

$Path     = "\\server\profiles\*\Data\"  #"# use wildcard to search all user folders 
$filename = 'notes.ini'

# this returns the full file and path names of the file you are looking for:
Get-ChildItem -Path $Path -Recurse -Filter $filename -File -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName

如果这不是您所追求的,请在您的问题中添加更多解释。

【讨论】:

    【解决方案2】:

    %username% 在 powershell 中是 $env:username

    %username% 不是通配符,它​​是 cmd 中的环境变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2022-10-15
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多