powershell - 从文件夹中提取名称并使用它 powershell
问题描述
我必须在 powershell 中构建一个执行这些任务的脚本:
- 读取文件夹的名称(例如 GRA - First Man on Mars)
- 提取“-”字符之前的文件夹名称并存储到名为“shop”的变量中
- 将名为“2021”的示例文件夹及其子文件夹和文件复制到第一个文件夹“例如 GRA - 火星上的第一人”
- 进入文件夹(GRA - 火星上的第一人)并将excel文件从“sample.xlsx”重命名为“shop - sample.xlsx
- 移动到第二个文件夹 该脚本必须对所有第一级文件夹重复相同的操作。
我批量尝试了一些脚本,但我认为我必须转移到 powershell ......
第一步:我试图用这个读取文件夹的名称:
Get-ChildItem -Recurse -Directory | Select Name,
@{ n = 'Foldername'; e = { ($_.PSPath -split "-")[1] } }
但我必须提取名称的左侧部分,而不是右侧部分。
解决方案
像这样接近它:
- 获取文件夹列表
- 对于每个文件夹..
- 解析名称
- 复制内容
- 重命名文件
$sampleDirectory = Get-Item -Path C:\PathTo\yourSample\Directory
$Folders = Get-ChildItem -Directory -Path C:\PathTo\YourFolders
ForEach ($folder in $folders){
$folderName = $_.Name.Split("-")[0]
Copy-Item $sampleDirectory -Destination $folder
#Play with the syntax here to get your ideal name
$desiredName = $folderName + "whatever.xls"
Get-Item $folder\sample.xls | Rename-Item -NewName $desiredName -WhatIf
}
这应该让你去
推荐阅读
- c# - 构建使用 ZipDiff 和 SharpLibZib 的项目时出错
- r - 使用R语言从具有相同名称标签的XML中提取数据
- android - Android Activity history tracking and clearing partially
- azure - 将数据从表存储发送到事件中心
- vue.js - Vuex 提交不更新状态
- reporting-services - 如何在 SSRS 中使用表达式转移到报告
- python - 无法在 Python 中导入 C 扩展文件
- javascript - 覆盖 javascript 文件 (summernote.js) 中的一些函数
- c++ - C++ 中的 inData.open 和 inData.close
- selenium-webdriver - 如何在脚本(Appium)中自动将apk文件从本地路径上传到android模拟器?