I need to create a console application to print some help messages.I have done that but it does not show the results in the default tabular format in console like,
c:\Users\>dir /?
Displays a list of files and subdirectories in a directory.
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
Do i need to use escape sequences or is there any inbuilt function to display like this.I googled it.But unable to find solution can any one help:)?
You probably want to use Composite Formatting for this.
In this very specific case, simply put the content in a text file, and write to the console stream the content of the text file.
I would put the text file as a resource to simplify the deployment.
You should have to use #-quoted string literal.
string help = #"
Usage of #-quoted literal:
1. Escape sequences are not processed
2. To include double quotes then ""double it""
";
Console.WriteLine(help);
Just simply use spaces to format the output.
Console.WriteLine("DIR [drive:][path][filename] [/A[[:]attributes]]");
Console.WriteLine(" [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]]");
Related
I need to be able to extract the full file path out of this string (without whatever is after the file extension):
$/FilePath/FilePath/KeepsGoing/Folder/Script.sql (CS: 123456)
A simple solution such as the following could would work for this case, however it is only limited to a file extension with 3 characters:
(\$.*\..{3})
However, I find problems with this when the file contains multiple dots:
$/FilePath/FilePath/File.Setup.Task.exe.config (CS: 123456)
I need to be able to capture the full file path (from $ to the end of whatever the file extension is, which can be any number of things). I need to be able to get this no matter how many dots are in the name of the file. In some cases there are spaces in the name of the file too, so I need to be able to incorporate that.
Edit: The ending (CS....) in this case is not standard. All kinds of stuff can follow the path so I cannot predict what will come after the path, but the path will always be first. Sometimes spaces do exist in the file name.
Any suggestions?
Try this:
(\$.*\.[\w.-]+)
But! it will not properly match files with space or special chars in the file extension. If you need to match files that might have special chars in the file extension you'll need to elaborate on the input (is it quoted? is it escaped?).
I have an folder with more than 150 files, I want to collect a list with all the ones which contain a certain keyword. The keyword can be at the beginning or somewhere in the middle. "*.xml" catches all the xml files.
Here is my question when I do this "*partkey*.xml" does this catch all the files which contain the substring?
for example:
string[] files = Directory.GetFiles("thepathtothefolder", "*key*.xml");
Do I get my expected output?
You might want to look it up here. There you will find the "exact" description of the meaning for the wildcard characters * and ?. It is the same meaning the * caracter had since MS DOS times, it stands for 'zero or more' characters.
The line
string[] files = Directory.GetFiles("thepathtothefolder", "*key*.xml");
will give you an array with all the filenames that contain thre characters 'key'.
Yes. With search pattern "*partkey*.xml" you will get all the files that ends with ".xml" and contains string "partkey"
Output example:
123partkey123.xml
456partkey456.xml
I am trying to find files from a directory:
String[] search1 = Directory.GetFiles(voiceSource, "85267-*.wav")
.Select(path => Path.GetFileName(path))
.ToArray();
String[] search2 = Directory.GetFiles(voiceSource, "85267 *.wav")
.Select(path => Path.GetFileName(path))
.ToArray();
But in search1, it selects both 85267-s.wav and 85267 -s.wav. But I want only 85267-s.wav to be selected.
search2 is doing well.
How can I do that?
The behaviour you are experiencing is because of short file name. Since you will get 85267-~1.WAV for 85267 -s.wav and since that matches your wild card "85267-*.wav" you get both files back.
The is explained in Directory.GetFiles Method (String, String)
Because this method checks against file names with both the 8.3 file
name format and the long file name format, a search pattern similar
to "1.txt" may return unexpected file names. For example, using a
search pattern of "1.txt" will return "longfilename.txt" because the
equivalent 8.3 file name format would be "longf~1.txt".
For workaround you can use Directory.EnumerateFiles to first select both files matching your criteria and then compare the actual(long) file name part using StartsWith. Remember EnumerateFiles does lazy evaluation.
String[] search1 = Directory.EnumerateFiles(#"C:\test", "85267-*.wav")
.Where(file => Path.GetFileName(file).StartsWith("85267-"))
.Select(path => Path.GetFileName(path))
.ToArray();
Yes, this is a side-effect of the MS-Dos 8.3 short name support that's still turned on today on most file systems. Something you can see with the DIR /X command, it displays those short names. On my machine:
C:\temp>dir /x *.wav
01/21/2015 09:11 AM 6 85267-~1.WAV 85267 -s.wav
01/21/2015 09:11 AM 6 85267-s.wav
2 File(s) 12 bytes
0 Dir(s) 235,121,160,192 bytes free
Note how the short name for "85267 -s" is missing the space. It is not a valid character in a short name. What's left over now also matches your wildcard.
That's not where the trouble ends with those short names, A wildcard like *.wav will also match a file like foobar.wavx, a completely different file type.
Short-name generation is, frankly, a relic from the previous century that ought to be turned off today. But that is not typically anything you can control yourself. You have to deal with these accidental matches and double-check what you get back. With a Regex for example.
I need get last part means the numeric value(318, 319) of the following text (will vary)
C:\Uploads\X\X-1\37\Misc_318.pdf
C:\Uploads\X\X-1\37\Misc_ 319.pdf
C:\Uploads\X\C-1\37\Misc _ 320.pdf
Once I get that value I need to search for the entire folder. Once I find the files name with matching number, I need to remove all spaces and rename the file in that particular folder
Here is What I want
First get the last part of the file(numeric number may vary)
Based upon the number I get search in the folder to get all files names
Once I get the all files name check for spaces with file name and remove the spaces.
Finding the Number
If the naming follows the convention SOMEPATH\SomeText_[Optional spaces]999.pdf, try
var file = System.IO.Path.GetFileNameWithoutExtension(thePath);
string[] parts = file.split('_');
int number = int.Parse(parts[1]);
Of course, add error checking as appropriate. You may want to check that there are 2 parts after the split, and perhaps use int.TryParse() instead, depending on your confidence that the file names will follow that pattern and your ability to recover if TryParse() returns false.
Constructing the New File Name
I don't fully understand what you want to do once you have the number. However, have a look at Path.Combine() to build a new path if that's what you need, and you can use Directory.GetFiles() to search for a specific file name, or for files matching a pattern, in the desired directory.
Removing Spaces
If you have a file name with spaces in it, and you want all spaces removed, you can do
string newFilename = oldFilename.Replace(" ", "");
Here's a solution using a regex:
var s = #"C:\Uploads\X\X-1\37\Misc_ 319.pdf";
var match = Regex.Match(s, #"^.*?(\d+)(\.\w+)?$");
int i = int.Parse(match.Groups[1].Value);
// do something with i
It should work with or without an extension of any length (as long as it's a single extension, not like my file 123.tar.gz).
So, I feel lame for asking this, but I'm kinda stumped. I'm trying to get a list of file in a directory that end in tif ... only tif ... not tiff. So, I did this in C# ...
Directory.GetFiles(path, "*.tif", SearchOption.TopDirectoryOnly);
I would expect it to only return tif files, but that is not the case. I get tiff as well. I would think that if I supplied the mask .tif? that would get me both, but not the mask .tif. I tried it at a command prompt as well and I am getting both as well in DOS. Am I missing something here? This just seems wrong to me. I guess I could sanitize the results afterwards, but if I don't have to that would be best.
From MSDN:
When using the asterisk wildcard character in a searchPattern (for
example, "*.txt"), the matching behavior varies depending on the
length of the specified file extension. A searchPattern with a file
extension of exactly three characters returns files with an extension
of three or more characters, where the first three characters match
the file extension specified in the searchPattern. A searchPattern
with a file extension of one, two, or more than three characters
returns only files with extensions of exactly that length that match
the file extension specified in the searchPattern. When using the
question mark wildcard character, this method returns only files that
match the specified file extension. For example, given two files in a
directory, "file1.txt" and "file1.txtother", a search pattern of
"file?.txt" returns only the first file, while a search pattern of
"file*.txt" returns both files.
That's just how Directory.GetFiles works. From the manual:
When using the asterisk wildcard character in a searchPattern, such as
"*.txt", the matching behavior when the extension is exactly three
characters long is different than when the extension is more or less
than three characters long. A searchPattern with a file extension of
exactly three characters returns files having an extension of three or
more characters, where the first three characters match the file
extension specified in the searchPattern.
Directory.GetFiles internally uses FindFirstFile function from Win32 API.
From the documentation of FindFirstFile:
• The search includes the long and short file names.
A file that has long file name of asd.tiff will have a short file name like asd~1.tif and this is why it shows up in the results.
More than three character extensions are matched except when the path is on a network share (or mapped drive). For some reason the pattern only matches the long file name on remote drives.