# Cross platform dynamic file path generation

**URL:** <https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243>\
**Category:** General\
**Created:** [27 August 2021 02:41 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243 "2021-08-27T02:41:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keithnz](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@keithnz](https://discourse.nodered.org/u/keithnz)\
**Post date:** [27 August 2021 02:41 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243/1 "2021-08-27T02:41:01Z")

</div>

I have searched extensively but I cant find anything about the best / simplest method of dynamically creating file paths within node-red functions that will work on both a linux and a windows host.

I am modifying an application written by someone else in our company that is intended to be deployed on whatever suitable host is available at the time. The person that wrote it used a windows host, but now I need to deploy it on a Linux host and the paths are all windows style. The biggest issue is "/" vs "" but also linux is case sensitive whereas windows isn't.

Can someone point me to a guide or something on the best practice for making it cross platform ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 August 2021 06:23 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243/2 "2021-08-27T06:23:11Z")

</div>

Welcome to the forum.

To make it cross platform always use forward slash (which works on both in most cases) and assume that names are case sensitive. Then it will work on both.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 August 2021 16:38 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243/3 "2021-08-27T16:38:10Z")

</div>

> [@keithnz](#):
>
> he biggest issue is "/" vs "\"

As Colin says, Windows supports "/" but prefers "\". If you want a true cross-platform answer, you should be using `path.join` in your code which will take care of that plus a bunch of other issues for you. That is what I recommend.

Another issues is drive letters and drive paths. Linux doesn't have drive letters and may struggle with drive paths (UNC paths).

Some versions of Windows (and some badly written Windows applications) may also struggle with path lengths and will certainly struggle with folder/file names that are valid on POSIX but not on Windows (such as ones that include `:` in the name for example).

So there are certainly some things you need to be cautious about but generally if you use `path.join` and avoid crazy file names and case-sensitive names, you should be fine 😀

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [28 August 2021 13:21 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243/4 "2021-08-28T13:21:21Z")

</div>

If your going to be dealing with listing Dir's and all the files in that Dir here is 2 functions I use that deal with path

```auto
let fs = require('fs'); //Node.js fs module see https://nodejs.dev/learn/the-nodejs-fs-module
let path = require('path'); //Node.js path module see https://nodejs.dev/learn/the-nodejs-path-module
       async function listDirectories(rootPath)
      {
          const fileNames = await fs.promises.readdir(rootPath);

          if ( fileNames.indexOf(".DS_Store") == 0 ) //for MacOS - if fileNames contains .DS_Store
          {
            fileNames.shift(); // get rid of it
          }
          const filePaths = fileNames.map(fileName => path.join(rootPath, fileName));
          const filePathsAndIsDirectoryFlagsPromises = filePaths.map(async filePath => ({path: filePath, isDirectory: (await fs.promises.stat(filePath)).isDirectory()}));
          const filePathsAndIsDirectoryFlags = await Promise.all(filePathsAndIsDirectoryFlagsPromises);
          return filePathsAndIsDirectoryFlags.filter(filePathAndIsDirectoryFlag => filePathAndIsDirectoryFlag.isDirectory)
              .map(filePathAndIsDirectoryFlag => filePathAndIsDirectoryFlag.path);
      }

      async function listFiles(rootPath)
      {
          const fileNames = await fs.promises.readdir(rootPath);
          if ( fileNames.indexOf(".DS_Store") == 0 ) //for MacOS - if fileNames contains .DS_Store
          {
            fileNames.shift(); // get rid of it
          }
          const filePaths = fileNames.map(fileName => path.join(rootPath, fileName));
          const filePathsAndIsFileFlagsPromises = filePaths.map(async filePath => ({path: filePath, isFile: (await fs.promises.stat(filePath)).isFile()}));
          const filePathsAndIsFileFlags = await Promise.all(filePathsAndIsFileFlagsPromises);
          return filePathsAndIsFileFlags.filter(filePathAndIsFileFlag => filePathAndIsFileFlag.isFile)
              .map(filePathAndIsFileFlag => filePathAndIsFileFlag.path);
      }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 October 2021 13:22 UTC](https://discourse.nodered.org/t/cross-platform-dynamic-file-path-generation/50243/5 "2021-10-27T13:22:09Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
