简体   繁体   English

Powershell根据调整后的文件名将文件移动到新目标

[英]Powershell Move file to new destination based on trimmed file name

I have a folder where files get dropped, I wish to pull the files from that folder and move to a new folder based on part of the file name. 我有一个放置文件的文件夹,我希望从该文件夹中提取文件,然后根据部分文件名移至新文件夹。 If the new folder is missing then create it. 如果缺少新文件夹,则创建它。

I have attempted to put together the below, however it throws an error about the path already existing and doesn't move the file. 我试图将以下内容放在一起,但是它会引发有关已存在路径的错误,并且不会移动文件。

File names can be any thing with out pattern except the last 16 characters of the file, I am removing these and using the remaining as the folder name. 文件名可以是任何带有模式的东西,除了文件的最后16个字符外,我将其删除,并将其余的用作文件夹名。 I am really new to scripting so if i have made a silly mistake explanations are appreciated. 我真的是脚本新手,所以如果我犯了一个愚蠢的错误,将不胜感激。

Edit I have played with different orders of operations, added a "-Force" to the new item command, tried with using "Else" and not "If (!(". I am now at the point where it proudly displays the new directory and then stops. Could i add the move-item part to a new for each loop so it is processed after the dir is created and tested? If so how do you arrange the { } parts? 编辑我以不同的操作顺序进行操作,在新项目命令中添加了“ -Force”,并尝试使用“ Else”而不是“ If(!(”。我现在很自豪地显示新目录我可以为每个循环将move-item部分添加到新的循环中,以便在创建和测试目录后对其进行处理吗?如果是,如何安排{}部分?

Edit 2 I finally have it working, updated script below, the movie-item command was having issues when running into special characters in file names, in my case it was square brackets. 编辑2我终于可以正常工作了,下面是更新的脚本,当在文件名中遇到特殊字符时,movie-item命令出现问题,在我的情况下是方括号。 The -literalpath switch fixed that for me. -literalpath开关为我修复了该问题。 Thanks every one for your input. 谢谢各位的意见。

Updated script 3.0 更新了脚本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"
}

You may have to tweak the paths being used but the below should work. 您可能需要调整正在使用的路径,但以下方法应该可以工作。

 $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"
 }

I finally have it working, updated script below, the movie-item command was having issues when running into special characters in file names, in my case it was square brackets. 我终于有了它的工作,下面更新了脚本,当在文件名中遇到特殊字符时,movie-item命令出现了问题,在我的情况下是方括号。 The -literalpath switch fixed that for me. -literalpath开关为我修复了该问题。 Thanks every one for your input. 谢谢各位的意见。

#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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM