【问题标题】:How to Handle Blanks in %UserProfile% Batch File?如何处理 %UserProfile% 批处理文件中的空白?
【发布时间】:2017-12-11 02:59:05
【问题描述】:

我创建了一个脚本,它从 DropBox 复制我们的工作 PowerPoint 演示文稿模板并将其保存为默认模板。

有些用户帐户有空格,有些则没有,而且我无法获取用于触发此脚本运行的批处理文件。

我已经尝试了我能想到的所有类型的格式 {}""'' 等


"'%UserProfile%'\'Dropbox'\'Group Team Folder'\'Templates'\'PowerPoint Template.potx'"

预期结果是批处理文件使用用户名“John Smith”中的空白作为 PowerPoint 模板路径的一部分,而不是将其截断。

它吐出一个错误,说明路径“C:\Users\John”无法识别。

任何帮助将不胜感激

编辑

@ECHO OFF

If exist "%UserProfile%\Dropbox\Group Team Folder\Templates\PowerPoint Template.potx" (
PowerShell.exe -ExecutionPolicy Bypass -Command "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"
)

If exist  "%UserProfile%\Dropbox (Group)\Group Team Folder\Templates\PowerPoint Template.potx" (
PowerShell.exe -ExecutionPolicy Bypass -Command "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"
)

PAUSE

如果没有单个刻度,它不适用于具有没有空格的用户配置文件的系统。 请注意重复代码的原因是因为在某些系统上 Dropbox 有 2 个不同的目录,因此它使用 PowerPoint 模板来确定路径是否适合该系统。

编辑 2

C:\Users\John\Dropbox\Group : The term 'C:\Users\John\Dropbox\Group' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + C:\Users\John\Dropbox\Group Team Folder\Templates\ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 那是我的命令提示符错误。

我不知道为什么这不起作用。

【问题讨论】:

  • 您需要向我们展示您在其中使用的命令。并删除单引号。
  • 去掉所有的单引号,留下周围的双引号。
  • 删除单个刻度会阻止脚本在没有空格以及包含空格的系统上运行
  • 当涉及到批处理文件中包含空格的任何内容时,我们会为您提供一个简单明了的事实。用双引号括起来。在您向我们展示所有脚本之前,我们无法帮助您。
  • 我更新了整个脚本,我希望这是删除单个刻度的问题-_-

标签: powershell batch-file path


【解决方案1】:

这些是运行 powershell 脚本的常用方法;

使用-File:

PowerShell.exe -ExecutionPolicy Bypass -File "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"

使用-Command:

PowerShell.exe -ExecutionPolicy Bypass -Command "& '%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1'"

【讨论】:

  • 这解决了它,通过将其格式化为 - 文件而不是 - 命令。非常感谢!
【解决方案2】:

我遇到了一些用户尝试将文件从\复制到他们的个人资料的问题。

我最终在 Windows 8.1 中使用 $env 变量。

$env:systemDrive\users\$env:USERNAME

但我看到您想在那里运行 PowerShell 脚本,因此 Compo 会提示您如何运行这些脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多