powershell - Powershell - 如何将文件名前缀到包含该文件类型的任何路径的文件夹中
问题描述
Powershell 脚本需要从目录上带有时间和日期戳的文件夹中的文件类型中提取文件基名,并使用该基名附加这些文件夹。
下面的脚本可以替换下面的路径名,但我需要为文件夹添加前缀。是否可以通过这种方式加入路径?
cd C:\Directory
Get-ChildItem *.lsa -File -Recurse | ForEach-Object {
Rename-Item (Split-Path $_ -Parent) ($_.BaseName) -WhatIf
}
结果:
What if: Performing the operation "Rename Directory" on target "Item: C:\Directory\originalpath
Destination: C:\Directory\basename".
我想要的是"C:\Directory\basename_originalpath"
或者更好的是"C:\Directory\basename\originalpath"
我认为Join-path
是解决方案,但我是第一次编写脚本,无法链接这两个命令。我还必须指定起始目录,因为第一次尝试在c:\
没有它的情况下运行我的整个驱动器。
解决方案
它不漂亮,但我认为这会起作用:
Get-ChildItem *.lsa -File -Recurse | ForEach-Object {
if(-Not (Test-Path -Path "$outputDirectory\$($_.BaseName)")){
New-Item -WhatIf -ItemType Directory -Path "$outputDirectory\$($_.BaseName)"
}
Move-Item (Split-Path $_ -Parent) "$outputDirectory\$($_.BaseName)\$($_.Directory.BaseName)" -WhatIf
}
更新了代码。
推荐阅读
- javascript - Javascript 动态元素创建导致 HTML 中的阴影内容
- mongodb - Mongodb集合设计
- math - 我想知道我是否可以使用 Prolog 进行解析推理
- jenkins - 在哪里可以在我的 ubuntu 机器中找到新的重置密码路径?
- variables - 如何在 ABB codesys 中存储负值
- javascript - Next.js 和 Express.js,只为网页运行认证中间件
- python - 预期的二维数组,得到了标量数组:
- python - asyncio 错误 = rs.append(await session.get(urls)) RuntimeError: Task got bad yield:
- java - Springboot中如何配置@Transactional对Redis的支持?
- javascript - CSS 和 Material UI - 如何让表格内容只向下扩展 || 如何设置容器顶部的固定位置