-2

I need to parse a file name and move the file to a folder based on part of the file name.

Lets say I have several files in a folder C:\SYSLOG\Servers and Firewall\Logs. They are log files from several different servers or devices.. I need to move them to our NAS for archival. Sample file names:

 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

I need to move them to a corresponding folder on the NAS. My folder structure looks like this:

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

So basically I am wanting to parse the file name for everything to the left of the server (Sxx), and replace the . with spaces to create the destination folder name.

The files are not always .log files. All files have a .Sxx in the name and xx will always be numbers. If destination folder does not exist, then skip the file. There are no subfolders in C:\SYSLOG\Servers and Firewall\Logs.

I think I am trying to do something similar to powershell to move files based on part of file name

3
  • 1
    So, what have you tried so far? Commented Nov 22, 2014 at 13:07
  • I haven't tried anything yet. I have been looking at this page: [link]stackoverflow.com/questions/21117208/… - But I do not know how or where to search for the Sxx in the file name. I am new to powershell. Commented Nov 22, 2014 at 13:13
  • 3
    Then I suggest you try something yourself first and come back when you have a specific question about something you can't get to work. SO is not a place where other people write code for you. Commented Nov 22, 2014 at 13:16

2 Answers 2

1

Rely on the fact that everything in the file name up until the Sxx block is the destination if you just replace the . with a space:

# 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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the above and nothing happened. How is this searching for Sxx (S01, S02, etc.)
It's not, but since Sxx will always be the 3rd item counting from the end of the file name and split by ., everything up to and including the 4th item (still counting backwards) will be the Destination folder name
I thought that was what you were doing, but the Sxx is not always the 4th item. I am using the IndexOf instead. But thank you for your help.
0

Final Version.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.