• erikd (6/25/2013)


    opc.three (6/25/2013)


    Gosh, that seems like a lot of code to move the newest file from one place to another. Here is how you would do the same in PowerShell:

    $SourceFileSpec = "H:\"

    $DestinationFileSpec = "J:\"

    $NewestFile = Get-ChildItem -Path $SourceFileSpec | Where {-not $_.PSIsContainer} | Sort-Object -Property LastWriteTime -Descending | Select-Object -Property FullName -First 1

    $NewestFile

    Move-Item -Path $NewestFile -Destination $DestinationFileSpec

    #send an email that the file was moved

    You could save this to a .ps1 file and then call it from a SQL Agent CmdExec job step using powershell.exe.

    PowerShell is on my long list of things to learn. I've read some on it, just not enough to comfortably use it, never mind integrate its use with SQL.

    Thanks

    I hear you. It's hard to branch out, but think about how much time you're spending trying to contort T-SQL to do what is in essence a set of file system management tasks. Consider what you're trying to accomplish, what SQL stands for and what SQL Server actually does best. Learning some basic PowerShell (this is basic stuff PowerShell does with ease) might be a worthwhile investment of your time to broaden your skills and eventually know that you're applying the right tool for the job at hand.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato