0

I'm developing a command-line utility and passing paths as arguments inside a batch file. Do I need to add the @ symbol to the paths within my application to prevent characters like "\" from escaping?

This link specifies using the @ symbol, however currently, I'm not using the @ or \\ to prevent escaping. When I pass in my path as it is, it works fine. Why is this?

I would call it like this, inside my batch file:

foldercleaner.exe -tPath: "C:\Users\Someuser\DirectoryToClean"

Program:

class Program
    {
        public static void Main(string[] args)
        {
            if(args[0] == "-tpath:" || args[0] == "-tPath:"  && !isBlank(args[1]))  {
                    clearPath(args[1]);
            }
            else{

                Console.WriteLine("Parameter is either -tpath:/-tPath: and you must provide a valid path");
            }
    }  

Clear Path Method:

public static void clearPath(string path)
        {
            if(Directory.Exists(path)){

                int directoryCount = Directory.GetDirectories(path).Length;

                if(directoryCount > 0){

                    DirectoryInfo di = new DirectoryInfo(path);

                    foreach (DirectoryInfo dir in di.GetDirectories())
                    {
                        dir.Delete(true); 
                    }

                }
                else{

                    Console.WriteLine("No Subdirectories to Remove");
                }

                int fileCount = Directory.GetFiles(path).Length;

                if(fileCount > 0){

                    System.IO.DirectoryInfo di = new DirectoryInfo(path);

                    foreach (FileInfo file in di.GetFiles())
                    {
                            file.Delete(); 
                    }


                }
                else{

                    Console.WriteLine("No Files to Remove");
                }

                }
            else{

                Console.WriteLine("Path Doesn't Exist {0}", path);
            }
        }
Community
  • 1
  • 1
Emma Geller-Green
  • 297
  • 1
  • 4
  • 9
  • 2
    The escaping happen on the string literals. You don't have literals in this piece of code, just variables. There is no need to escape the content of the variables – Steve Jan 07 '16 at 18:18
  • @AH!, so unless I declare a variable with a path, I don't have to worry, correct? – Emma Geller-Green Jan 07 '16 at 18:19
  • 1
    If you declare a variable AND INITIALIZE IT with a string literal expressing a path you need the Verbatim @ character in front of the literal to avoid the escaping. In your code the literal comes from the command line environment that probably has its own way to handle the \ (if any method is required) – Steve Jan 07 '16 at 18:21

1 Answers1

2

Escaping of special characters (like " or ) is only needed inside of String literals within your code:

var str = "This is a literal";
var str2 = otherVariable; //This is not a literal

No need to escape the characters when calling the application.

However, when using Batch for example, there might be a different set of special characters with different types of escape characters. For example, if you want to pass a "%" (from Batch) you need to pass the escape sequence "%%".

Matthias
  • 12,053
  • 4
  • 49
  • 91