This isn't exactly a problem with C# coding rules, rather I need help with logic. Here's my situation.

In my program, I deal with a JobID. Say, a print job, and there is a unique (obviously) JobID assigned to each print job. Now, regarding each job there are MANY files associated. Say, JobID = JOB_001 might have to save several files. Then at later times I might need to read those files based on the job id.

So here is the problem. I have a path which is fixed within the scope of the program. Say, ParentPath = "C:\MyFolder".
Now, I need to create a UNIQUE folder for each job under this folder. So, if I have JobIDs JOB_001 and JOB_002, I need two folders as follows.
"C:\MyFolder\JOB_001"
"C:\MyFolder\JOB_002"
The problem? JobID, which is a string, might contain characters that are invalid as folder names. For example, it might have something like JOB_?_001.

So far, I came up with a plan to replace any illegal character with "_" using

System.IO.Path.GetInvalidFileNameChars()

.
So my JobID would change to something like JOB___001.

This works for almost all practical purposes. However, there is a tiny chance, a chance nonetheless, that I might have two JobIDs such as JOB_?_001 and JOB_<_001 or some such character that is invalid as a file path. (I'm working on Windows, and my program runs only on Windows). So, I need the program to be able to tackle this problem as well.

In a nutshell, my primary need is, I need to create unique folders in Windows Platform for each JobID I get, and later be able to access those folders just by specifying the JobID.

Any ideas how I might get around this?

Ok, beforehand a small question: can't you take a control over filename generating and eliminate the insertion of illegal symbols into it's future name?

As a way to centralize your name converting, I would suggest to look at how they do the 8.3 format: How Windows Generates 8.3 File Names from Long File Names. That's just a start point to get your own method.

That's just a few tips to get into your problem .. tell me if it solves it, or what's wrong with it.

Antenka, thanks for the reply.

I wonder if I didn't make my question very understandable, because it is not the file name that I have a problem with.

Simply, I need to create folders inside a specific folder, and those folders should be based on the JobID in some way. Reason: later I need to access those folders (not files) based on the JobID. For instance if my program say check the folder for JobID = 001, then I should be able to find that folder inside the specific folder.

The way I have used, replacing illegal characters get the job done pretty well, however as I pointed out it gives a problem when two different illegal characters are in the JOBID, in the same place.

Oh, sorry for answering a wrong question :D Ok, how to deal with duplicates .. Ok, just a few thoughts ..
After getting the 2nd file name (the 1st has the same name), you may check if File.Exists.

Then you may want to include some difference into these files... here, I can suggest you 2 ways:
1. If you have plenty of files, you could append the files with " - Duplicate<N>" thingy as Windows does when copying a file in the same directory.
2. If you have not really much files, you may want to reorganize them .. e.g. putting them all together and moving to folders only those, who has duplicates. Here's an example.

Suppose, you have 2 jobs: JOB_001 and JOB_002. The first job has 3 files: File1.txt, File2.txt, File2.txt. The second one has 1 file, called File1.txt. So, in this case you may have this file structure:

C:
|--MyFolder
| |--JOB_001.File1.txt
| |--JOB_001.File2.txt
| | |--1
| | |--2
| |--JOB_002.File1.txt

Notice, that the "JOB_001.File1.txt" is a file, and the "JOB_001.File2.txt" is a folder.

Oh, sorry for answering a wrong question :D Ok, how to deal with duplicates .. Ok, just a few thoughts ..
After getting the 2nd file name (the 1st has the same name), you may check if File.Exists.

Then you may want to include some difference into these files... here, I can suggest you 2 ways:
1. If you have plenty of files, you could append the files with " - Duplicate<N>" thingy as Windows does when copying a file in the same directory.
2. If you have not really much files, you may want to reorganize them .. e.g. putting them all together and moving to folders only those, who has duplicates. Here's an example.

Suppose, you have 2 jobs: JOB_001 and JOB_002. The first job has 3 files: File1.txt, File2.txt, File2.txt. The second one has 1 file, called File1.txt. So, in this case you may have this file structure:

C:
|--MyFolder
| |--JOB_001.File1.txt
| |--JOB_001.File2.txt
| | |--1
| | |--2
| |--JOB_002.File1.txt

Notice, that the "JOB_001.File1.txt" is a file, and the "JOB_001.File2.txt" is a folder.

Antenka, I cannot create duplicate files or folders. This program runs along with a service and the service handles this program. So it might throw any JobID to my program, and the program creates a unique folder for that JobID if it does not exist already. And then store files in the relavent folder. Later, it might want to access files related to a certain Job, so the service throws just the JobID and my program should return the location of the folder relating to that particular JobID.

Actually, if I did not have this second requirement or restriction that I need to access the folder later, I would have used a GUID or something and my life would have been so much easier. Alas, life isn't easy.

I have an idea, how about if your job id is JOB_?_001, and you create a folder named as JOB_63_001&?

What I am trying to say here is whenever the illegal character appears in the job_id just replace it with its ascii. You will have to rearrange the whole character array though. The last character of the folder will determine, that char to ascii conversion took place for this specific folder.

Because there might be a case when you get job titles as "JOB_63_001" and "J0B_?_001".
It is just a suggestion...

Why not have an index file (or Dictionary class object) containing JobID and folder names.
Then the folder names could be anything you want.
When the service passes you a JobID, look it up in the index.
If it is not there then create a new folder and add it to the index.

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.