-1
\$\begingroup\$

In Unity3D, How would I use something like this: path = "C:\Users\Jonathan\Documents\FileData" + fileName + ".txt"; so I can make a for loop and create several text files under desired path?

The loop looks like this currently:

named = namesAsset.text.Split ('\n');
sliced = statAsset.text.Split ('\n');
for (int i = 0; i < named.Length; i++) {
    fileName = named[i].ToLower();
    path = "C:\Users\Jonathan\Documents\FileData" + fileName.Substring(1) + ".txt";
    File.WriteAllText(path, sliced[i]);
    Debug.Log (named[i]+".txt");
}

But there is a problem: They won't accept this! It spits out the error that the name has an invalid char.

Full script:

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using System;

public class SplitPage : ScriptableWizard {

    public TextAsset statAsset;
    public TextAsset names;

    public string[] named = new string[151];
    public string[] sliced = new string[151];
    public string path;

    public string fileName;

    [MenuItem("GameObject/SlicePage")]
    static void CreateWizard() {
        ScriptableWizard.DisplayWizard<SplitPage>("Split Text File", "Create");
    }

    void OnWizardCreate() {
        named = names.text.Split ('\n');
        sliced = statAsset.text.Split ('\n');
        for (int i = 0; i < named.Length; i++) {
            fileName = named[i].ToLower();
            path = "C:\Users\Jonathan\Documents\FileData" + fileName.Substring(1) + ".txt";
            File.WriteAllText(path, sliced[i]);
            Debug.Log (named[i]+".txt");
        }

    }
}
\$\endgroup\$

closed as off-topic by MichaelHouse Sep 1 '14 at 15:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ You want several files with the same path? I don't get it. \$\endgroup\$ – Laurent Couvidou Sep 1 '14 at 14:49
  • \$\begingroup\$ Each one is SUPPOSED to change depending on what named[i] is at that point. \$\endgroup\$ – BillTrollberg Sep 1 '14 at 15:08
  • \$\begingroup\$ This is a bit confusing. Is that first code snippet meant to be part of the second one? They almost match, but not exactly. \$\endgroup\$ – Anko Sep 1 '14 at 21:55
  • \$\begingroup\$ They DO match, because that first bit was where the script goes Haywire. the 2nd is the full code. \$\endgroup\$ – BillTrollberg Sep 2 '14 at 1:21
1
\$\begingroup\$

It is impossible to know for certain from the given code what the issue is, but a reasonable guess is that you namesAsset string contain DOS/Windows style line endings, and therefore when you split by '\n' you leave an '\r' at the end of strings.

I of course assume that the path you use is only a placeholder for code that reference the user folder properly.

\$\endgroup\$
  • \$\begingroup\$ You're right on the money. however, I only changed the \FileData\ Portion of it. So it should be, Also, It's a Unity Wizard script. so if need be, I'll edit and show the full code. \$\endgroup\$ – BillTrollberg Sep 1 '14 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.