0

I have the code below.

{
    var custname= "@1";
    var file = "c:/temp/"+ custname + ".txt";
    var fso  = new ActiveXObject("Scripting.FileSystemObject");
    var fh = fso.CreateTextFile(file, true, false);
    fh.Write(text);
    fh.Close();
}

The @1 is coming from a database and it's a customer's name. The above code works until I came across a customer name with a forward slash in it, e.g. "Company A/S".

How do I make this work for customers with the forward slash?

1
  • 2
    what system is this trying to create a file on? Commented Jul 12, 2011 at 20:55

5 Answers 5

7

Replace any slashes in the customer name with another character (e.g. an underscore).

var custname= "@1";
custname = custname.replace(/\//g, '_');
...

Note the use of the g modifier on the regular expression to ensure that every slash character is replaced.

This is also why a regexp is used instead of just '/' - the substring version of String.replace won't handle repeated occurences.

Sign up to request clarification or add additional context in comments.

Comments

1

You can replace (or delete) any characters not allowed by the filesystem. For example:

custname = custname.replace(/[/|&$]/g, '');

2 Comments

Just a note that this would only get the first /
@highlycaffeinated good call on the extra characters, although & is actually allowed on win7, at least. Not permitted are <, >, :, |, *, ? and "
0

You're creating the file from the customers name as well, so when they have a '/' in their name, the file directory could look something like:

var custname= "/isuck";
var file = "c:/temp/"+ custname + ".txt";

File would equal:

c:/temp//isuck.txt

Which wouldn't be a valid file. The SMART move would be to not allow any '/' marks in a customer name, which really doesn't make sense away. Or if they HAVE to put it in that way, remove the '/' before storing it, or replace it with another character.

Comments

0

Store an id, eg a number, for each customer in the database and get the database to return this. Use the number for the filename. If you then need the name do another query.

Comments

0

If it's Windows you can't have a file name with "/" in it so personally I'd just replace all "/" with something else.

var file = "c:/temp/"+ custname.replace(/\//g, "-") + ".txt";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.