2014-11-22 49 views
-2

我需要解析一个文件名并根据部分文件名将文件移动到一个文件夹。基于PowerShell的文件名移动文件

假设我有一个文件夹C:\ SYSLOG \ Servers和Firewall \ Logs中有好几个文件。他们是来自几个不同服务器或设备的日志文件..我需要将它们移动到我们的NAS进行存档。示例文件名称:

Boston.North.Application.S01.120613.log 
Boston.South.Application.S12.122513.log 
Lexington.System.S02.073013.log 
Lexington.System.S22.073013.log 
Madison.IPS.S01.050414.txt 

我需要将它们移动到NAS上的相应文件夹。我的文件夹结构如下所示:

\\NAS\Logs\Boston North Application 
\\NAS\Logs\Boston South Application 
\\NAS\Logs\Lexington System 
\\NAS\Logs\Madison IPS 

所以基本上我想要的一切服务器(随心)的左解析文件名,并更换。用空格来创建目标文件夹名称。

这些文件并不总是.log文件。所有文件的名称中都有一个.Sxx,并且xx将始终是数字。如果目标文件夹不存在,则跳过该文件。 C:\ SYSLOG \ Servers和Firewall \ Logs中没有子文件夹。

我想我试图做同样的事情到powershell to move files based on part of file name

+1

那么,你到目前为止尝试过什么? – 2014-11-22 13:07:41

+0

我还没有尝试过任何东西。我一直在看这个网页:[链接] http://stackoverflow.com/questions/21117208/powershell-to-move-files-based-on-part-of-file-name - 但我不知道如何或在文件名中搜索Sxx的位置。我是Powerhell的新手。 – RMcLean 2014-11-22 13:13:31

+3

然后,我建议你先尝试一下自己,然后当你对某些你不能工作的具体问题回来时。 SO不是其他人为你写代码的地方。 – 2014-11-22 13:16:16

回答

1

依靠的事实,一切都在文件名,直到随心块是目标,如果你只是用空格代替.

# Retrieve the filenames 
$Directory = "C:\SYSLOG\Server and Firewall\Logs" 
$FileNames = (Get-Item $Directory).GetFiles() 

foreach($FileName in $FileNames) 
{ 
    # Split the filename on "." 
    $Pieces = $FileName-split"\." 

    # Counting backwords, grab the pieces up until the Sxx part 
    $Start = $Pieces.Count*-1 
    $Folder = $Pieces[$Start..-4]-join" " 

    # Build the destination path 
    $Destination = "\\NAS\Logs\{0}\" -f $Folder 

    # Test if the destination folder exists and move it 
    if(Test-Path $Destination -PathType Container) 
    { 
     Move-Item -Path $FileName -Destination $Destination 
    } 
} 
+0

我尝试了以上,没有发生任何事情。这是如何搜索Sxx(S01,S02等)的 – RMcLean 2014-11-23 00:22:19

+0

这不是,但是由于Sxx将始终是从文件名末尾算起的第3个项目,并以'.'分割,所有项目都包括第4项(仍然向后计数)将是目标文件夹名称 – 2014-11-23 17:01:36

+0

我认为这是你在做什么,但Sxx并不总是第四项。我正在使用IndexOf。但是谢谢你的帮助。 – RMcLean 2014-11-24 18:00:10

0

最终版本。

# Retrive list of files 
# $sDir = Source Directory 
$sDir = "C:\Temp\Logs\" 
# Generate a list of all files in source directory 
$Files = (Get-ChildItem $sDir) 
# $tDir = Root Target Directory 
$tDir = "C:\Temp\Logs2\" 

# Loop through our list of file names 
foreach($File in $Files) 
{ 
    # $wFile will be our working file name 
    $wFile = $File.Name 

    # Find .Sx in the file name, where x is a number 
    if ($wFile -match ".S0") 
     { 
     # tFile = Trimmed File Name 
     # We now remove everything to the right of the . in the .Sx of the file name including the . 
     $tFile = $wFile.substring(0,$wFile.IndexOf('.S0')) 
     } 
    # If we do not find .S0 in string, we search for .S1 
    elseif ($wFile -match ".S1") 
     { 
     $tFile = $wFile.substring(0,$wFile.IndexOf('.S1')) 
     } 
    # If we do not find .S0, or S1 in string, then we search for .S2 
    elseif ($wFile -match ".S2") 
     { 
     $tFile = $wFile.substring(0,$wFile.IndexOf('.S2')) 
     } 
    # Now we exit our If tests and do some work 

    # $nfold = The name of the sub-folder that the files will go into 
    # We will replace the . in the file name with spaces 
    $nFold = $tFile.replace("."," ") 

    # dFold = the destination folder in the format of \\drive\folder\SubFolder\  
    $dFold = "$tDir$nFold" 

    # Test if the destination folder exists 
    if(Test-Path $dFold -PathType Container) 
     { 
     # If the folder exists then we move the file 
     Move-Item -Path $sDir$File -Destination $dFold 

     # Now we just write put what went where   
     Write-Host $File "Was Moved to:" $dFold 
     Write-Host 
     } 
     # If the folder does not exist then we leave it alone! 
     else 
     { 
     # We write our selves a note that it was not moved 
     Write-Host $File "Was not moved!" 
     } 

# Now we have a drink! 
} 
相关问题