Hello.

I have a app designed to organize dir’s on on the C drive where it was created. XML is extensively used in the business logic and house cleaning etc.

public static final String USERHOMEDIR = System.getProperty("user.home", ".");

<linkPath>
C:\Users\Steves\Documents\.targetGuide\admin_\admin\Note Folder\test\c drive test tab\index
</linkPath>

public static final String USERHOMEDIR = System.getProperty("user.home", ".");

Now, this app is running from a jump drive. Its intended purpose is to run as a standalone app on a jump drive and organize dir’s on the same thumb drive.

Now, when My XML is written it is in a different pattern that looks like this :

<linkPath>
K:\\.targetGuide\admin_\admin\Note Folder\CREATED ON THUMB\index
</linkPath>

<linkPath>
C:\Users\Steves\Documents\.targetGuide\admin_\admin\Note Folder\test\c drive test tab\index
</linkPath>

public static final String USERHOMEDIR = System.getProperty("user.home", ".");

First, What is the definition or difference between C:\ and K:\ concerning the double or single backslashes?

Secondly, If I use the jump drive at another client machine it may again change the drive letter to another relative path.

<linkPath>
L:\\.targetGuide\admin_\admin\Note Folder\CREATED ON THUMB\index
</linkPath>

Now when I get to the next client machine it may write another drive letter. Obviously, Reading XML elements using previous drive letters will not be found and the app will fail.

What are my options?

Can a variable be used to generalize the drive letter when written to XML? Then when the XML is read it could be translated to equal : ?

public static final String USERHOMEDIR = System.getProperty("user.dir", ".");

<linkPath>
L:\\.targetGuide\admin_\admin\Note Folder\CREATED ON THUMB\index
</linkPath>

I am not really able to think this one through. Yet, I would ask if it was possible to force a consistent unique drive letter regardless of the client machine?

Dani AI

Generated

Short answer: stop persisting the drive-letter and persist paths relative to the thumb-drive root (or use a tiny marker/manifest on the thumb to locate it at runtime). The double-backslash you saw is an escaping/serialization artifact (backslash is an escape in Java strings and many serializers), so inspect the writer, but don’t rely on that visual detail as your runtime contract. (docs.oracle.com)

Practical, reliable approach (what to change in your XML + runtime flow): write every thumb-drive path as a relative path plus a small root placeholder (for example ${THUMB_ROOT}/.targetGuide/...) or just store the path component beneath your app folder. On startup, scan mounted roots and find the thumb by the presence of your marker folder or manifest; then resolve the stored relative path against that root using java.nio.file APIs (relativize/resolve). That avoids depending on whatever drive letter Windows assigns. As pointed out, you can’t rely on a fixed letter on arbitrary machines without admin/manual setup. (docs.oracle.com)

Example patterns (save & resolve):

// when saving: compute and store a relative path (no drive letter)
Path thumbRoot = Paths.get(currentDriveRoot);       // e.g. "F:\"
Path absolute = Paths.get(absPathString);
Path relative = thumbRoot.relativize(absolute);
String xmlSaved = "${THUMB_ROOT}/" + relative.toString().replace(File.separatorChar, '/');
// at startup: find the thumb and resolve stored XML entries
for (File r : File.listRoots()) {
    Path marker = r.toPath().resolve(".targetGuide").resolve("manifest.json");
    if (Files.exists(marker)) { thumbRoot = r.toPath(); break; }
}
Path resolved = thumbRoot.resolve(storedXmlPath.substring("${THUMB_ROOT}/".length())
                                 .replace('/', File.separatorChar)).normalize();

Extra tips: use forward slashes in persisted XML to avoid escape noise and always parse/normalize with Path/Files so separators are handled for you. Keep a simple manifest (app id + version) at the thumb root so the app can uniquely identify its drive; if multiple drives have the marker add a second check (volume label or a UUID stored inside the manifest). (docs.oracle.com)

Recommended Answers

All 3 Replies

I'll try to enlight you a little bit.

First, we can not say you why the application is writing the double back slash before the point if you do not put here the code that produces those results.

Second, a drive letter probably can not be fixed unless you can fully administer on all the machines:
a) Hoy many phisical drives exist on the machine and the drive letters assigned to them
b) If the user can connect to a share using the net use on windows machines.
c) If the user can use the subst command on windows machines, to acces to a folder as if it was a drive
d) How USB (or firewire, or Sata) external drives will be mounted
e) The total number of mounted drives

Also have in mind that, on windows machines depending on the installed OS version, the free drives letter assignement goes from d to z while on others goes from z to d

Hope this helps you

Windows is the ONLY system (currently in common use) that uses backslashes for directory delimiters. It can also handle regular forward slash delimiters. The way any particular XML tool/library handles this is - well "indeterminate" comes to mind! Use forward slashes - which Windows can normally handle just fine, such as "C:/root/branch/node/file" instead of "C:\root\branch\node\file". If you do that, then your code should work also on non-windows systems!

I built the program and created the dir structure on C: drive only. So it always use the one slash pattern. I altimatly wanted the app to work form thumb drive only.

When I entered this phase I noticed as I jumped from one computer to the next while running the app on the thumb drive it was using the many drive letters possible with the double slash pattern.

If my program was able to evaluate a substring of a path before it was written to XML then could I enter it into my XML so no matter what machine I was using it would insert a variable in place of the ever changing drive letter. In this case K:\

[code]

1.  <linkPath>
2.  K:\\.targetGuide\admin_\admin\Note Folder\CREATED ON THUMB\index
3.  </linkPath>
4.  
5.  <linkPath>
6.  C:\Users\Steves\Documents\.targetGuide\admin_\admin\Note Folder\test\c drive test tab\index
7.  </linkPath>
8.  
9.  public static final String USERHOMEDIR = System.getProperty("user.dir", ".");

[/code] 

it would load a variable that would tell the app how to open the files in the dir on the thumb by replacing the drive letter to what it actually is on the various machine where the thumb drive is.

public static final String USERHOMEDIR = System.getProperty("user.dir", ".");

[/code][code]

1.  <linkPath>
2.  K:\\.targetGuide\admin_\admin\Note Folder\CREATED ON THUMB\index
3.  </linkPath>
4.  
5.  <linkPath>
6.  C:\Users\Steves\Documents\.targetGuide\admin_\admin\Note Folder\test\c drive test tab\index
7.  </linkPath>
8.  
9.  public static final String USERHOMEDIR = System.getProperty("user.dir", ".");

[/code] 

Does this sound possible?
Thanks for the insight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.