execute dos command on results of a query.

  • I have a table called historyfile which contains a column that is called fullfilepath.

    I would like to run a select fullfilepath from historyfile and take the results and use them in a delete command at a command prompt to remove the directory.  I have an idea on how I can do this with a cursor, but since cursors are frowned upon I was hoping to find a way of doing this without the cursor.  Any suggestions are appreciated. 

     

  • Try this:

    declare @SQLCommand Varchar(500)

    declare @FullFilePath Varchar(100)

    Select @FullFilePath='d:\cert' --FullFilePath From yourtable Where something unique

    Set @SQLCommand='RD "' + @FullFilePath + '" /S /Q'

    Print @SQLCommand

    --Exec Master..xp_cmdshell @SQLCommand

    Once you confirm the printed @SQLCommand is correct then remove the comment char in Exec command line. /S is including all the sub-directories and /Q is quiet option.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply