renaming a txt file

  • hi,

    Just I am renaming the one txt file

    code :--

    Declare @dt datetime ,@SQLstmt varchar(100)

    Set @dt=getdate()

    set @SQLstmt ='REN c:\ask\sintdat.dat '+convert(varchar(15),@dt,112)+'.txt'

    print @SQLstmt

    exec xp_cmdshell @SQLstmt

    output:--

    1. Access is denied.

    2. NULL

    so, pl. help me which is usefull me advance thanks

    ashok

  • I believe that it is related to the permissions of the folder where the file is.

    Check permissions (security) on the c:\ask folder or the file itself.

    You didn't say how/where you are running it from, but make sure that the user has modify access on that folder.

    (User would be account that is running SQL Service or SQL Agent or similar - this depends on the context -how/where is code executed)

    Sorry, I know this is not precise enough, but if you need additional help, please be more specific with the question.

    P.S. Is it really on the local C drive?

  • also want to know the correct answer.

  • Permission denied means exactly that... can't access the file. Not much else to add to that one.

  • xiewei (5/13/2011)


    also want to know the correct answer.

    See the post above your original...

    --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)
    Intro to Tally Tables and Functions

  • Hi,

    I have given every on permission to my C:\ Drive

    even though same error

    Thankks

  • Hi

    I have copied the txt file from c:\ drive to a table (txtid) through bulk insert it is working fine

    then same time i want rename the file it is giving the error

    the following is my code :--

    create table txtid

    (

    id varchar(200)

    )

    go

    select * from txtid1

    /* bulk insert from txt file to table */

    bulk insert txtid from 'c:\ASK\SINTDAT.DAT'

    with (fieldterminator = ',')

    go

    /* rename a txt file */

    Declare @dt datetime ,@SQLstmt varchar(100)

    Set @dt=getdate()

    set @SQLstmt ='REN c:\ask\sintdat.dat '+convert(varchar(15),@dt,112)+'.txt'

    print @SQLstmt

    exec xp_cmdshell @SQLstmt

    Thanks

    Ashok

  • Hi Ashok,

    It is almost impossible to help you without more information.

    Based on what you described, I believe it is Windows permissions issue.

    Could you please:

    - write where from you are running that code? (is it job, scheduled task that is running sql, stored procedure etc)

    - confirm that .dat file is not open while you are running your code

    - write exactly how is security set up on that file and the folder where the file is.

    Must be security, just check carefully the full path and make sure that the file inherits permissions from above.

  • ashok_bayyana (5/14/2011)


    Hi

    I have copied the txt file from c:\ drive to a table (txtid) through bulk insert it is working fine

    then same time i want rename the file it is giving the error

    the following is my code :--

    create table txtid

    (

    id varchar(200)

    )

    go

    select * from txtid1

    /* bulk insert from txt file to table */

    bulk insert txtid from 'c:\ASK\SINTDAT.DAT'

    with (fieldterminator = ',')

    go

    /* rename a txt file */

    Declare @dt datetime ,@SQLstmt varchar(100)

    Set @dt=getdate()

    set @SQLstmt ='REN c:\ask\sintdat.dat '+convert(varchar(15),@dt,112)+'.txt'

    print @SQLstmt

    exec xp_cmdshell @SQLstmt

    Thanks

    Ashok

    "C:Drive" ... WHICH C:Drive? On the SQL Server? On your desktop? What?

    --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)
    Intro to Tally Tables and Functions

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

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