Get Only the File Name

  • Hi I have the following script

    --= Verify if table name exists

    IF NOT EXISTS (select name from sysobjects where xtype = 'U' and name = 'Test_Table')

    --= Create Table

    CREATE TABLE Test_Table (Files varchar(100))

    --= Populate Test_Table

    INSERT INTO Test_Table

    SELECT 'D:\File\Lester.xls' UNION ALL

    SELECT 'D:\File\Data\test\Test.xls' UNION ALL

    SELECT 'D:\Excel\Data\Sheet\Excel.xls'

    --= Select in Table Test_Table

    Select * from Test_table

    This will output the file path ofthe excel file. I want only the filename excluding the path for example

    D:\File\Data\test\Test.xls - i only want Test.xls

    I can do this by using the reverse command

    select reverse(substring (reverse(files),1,(charindex('\',reverse(files))-1))) from test_table

    Are there any other solution for this??

    "-=Still Learning=-"

    Lester Policarpio

  • If you input data manually, what if you only input file name?

    If you would like to input data programmically, you may consider to use Scripting.FileSystemObject, which can retrieve file name only.

  • Sure... dunno how many of these you have to do, but the following is about twice as fast because there's only 1 Reverse and RIGHT is faster than SUBSTRING.

    [font="Courier New"]DECLARE&nbsp@FileName&nbspVARCHAR(256)

    &nbsp&nbsp&nbsp&nbspSET&nbsp@FileName&nbsp=&nbsp'D:\File\Data\test\Test.xls'

    &nbspSELECT&nbspRIGHT(@FileName,CHARINDEX('\',REVERSE(@FileName))-[/font]1)

    Of course, if you want to get all file names from a particular directory...

    [font="Courier New"]--=====&nbspAssign&nbspa&nbsppath&nbspfor&nbspthe&nbspfiles&nbspto&nbspbe&nbspfound

    DECLARE&nbsp@Path&nbspVARCHAR(1000)

    &nbsp&nbsp&nbsp&nbspSET&nbsp@Path&nbsp=&nbsp'C:\'&nbsp--Must&nbspbe&nbspUNC&nbspif&nbspoutside&nbspof&nbspserver

    --=====&nbspTable&nbspto&nbsphold&nbspall&nbspthe&nbspdirectory&nbspinfo&nbspfrom&nbspxp_DirTree.

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspWill&nbsphave&nbspboth&nbspfile&nbspnames&nbspand&nbspdirectory&nbspnames.

    &nbspCREATE&nbspTABLE&nbsp#MyDir

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFileNum&nbsp&nbspINT&nbspIDENTITY&nbsp(1,1)&nbspPRIMARY&nbspKEY&nbspCLUSTERED,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspLongName&nbspVARCHAR(1000),

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspDepth&nbsp&nbsp&nbsp&nbspINT,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspIsFile&nbsp&nbsp&nbspINT

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp)

    --=====&nbspStore&nbspall&nbspthe&nbspdirectory&nbspinfo&nbspfor&nbsppath&nbspjust&nbspone&nbsplevel&nbspdeep

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspSyntax&nbspnote:&nbspMaster.dbo.xp_DirTree&nbsppath,level,markfiles

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbsp"level"&nbspshould&nbspprobably&nbspalways&nbspbe&nbsp"1".&nbsp"0"&nbspmeans&nbsp"all&nbsplevels".

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbsp"markfiles"&nbspwill&nbspproduce&nbspextra&nbspcolumn&nbsp"file"&nbspif&nbspis&nbspnot&nbspnull&nbspor&nbsp"0".

    &nbspINSERT&nbspINTO&nbsp#MyDir

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(LongName,Depth,IsFile)

    &nbsp&nbsp&nbspEXEC&nbspMaster.dbo.xp_DirTree&nbsp@Path,1,1

    --=====&nbspShow&nbsponly&nbspthe&nbspfile&nbspnames...

    &nbspSELECT&nbspLongName&nbsp

    &nbsp&nbsp&nbspFROM&nbsp#MyDir

    &nbsp&nbspWHERE&nbspIsFile&nbsp=&nbsp1

    &nbsp&nbsp&nbspDROP&nbspTABLE&nbsp#MyDir

    [/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks never thought of the RIGHT command can i do it without the reverse command?

    "-=Still Learning=-"

    Lester Policarpio

  • Yes, but it will take longer... you can do it with a Tally table and a join on the backslashes using SUBSTRING. I've already tried it (damn... should have kept the code) and it was quite a bit slower.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • when you can use CLR assembly, it is simple command in .NET System.Path.GetFileName(fullName). create and register user function, template is here http://msdn2.microsoft.com/cs-cz/library/ms131052(en-us).aspx

  • Why would you want to create, compile, register, and attach a CLR to do the same thing that was just done in T-SQL?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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