views:

259

answers:

1

I am executing a query in SQL Server and returning a single column result set. I need to loop through the result set and find file names matching the record from the result set. Part of the file name is a sequence number. I need to sort the files in ascending order, select the first file, and then copy only that file to a subdirectory. The records in the result set look like this:

MEMBERS.net MEMBERS_COMMENTS1.net MEMBERS_COMMENTS2.net MEMBERS_LANGUAGE.net MEMBERS_COVER_OHC.net MEMBERS_PROBLEM_LIST.net

The file names have this kind of structure:

00_1914330_MEMBERS.net

Can someone tell me why this does not achieve my end result?

add-pssnapin sqlservercmdletsnapin100
add-pssnapin sqlserverprovidersnapin100
cd SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_EXPORT_JOBS 
$ds = Invoke-Sqlcmd -ServerInstance 'LOCALHOST' -Query "SELECT CASE WHEN SUBSTRING([EXPORT_NAME],1,3) = 'MHC' THEN SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' ELSE [EXPORT_NAME]+'.net' END AS export FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] WHERE [JOB_NAME] = 'MHC_STG0_MEMBERS'"
foreach ($files in $ds) {
    $oldestfile = Get-ChildItem C:\Scripts | 
        where{!$_.PSIsContainer -and $_.Name.EndsWith("$($files.export)")} | 
        sort-object -property name | select-object -first 1 Name | 
        Copy-Item "C:\Scripts\" + $oldestfile.substring(7,$oldestfile.length - 8) `
                   C:\Scripts\MEMBERS
 }

Here is what I get when I run this:

Windows PowerShell Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS H:> C:\powershell_MoveKoreFiles.ps1 Unexpected token 'in' in expression or statement. At C:\powershell_MoveKoreFiles.ps1:1 char:472 + add-pssnapin sqlserverprovidersnapin100 add-pssnapin sqlservercmdletsnapin100 Set-Location SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_ EXPORT_JOBS $ds=Invoke-Sqlcmd -Query "SELECT CASE WHEN SUBSTRING([EXPORT_NAME], 1,3) = 'MHC' THEN SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' ELSE [ EXPORT_NAME]+'.net' END AS export FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] WHERE [ JOB_NAME] = 'MHC_STG0_MEMBERS'" -ServerInstance "LOCALHOST" foreach ($files in <<<< $ds){$oldestfile = Get-ChildItem C:\Scripts|where{!$.PSIsContainer -and $.Name.EndsWith("$($files.export)")}|sort-object -property name -descending|se lect-object -first 1 Name|Copy-Item -path "C:\Scripts\"+$oldestfile.substring(7 ,$oldestfile.length - 8) -destination C:\Scripts\MEMBERS} + CategoryInfo : ParserError: (in:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken

PS H:>

A: 

I think there might be one too many pipes in the script. Remove the one after the Select -first 1 Name e.g.:

Add-PSSnapin sqlservercmdletsnapin100
Add-PSSnapin sqlserverprovidersnapin100
cd SQLSERVER:\SQL\LOCALHOST\DEFAULT\Databases\SYSDB\Tables\dbo.STG0_EXPORT_JOBS
$ds = Invoke-Sqlcmd -ServerInstance 'LOCALHOST' -Query "SELECT CASE WHEN " +
          "SUBSTRING([EXPORT_NAME],1,3) = 'MHC' THEN " +
          "SUBSTRING([EXPORT_NAME],5,(LEN([EXPORT_NAME])))+'.net' " +
          "ELSE [EXPORT_NAME]+'.net' END AS export " +
          "FROM [SYSDB].[dbo].[STG0_EXPORT_JOBS] " +
          "WHERE [JOB_NAME] = 'MHC_STG0_MEMBERS'" 
foreach ($files in $ds) 
{ 
    $oldestfile = Get-ChildItem C:\Scripts |  
        Where {!$_.PSIsContainer -and $_.Name.EndsWith("$($files.export)")} |
        Sort name | Select Name -First 1
    $oldName = "C:\Scripts\$($oldestfile.substring(7,$oldestfile.length - 8))" 
    Copy-Item $oldName C:\Scripts\MEMBERS 
}
Keith Hill