# Convert std.fs.realpath to file:// uri

**URL:** <https://ziggit.dev/t/convert-std-fs-realpath-to-file-uri/6758>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [November 8, 2024, 2:03pm UTC](https://ziggit.dev/t/convert-std-fs-realpath-to-file-uri/6758 "2024-11-08T14:03:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [November 8, 2024, 2:03pm UTC](https://ziggit.dev/t/convert-std-fs-realpath-to-file-uri/6758/1 "2024-11-08T14:03:31Z")

</div>

is there a library function that would convert my (windows) `"C:\dir\..."` realpath string into something suitable for a file:// uri???

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [November 8, 2024, 5:16pm UTC](https://ziggit.dev/t/convert-std-fs-realpath-to-file-uri/6758/2 "2024-11-08T17:16:08Z")

</div>

After replacing backslashes to slashes you can use [`std.Uri`](https://ziglang.org/documentation/master/std/#std.Uri) to encode it:

```zig
    const uri: std.Uri = .{
        .scheme = "file",
        .path = .{
            .raw = "C:/Program Files/foo",
        },
    };
    std.debug.print("{}\n", .{uri});

```

prints `file:C:/Program%20Files/foo`

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [November 8, 2024, 6:05pm UTC](https://ziggit.dev/t/convert-std-fs-realpath-to-file-uri/6758/3 "2024-11-08T18:05:10Z")

</div>

Just a side note, `file:C:/Program%20Files/foo` is technically not a valid file URI. File URI paths must be absolute and begin with a slash, so when encoding an absolute path with a Windows drive letter as a file URI you are required to prepend a slash (which you must then detect and strip when decoding). A correct file URI would be one of the following:

- `file:///C:/Program%20Files/foo` (most common)
- `file:/C:/Program%20Files/foo`
- `file://localhost/C:/Program%20Files/foo`
