2014-11-22 79 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! 
} 
相關問題