Reputation: 8358
How can I encode a filepath in filename so the filename is valid and there are no collisions? In other words:
Example: filepath "C:\Program Files\My Program" -> filename "NGQqKY4pBaP7lPKQPD6Y..."
This example is only for illustration; there may be better (and simpler) ways to do this.
Problem background: This filepath encoding is used for a simple single-instance-check in Java: Two instances of the program can be run simultaneously if they are located in different directories, but not when they are located in the exact same directory. I know there are other ways to ensure the single-instance property, but I find the filepath encoding to be the best in terms of cost-benefit-ratio in my particular situation.
Upvotes: 3
Views: 1732
Reputation: 28074
Use the String of the path and encode just the specials, like:
% -> %%
\ -> %)
/ -> %(
: -> %;
No more special chars, but still useful and readable. However, since you just want to ensure uniqueness, a better (but not invertable solution) may be:
: -> ;
\ -> %
/ -> %
% -> %
I don't believe there will be a some%file and some/file in your filesystem, so this might work good enought for you.
Upvotes: 4