xp_cmdshell

  • Hello there,

    i would like to delete all the files in a directory in another server using xp_cmdshell

    EXEC xp_cmdshell 'FORFILES /p \\t-fc-fim\logs /m *.* /d -30 /c "CMD /C del /Q /F @FILE"'

    i get an error ERROR: UNC paths (\\machine\share) are not supported.

    what can i do to make this work ?

    your help is much appreciated.

    Thank you

  • You cannot use UNC paths in a CMD window.

    Try mapping the UNC path as a mapped drive or use SSIS with a file system task.

  • I mapped the network drive in sql sevrer box and i have ran this

    EXEC xp_cmdshell 'FORFILES /p I:\ /m *.* /d -30 /c "CMD /C del /Q /F @FILE"'

    ERROR: The specified directory does not exist.:w00t:

  • fhfh (12/13/2012)


    I mapped the network drive in sql sevrer box and i have ran this

    EXEC xp_cmdshell 'FORFILES /p I:\ /m *.* /d -30 /c "CMD /C del /Q /F @FILE"'

    ERROR: The specified directory does not exist.:w00t:

    I hope you might know the risk about cmdshell. You can use the 'Net use with uname and password' to access the share.

    Muthukkumaran Kaliyamoorthy
    https://www.sqlserverblogforum.com/

  • sounds user profile related, are you mapping it as a persistent mapping with the same account as the sql-server is running (I'm not sure this will work, but it might)

  • Do you know what is the syntax of NET use in my scenario ?

  • tried to map the network drive with username and password but no luck

  • hi,

    please refer below link, it will help u.

    http://www.howtogeek.com/118452/how-to-map-network-drives-from-the-command-prompt-in-windows/

  • fhfh (12/13/2012)


    Do you know what is the syntax of NET use in my scenario ?

    http://www.sqlservercentral.com/Forums/Topic667056-145-1.aspx#bm1027279

    Muthukkumaran Kaliyamoorthy
    https://www.sqlserverblogforum.com/

  • anthony.green (12/13/2012)


    You cannot use UNC paths in a CMD window.

    Just for clarification, You can use UNC paths in a CMD windows. It is FORFILES that does not allow UNC paths.

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • Never tried FORFILES but this works well and is very simplistic EXEC MASTER.dbo.xp_cmdshell 'del \\YourUNCPath\*.*'

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • if this is something to be scheduled you could avoid xp_cmdshell and run this powershell script with SQL Agent.

    $Now = Get-Date

    $Days = “30”

    $TargetFolder = “\\t-fc-fim\logs”

    $LastWrite = $Now.AddDays(-$days)

    get-childitem $TargetFolder *.* | Where {$_.LastWriteTime -le “$LastWrite”} |Remove-Item

    you will have to make sure that the account that SQL Agent is running under has modify permissions the the shared folder.

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

Viewing 12 posts - 1 through 11 (of 11 total)

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