locked
Creating Folder using xp_cmdshell

    Question

  • Hi All:
    I am trying to create folder using xp_cmdshell it works fine...But the problem is I cant consider a space char
    in my folder name (i.e., Folder Name) the below script i used for .... It just create a folder ABC..
    Why it's not create the folder with the full name i.e.,
    ABC 1-X ...I don't know why.


    
    --EXEC spCreateF0lder 'C:\ComName\Test\' ,'ABC X-1'

    CREATE PROCEDURE spCreateF0lder
    @DefaultPath varchar(MAX)
    ,@FolderName VARCHAR(100)
    AS
    BEGIN

    DECLARE @SqlScript VARCHAR (MAX)

    SET @SqlScript = 'EXEC master.dbo.xp_cmdshell ''' + 'mkDir ' + @DefaultPath + @FolderName + ''''


    EXEC @SqlScript
    Print @SqlScript

    END
    GO



    Thanks

    Md. Marufuzzaman
    Sunday, December 27, 2009 12:39 PM

All replies

  • put double quotations on the both sides of the path...like below...

    EXEC

     

    master.dbo.xp_cmdshell ' mkDir "c:\Test2 test"'

    Thanks,

    Jahedur Rahman

    Wednesday, July 21, 2010 11:39 AM
  • put double quotations on the both sides of the path...like below...

    EXEC

     

    master.dbo.xp_cmdshell ' mkDir "c:\Test2 test"'

    Thanks,

    Jahedur Rahman


    Thanks very much! Could you explain it more clearly? I'm a beginner.
    Monday, August 30, 2010 11:25 PM
  • Hedda Davenport,

    If you want to create a folder named "Test" in C drive, below code will work perfectly...

    master.dbo.xp_cmdshell 'mkDir C:\Test'

    If you want to create a folder that contains space in its name, or if the path contains space(s), then this will not work. See below:

    master.dbo.xp_cmdshell 'mkDir C:\Test Folder'

    This will give you error because "Test Folder" contains a space between the words Test & Folder.

    If you want to create a folder named "Test" underneath "C:\Program Files" folder with below code, you will get the same error:

    master.dbo.xp_cmdshell 'mkDir C:\Program Files\Test'

    To avoid the error creating a folder with space in the name, or space in the path, just use double quotations at the both sides of the full path like below:

    master.dbo.xp_cmdshell 'mkDir "C:\Test Folder"'

    master.dbo.xp_cmdshell 'mkDir "C:\Program Files\Test"'

    But this will work for the first example as well:

    master.dbo.xp_cmdshell 'mkDir "C:\Test"'

    Hope this helps...

    -Jahedur Rahman

    Friday, September 03, 2010 6:07 AM


  • The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, January 28, 2011 5:32 AM