1

I'm using the following query to view files/folders in a given directory. It works locally, but when trying to view a mapped drive I get...

"The system cannot find the path specified."

I have the permissions for the whole mapped drive set to the service account associated with SQL, but it is still not working.

declare @files table (ID int IDENTITY, FileName varchar(100))
insert into @files execute xp_cmdshell 'dir Z:\ /b'
select * from @files

End goal is to create a stored procedure that will transact specific file names into a table.

5
  • Use a UNC path (\\Server\Share) rather than a mapped drive. Drive mappings are specific the user/session.
    – Dan Guzman
    Commented Jul 15, 2018 at 15:44
  • @DanGuzman when I do so I'm getting The network name cannot be found
    – duckfeet23
    Commented Jul 15, 2018 at 16:28
  • Do you see the share listed if you login the to SQL box interactively with the service account and run NET VIEW \\server from a command prompt?
    – Dan Guzman
    Commented Jul 15, 2018 at 16:41
  • @DanGuzman Thanks I guess I might of overlooked something, but I'm going to be using the UNC path now thanks to you!
    – duckfeet23
    Commented Jul 15, 2018 at 17:28
  • Glad you got it working. I should add that such tasks are better done in application code or script rather than T-SQL and xp_cmdshell.
    – Dan Guzman
    Commented Jul 15, 2018 at 20:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.