powershell - Powershell 创建文件夹名称
问题描述
所以,我正在尝试创建一个文件夹,在其中合并 2 个字符串并获取日期;像这样:
第2345章
我的代码:
Get-ChildItem 'C:\Scripts\MetadataExport' | New-Item -Name (Get-ChildItem 'C:\Scripts\MetadataExport' -Name).Split('-')[2] + '_XIPs' + "_$((Get-Date).ToString('yyyy-MM-dd'))" -ItemType Directory -Force
但它给了我以下错误:
New-Item : A positional parameter cannot be found that accepts argument '_XIPs'.
At line:1 char:45
+ ... taExport' | New-Item -Name (Get-ChildItem 'C:\Scripts\MetadataExport' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
你能帮我弄清楚我做错了什么吗?
最好的
解决方案
如果要根据管道中的每个输入对象动态计算参数值,则需要使用延迟绑定脚本块( { ... }
),其中自动$_
变量引用手头的输入对象:
Get-ChildItem C:\Scripts\MetadataExport | New-Item -Type Directory -Force -Name {
$_.Name.Split('-')[2] + '_XIPs' + "_$((Get-Date).ToString('yyyy-MM-dd'))"
} -WhatIf
注意:上面命令中的-WhatIf
常用参数是预览操作。-WhatIf
一旦您确定该操作将执行您想要的操作,请删除。
推荐阅读
- php - 如何仅使用 FOR 循环在 PHP 中获取复选框值
- node.js - 如何将 req.user 从 express-validator 传递到下一个中间件
- django - 如何使用 Django 和 OAuth 2 从我的 G-Suite 帐户发送电子邮件?
- swift - 如何从 Walk Run Stride 蓝牙传感器的特征数据中识别值
- windows - 通过控制台更改 Windows 上的屏幕亮度
- c# - 当我的角色移动到不同的方向时,我的 Unity 2D 播放器控制器脚本不会让我的角色翻转
- c# - 查找与组策略对象对应的注册表项
- java - 如何运行 java jar 文件并将输出存储在 shell 脚本变量中
- react-native - 反应原生:模态不显示
- c++ - 带参数的 C++ Meyer 单例