Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:58:54+00:00 2026-05-13T12:58:54+00:00

I have a table that contains file paths, like so: ——————– |Files | ——————–

  • 0

I have a table that contains file paths, like so:

--------------------
|Files             |
--------------------
|path nvarchar(500)|
--------------------

I want to split it into two tables, one containing unique directories and one containing filenames:

---------------------------
|Files                    |
---------------------------
|filename    nvarchar(255)|
|directoryId int          |
---------------------------

---------------------------
|Directories              |
---------------------------
|id          int          |
|path        nvarchar(255)|
---------------------------

So for example if an entry originally was “C:/folder/file.jpg”, I want an entry in Directories for “C:/folder/”, and the entry in Files would be updated to have “file.jpg” for the filename and the directory id of the new entry in Directories.

(In case you’re wondering at this point, the reason I need to do this is because I need to keep track of some information at the directory level.)

Is there a good way to do this in a T SQL script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:58:55+00:00Added an answer on May 13, 2026 at 12:58 pm
    DECLARE @FULL VARCHAR(50)
    DECLARE @PATH VARCHAR(50)
    DECLARE @FILE VARCHAR(50)
    DECLARE @directoryId INT
    
    DECLARE curs CURSOR FAST_FORWARD FOR
    SELECT path FROM FILES_1
    
    OPEN curs
    FETCH NEXT FROM curs
    INTO @FULL
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @FILE = right(@FULL, charindex('/',reverse(@FULL))-1)
        SET @PATH = left(@FULL, len(@FULL) - charindex('/',reverse(@FULL))+1)
        SET @directoryId = SELECT ID from Directories WHERE  path = @PATH
    
        BEGIN TRANSACTION
    
            IF @directoryId IS NULL
            BEGIN
                INSERT INTO Directories VALUES (@PATH)
                SET @directoryId = @@IDENTITY
            END
    
            INSERT INTO  Files VALUES ( @FILE,@directoryId)
    
            IF @@ERROR <> 0 ROLLBACK
    
        COMMIT TRANSACTION
    END
    
    CLOSE curs
    DEALLOCATE curs
    

    Changed to a cursor!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 309k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this with a 3rd party tool: Zeta… May 13, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer It turns out that it is not possible to remove… May 13, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer You've got almost the right answer in the comments from… May 13, 2026 at 9:50 pm

Related Questions

I have an image gallery that is created using a repeater control. The repeater
I have a MySQL indexing question for you guys. I've got a very large
I need to open foxpro free tables in vb.net using the oledb connection. But...
I'm wondering how to make a button or input field in Interface Builder react
Huge database in mssql2005 with big codebase depending on the structure of this database.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.