473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to remove file path?

hello,
I am reading all files from a directory and writing them to a text file. This is working just fine.
Expand|Select|Wrap|Line Numbers
  1.   IO.File.WriteAllLines(strFile, IO.Directory.GetFiles(strPath, "*.txt")) 
How would i go about removing the file path from the list of file names in the text file.
This is information I do not need.
THanks,
Starlight849
Aug 9 '10 #1

✓ answered by PsychoCoder

Instead of doing any string parsing why not just use the Path.GetFileName Method, which returns just the file name from the path

Expand|Select|Wrap|Line Numbers
  1. Dim allFiles = From str1File As String In IO.Directory.GetFiles(strPath, "*.txt")
  2.                             Select System.IO.Path.GetFileName(str1File)

6 5407
Joseph Martell
198 Expert 128KB
You can do this using LINQ:

Expand|Select|Wrap|Line Numbers
  1.         Dim allFiles = From str1File As String In IO.Directory.GetFiles(strPath, "*.txt") _
  2.                                     Select str1File.Remove(0, strPath.Length + 1)
  3.  
Then use allFiles.ToArray() to get your string array to use with IO.File.WriteAllLines.

The LINQ expression will cycle through all of the files and remove the path. I had to add + 1 because my path did not end with "\". You could do this with a for each loop as well if you are using VB before .Net version 3.5. Either way you basically have to add at least 1 more variable.
Aug 9 '10 #2
PsychoCoder
465 Expert Mod 256MB
Instead of doing any string parsing why not just use the Path.GetFileName Method, which returns just the file name from the path

Expand|Select|Wrap|Line Numbers
  1. Dim allFiles = From str1File As String In IO.Directory.GetFiles(strPath, "*.txt")
  2.                             Select System.IO.Path.GetFileName(str1File)
Aug 9 '10 #3
Joseph Martell
198 Expert 128KB
@Richard McCutchen
Good call. I didn't remember that method was available. It would avoid the problem I had of padding the strPath.length attribute to handle the '\' character.
Aug 9 '10 #4
Thanks for all your help so far, guys. I am trying this out and getting an error.
unable to cast object of type '<Select Iterator>d_d'2[system.string,system.string]' to type 'system.string[]'
Aug 10 '10 #5
Joseph Martell
198 Expert 128KB
The only way that I have been able to reproduce your error has been to declare allFiles as a string array before the LINQ statement that Richard and I gave you. Notice that in both of our postings our statements start off with the "Dim" keyword.

Make sure that allFiles is NOT declared before this statement, or, if you really want it declared prior to this statement, declare it as IEnumerable(Of String).
Aug 10 '10 #6
Figured it out! Thanks for all the help, guys!
Aug 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
2
by: Teis Draiby | last post by:
(Using C#) Question 1: Is there an easy build-in support for determining wether a given file path is 1) A valid file path (Only legal characters) 2) The file exists 3) The path exists
10
by: darrel | last post by:
I have an input type="file" field that I am using to accept a file upload. This works, but I'm having problems with the filename property. In firefox, this: MyInputField.postedfile.filename ...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
3
by: Blake McNeill | last post by:
How do you retrieve a file path which was stored in the Registry as a string without ending up with a string which has all the slashes escaped which make it useless for file IO? I can see the file...
2
by: Sridhar | last post by:
Hi, I have a web form where it has a <input type=file id=file1> control. I have an Upload button to upload the file. WHen I click on browse and select one file, it is showing the full file path...
2
xtianixm
by: xtianixm | last post by:
hola everyone, how can i save a file path into mysql database without removing the '\' im using XAMPP for my database or phpmyadmin then when i try to insert a file path (ex....
0
xtianixm
by: xtianixm | last post by:
hola everyone, how can i save a file path into mysql database without removing the '\' im using XAMPP for my database or phpmyadmin then when i try to insert a file path (ex....
4
by: ManWithNoName | last post by:
I was wondering if it is possible to derive the file path from a loaded dom xml object, for example: $doc = new DOMDocument(); $doc->load('/path/to/file.xml'); foreach($doc->childNodes as...
10
by: Raheem | last post by:
Hello, I built a development version of a live website on my hosted account. However the development version is having problems with finding include files. After troubleshooting I was able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.