Dynamic mapping drives

  • Good Day

    I need to dynamically map drive letters on a computer.

    Meaning that, the script needs to check if the letter is already mapped and find one that is not.

    So in the end, I need to map a drive, delete old files, copy new files and disconnect the drive.

  • This was removed by the editor as SPAM

  • I have a doubt, do you want to achieve this through T-SQL...

    Naveen

  • I managed to get this and it works like a bomb

  • That's gr8!...Please share the code.

    Naveen

  • Another suggestion - don't map remote drives, use fully qualified UNC paths instead.

    ie copy \\REMOTESERVER\remoteShare\myFile.txt c:\localFolder\myFile.txt

    /Kenneth

  • Here is the code

    The code is from the guy called Nigel Rivett

    CREATE procedure ISL_FTP_PutFile

    @FTPServer varchar(128) ,

    @FTPUser varchar(128) ,

    @FTPPWD  varchar(128) ,

    @FTPPath varchar(128) ,

    @FTPFileName varchar(128) ,

    @SourcePath varchar(128) ,

    @SourceFile varchar(128) ,

    @workdir varchar(128)

    as

    /*Please Maggy do not forget to use no_output so that nothing is returned on the screen

    --exec master..xp_cmdshell 'net use h: \\ISLDEV3\C$\bcp'

    exec master..xp_cmdshell 'net use \\ISLDEV3\C$\bcp'

    exec ISL_ftp_PutFile 

      @FTPServer = '196.26.120.242' ,

      @FTPUser = 'isljb1s' ,

      @FTPPWD = 'isljse4' ,

      @FTPPath = 'c:\wertest\' ,

      @FTPFileName = 'ChangeToAnyName.zip' ,

      @SourcePath = '' ,

      @SourceFile = 'ddap.sprd.isl.xmlby.zip',

      

      @workdir = 'c:\wertest\'

    exec master..xp_cmdshell 'copy c:\wertest\*.zip \\ISLDEV3\C$\bcp'

    exec master..xp_cmdshell 'NET USE \\ISLDEV3\C$\bcp /DELETE /YES'

    */

    declare @cmd varchar(1000)

    declare @workfilename varchar(128)

     

     select @workfilename = 'ftpcmd.txt'

    select @workdir + @workfilename

    select @workdir , @workfilename

     

     -- deal with special characters for echo commands

     select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')

     select @FTPUser = replace(replace(replace(@FTPUser, '|', '^|'),'<','^<'),'>','^>')

     select @FTPPWD = replace(replace(replace(@FTPPWD, '|', '^|'),'<','^<'),'>','^>')

     select @FTPPath = replace(replace(replace(@FTPPath, '|', '^|'),'<','^<'),'>','^>')

     

    -- declare @cmd varchar(1000)

    --  select @cmd = 'echo open 196.26.120.242 > c:\wertest\ftpcmd.txt'

    --  select @cmd

    --  exec master..xp_cmdshell @cmd

     select @cmd = 'echo '     + 'open ' + @FTPServer

       + ' > ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo '     + @FTPUser

       + '>> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo '     + @FTPPWD

       + '>> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo cd .. '

       + '>> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo bin ' 

       + '>> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo '     + 'get ' + @SourcePath + @SourceFile + ' ' + @FTPPath + @FTPFileName

       + ' >> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     select @cmd = 'echo '     + 'quit'

       + ' >> ' + @workdir + @workfilename

     exec master..xp_cmdshell @cmd

     

     select @cmd = 'ftp -s:' + @workdir + @workfilename

     

     create table #a (id int identity(1,1), s varchar(1000))

     insert #a

     exec master..xp_cmdshell @cmd

     

     select id, ouputtmp = s from #a

    GO

  • I don't see any place where the code checks for drive mappings. And you know why? Drive mappings belong to USER PROFILES which SERVICES (like SQL SERVER ) have no knowledge of!

    Therefore, that code is accessing a remote machine using UNC Paths which are the recommended way for the reasons explained above!

    HTH

     


    * Noel

Viewing 8 posts - 1 through 7 (of 7 total)

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