首页 > 解决方案 > 从文件夹中提取名称并使用它 powershell

问题描述

我必须在 powershell 中构建一个执行这些任务的脚本:

我批量尝试了一些脚本,但我认为我必须转移到 powershell ......

第一步:我试图用这个读取文件夹的名称:

Get-ChildItem -Recurse -Directory | Select Name, @{ n = 'Foldername'; e = { ($_.PSPath -split "-")[1] } }

但我必须提取名称的左侧部分,而不是右侧部分。

标签: powershellextract

解决方案


像这样接近它:

  1. 获取文件夹列表
  2. 对于每个文件夹..
  3. 解析名称
  4. 复制内容
  5. 重命名文件
$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
}

这应该让你去


推荐阅读