Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell基于文件名移动文件_Powershell - Fatal编程技术网

使用powershell基于文件名移动文件

使用powershell基于文件名移动文件,powershell,Powershell,我需要解析一个文件名,并根据部分文件名将文件移动到文件夹中 假设我在一个文件夹C:\SYSLOG\Servers和Firewall\Logs中有几个文件。它们是来自多个不同服务器或设备的日志文件。。我需要将它们移动到我们的NAS进行存档。示例文件名: Boston.North.Application.S01.120613.log Boston.South.Application.S12.122513.log Lexington.System.S02.073013.log Lexingto

我需要解析一个文件名,并根据部分文件名将文件移动到文件夹中

假设我在一个文件夹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
因此,基本上我想要解析服务器左侧所有内容的文件名(Sxx),并替换。使用空格创建目标文件夹名称

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


我想我正在尝试做一些类似于

的事情,这取决于这样一个事实,即在Sxx块之前,文件名中的所有内容都是目标,如果您只是用空格替换

# 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
    }
}
最后版本

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

那么,到目前为止你试过什么?我还没试过。我一直在看这个页面:[链接]-但我不知道如何或在哪里搜索文件名中的Sxx。我是powershell的新手。然后我建议你先自己尝试一下,当你有关于无法工作的特定问题时再回来。所以这不是一个其他人为你编写代码的地方。我尝试了上面的方法,但什么也没发生。搜索Sxx(S01、S02等)的过程是怎样的呢?不是这样,但由于Sxx始终是从文件名末尾算起的第三个项目,并按
分割,所以直到第四个项目(仍在倒数)为止的所有内容都将是目标文件夹名。我以为这就是您正在做的,但Sxx并不总是第四项。我使用的是IndexOf。但是谢谢你的帮助。