【问题标题】:Powershell Move file to new destination based on trimmed file namePowershell根据修剪的文件名将文件移动到新目标
【发布时间】:2016-02-07 16:06:38
【问题描述】:

我有一个文件被删除的文件夹,我希望从该文件夹中提取文件并根据部分文件名移动到一个新文件夹。如果新文件夹丢失,则创建它。

我试图将以下内容放在一起,但是它会引发有关已存在路径的错误并且不会移动文件。

文件名可以是除了文件的最后 16 个字符之外的任何不带模式的东西,我将删除这些并使用剩余的作为文件夹名称。 我真的是脚本新手,所以如果我犯了一个愚蠢的错误,我们将不胜感激。

编辑 我玩过不同的操作顺序,在新项目命令中添加了“-Force”,尝试使用“Else”而不是“If (!(”。 我现在正处于它自豪地显示新目录然后停止的地步。 我可以将移动项目部分添加到每个循环的新部分,以便在创建和测试目录后对其进行处理吗?如果是这样,您如何安排 { } 部分?

编辑 2 我终于让它工作了,更新了下面的脚本,movie-item 命令在遇到文件名中的特殊字符时出现问题,在我的情况下是方括号。 -literalpath 开关为我解决了这个问题。 谢谢各位的意见。

更新脚本 3.0

#Set source folder
$source = "D:\test\source\"
#Set destination folder (up one level of true destination)
$dest = "D:\test\dest\"
#Define filter Arguments
$filter = "*.txt"
<#
$sourcefile - finds all files that match the filter in the source folder
$trimpath - leaves $file as is, but gets just the file name.
$string - gets file name from $trimpath and converts to a string
$trimmedstring - Takes string from $trimfile and removes the last 16 char off the end of the string
Test for path, if it exists then move on, If not then create directory
Move file to new destination
#>
pushd $source
$sourcefile = Get-ChildItem $source -Filter $filter
foreach ($file in $sourcefile){
$trimpath = $file | split-path -leaf
$string = $trimpath.Substring(0)
$trimmedstring = $string.Substring(0,$string.Length-16)
If(!(Test-Path -path "$dest\$trimmedstring")){New-Item "$dest\$trimmedstring" -Type directory -Force}        
move-Item -literalpath "$file" "$dest\$trimmedstring"
}

【问题讨论】:

  • 如果您提供一些示例名称,我相信这可以用更少的行来完成。
  • @ricky89 文件名可以是任何东西,即使最后 16 个字符也会改变,但始终是 16 个字符,16 个字符包括 .txt
  • 例如“test012345678912.txt”删除16个字符得到“test”

标签: powershell file-manipulation


【解决方案1】:

您可能需要调整正在使用的路径,但以下应该可以工作。

 $sourcefiles = ((Get-ChildItem $source -Filter $filter).BaseName).TrimEnd(16)
 foreach ($file in $sourcefiles)
 {
     if(!(Test-Path "$dest\$file")){
         New-item -ItemType directory -path "$dest\$file"
     }
    Move-Item "$source\$file" "$dest\file"
 }

【讨论】:

  • 它只是从我的文本文件末尾修剪了.txt,确实创建了目录例如“test012345678912.txt”创建了dest\test012345678912,但仍然没有移动文件。
  • 我试过 $sourcefile = Get-ChildItem $source -Filter $filter |分割路径 -leaf foreach ($sourcefile 中的 $file) { $string = $file.Substring(0) $trimmedstring = $string.Substring(0,$string.Length-16) { if(!(Test-Path " $dest\$trimmedstring")){ New-item -ItemType directory -path "$dest\$trimmedstring" } Move-Item "$source\$file" "$dest\$trimmedstring" } } 真的很抱歉我不知道'不知道如何正确格式化此评论。
【解决方案2】:

我终于让它工作了,更新了下面的脚本,movie-item 命令在遇到文件名中的特殊字符时出现问题,在我的例子中是方括号。 -literalpath 开关为我解决了这个问题。感谢大家的意见。

#Set source folder
$source = "D:\test\source\"
#Set destination folder (up one level of true destination)
$dest = "D:\test\dest\"
#Define filter Arguments
$filter = "*.txt"
<#
$sourcefile - finds all files that match the filter in the source folder
$trimpath - leaves $file as is, but gets just the file name.
$string - gets file name from $trimpath and converts to a string
$trimmedstring - Takes string from $trimfile and removes the last 16 char off the end of the string
Test for path, if it exists then move on, If not then create directory
Move file to new destination
#>
pushd $source
$sourcefile = Get-ChildItem $source -Filter $filter
foreach ($file in $sourcefile){
$trimpath = $file | split-path -leaf
$string = $trimpath.Substring(0)
$trimmedstring = $string.Substring(0,$string.Length-16)
If(!(Test-Path -path "$dest\$trimmedstring")){New-Item "$dest\$trimmedstring" -Type directory -Force}        
move-Item -literalpath "$file" "$dest\$trimmedstring"
}

【讨论】:

    猜你喜欢
    • 2014-06-06
    • 2016-03-21
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多