Avatar of SQLM_M
SQLM_M
Flag for India asked on

File Split and move processed to archive using powershell

Hi,

I tried to make code dynamic, but couldn't able to move the files. Please help me the solve this..

$pattern = @("Mobile Originated Call", "Mobile Terminated Call", "Network Info")

$SourcePath = "D:\tap3"
$FileType = "*.cdr"
$DestPath = "D:\tap3\Archive\"

Get-ChildItem "$SourcePath\$FileType" |
  % { # $_ is file
    $name = $_.name
    get-content $_ |
    % { # $_ is single line
      foreach ($p in $pattern)
      { if ($_.StartsWith($p))
        { Out-File -Append -FilePath "$SourcePath\$p\$name" -InputObject $_  }
      }
    }
Move-Item "$SourcePath\$p\$name" "$DestPath\$name" -Force
     
  }
Powershell

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
the_endjinn

You are calling the variable $p outside of the foreach loop that defines it, therefore $p will be empty.

Move the move-item line into the foreach loop at the end.
Qlemo

Exactly. Move the move-item line two lines higher, so it comes after the Out-File line.
And, please, put code into a code snippet tag (select the code, and press the CODE button). That improves reading.
$pattern = @("Mobile Originated Call", "Mobile Terminated Call", "Network Info")

$SourcePath = "D:\tap3"
$FileType = "*.cdr"
$DestPath = "D:\tap3\Archive\"

Get-ChildItem "$SourcePath\$FileType" |
  % { # $_ is file
    $name = $_.name
    get-content $_ |
    % { # $_ is single line
      foreach ($p in $pattern)
      { if ($_.StartsWith($p))
        { Out-File -Append -FilePath "$SourcePath\$p\$name" -InputObject $_  }
        Move-Item "$SourcePath\$p\$name" "$DestPath\$name" -Force
      }
    }
  }

Open in new window

SQLM_M

ASKER
Hi,

When i execute the above code i get the following error,

Cannot find the path because it does not exist..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
the_endjinn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SQLM_M

ASKER
Hi,

Along with the move files to archive, I also want the count of each pattern in a file...i tried the following code, but i couldn't get the output...please give a solution..


$pattern = @("Mobile Terminated Call")
$SourcePath = "D:\tap3"
$FileType = "*.cdr"
$file="D:\tap3\Log\file.txt"
$a = (Get-Content $file).Count

Get-ChildItem "$SourcePath\$FileType" |
  % { # $_ is file
    $name = $_.name
    get-content $_ |
    % { # $_ is single line
      foreach ($p in $pattern)
      { if ($_.StartsWith($p))
       { (Get-Content $_).Count}
	   write-host $a    
      }
	
    }
  }

Open in new window

SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.