Question[Help]deleting / replacing

Posts 1–15 of 16 · Page 1 of 2
[Help]deleting / replacing
Few questions for you.

1- How can i delete all files in folder without deleteing folder?
- It doesn't work with kill("...")

2- How can i replace files
- Ex. I want to replace 1.jpg with 2.jpg


Thanks

Answer will get thanked and repped.
1. for each file in directory
io.file.delete(path)
next

(That sure isn't the fulll code -.-")

2. Delete current file, move/copy new one

Either way, Io.File Class is what you need.
Hmmz

[php]
Dim directoryLocation As String = C:\MyExamples\HelpMTH\LOL
For each file As String in FileIO.FileSystem.GetFiles(directoryLocation, FileIO.SearchOption.SearchTopLevelOnly)
IO.File.Delete(file)
Next
[/php]

That'll delete all files in a given directory at the top level only (won't scan subfolders)

To replace, use IO.File.Replace and use the info Visual Basic provides to figure out what goes where.
1- How can i delete all files in folder without deleteing folder?
- It doesn't work with kill("...")


Get all files in folder, delete all those files. The Kill(...) command is for terminating a process only - it has nothing to do with your file system

Code:
Imports System.IO

For Each item As String in Directory.GetFiles("YourFolderHere"))
    File.Delete(item)
Next
2- How can i replace files
- Ex. I want to replace 1.jpg with 2.jpg


Code:
Imports System.IO

    File.Delete("YourNEWFilePath)
    File.Copy("YourOLDFilePath, "YourNEWFilePath")
Note that if you want to move your file, you can use [ File.Move("YourOLDFilePath, "YourNEWFilePath") ] instead of [ File.Copy ]

By replacing, all you are doing is wiping the existence of your old file with your new one (aka, delete + copy/move). How dominant O:
Quote Originally Posted by freedompeace View Post
1- How can i delete all files in folder without deleteing folder?
- It doesn't work with kill("...")


Get all files in folder, delete all those files. The Kill(...) command is for terminating a process only - it has nothing to do with your file system

Code:
Imports System.IO

For Each item As String in Directory.GetFiles("YourFolderHere"))
    File.Delete(item)
Next
2- How can i replace files
- Ex. I want to replace 1.jpg with 2.jpg


Code:
Imports System.IO

    File.Delete("YourNEWFilePath)
    File.Copy("YourOLDFilePath, "YourNEWFilePath")
Note that if you want to move your file, you can use [ File.Move("YourOLDFilePath, "YourNEWFilePath") ] instead of [ File.Copy ]

By replacing, all you are doing is wiping the existence of your old file with your new one (aka, delete + copy/move). How dominant O:
EDIT: Don't worry.
Quote Originally Posted by Jason View Post


Uhh, exactly what I just posted? But IO.File.Replace is probably better for replacements.
Nope, My.Computer namespace is better. Even intellisense says that !!
Quote Originally Posted by Hassan View Post


Nope, My.Computer namespace is better. Even intellisense says that !!
Doesn't have the wildcard option though

But whatever floats your boat
Quote Originally Posted by Hassan View Post


Nope, My.Computer namespace is better. Even intellisense says that !!
There is no "better" one. They are both functional. Furthermore, you can use my snippet in both Visual Basic and C# languages. The My namespace is limited to Visual Basic, while the System.IO is not.
Quote Originally Posted by freedompeace View Post
There is no "better" one. They are both functional. Furthermore, you can use my snippet in both Visual Basic and C# languages. The My namespace is limited to Visual Basic, while the System.IO is not.















IO.File.WriteAllText - I want this function to accept wildcards, but I can't !!

Proves what ?
Quote Originally Posted by Hassan View Post

















IO.File.WriteAllText - I want this function to accept wildcards, but I can't !!

Proves what ?
?

What are you trying to imply? I don't see how you could use wildcards when you're writing a string to a file ...
Quote Originally Posted by freedompeace View Post
?

What are you trying to imply? I don't see how you could use wildcards when you're writing a string to a file ...

Oh carp, typo mistake. Meant AppendText parameter. But nvm, in 2010, they've included several more functions to Io.File class. So, meh...
Quote Originally Posted by Hassan View Post



Oh carp, typo mistake. Meant AppendText parameter. But nvm, in 2010, they've included several more functions to Io.File class. So, meh...
File.WriteAllText(destination, File.ReadAllText(destination + inputtext))

I think that's the parameter order, but correct me if I'm wrong (mobile).

It's not that much different. Rather than a "true" to append, you can just read the text and stick it in. Even better, take the file stream and just Encoding.ASCII.GetBytes() and stick it in to the end if the file.

You're right there though, there's no overload for appending text, but there are simple(Ish) measures of getting that functionality (speaking about .Bet 3.5). They've rectified this in .NET 4 though (not VS 2010)
Quote Originally Posted by freedompeace View Post
File.WriteAllText(destination, File.ReadAllText(destination + inputtext))

I think that's the parameter order, but correct me if I'm wrong (mobile).

It's not that much different. Rather than a "true" to append, you can just read the text and stick it in. Even better, take the file stream and just Encoding.ASCII.GetBytes() and stick it in to the end if the file.

You're right there though, there's no overload for appending text, but there are simple(Ish) measures of getting that functionality (speaking about .Net 3.5). They've rectified this in .NET 4 though (not VS 2010)
Functions are written to write quick and efficient code. If you're gonna always use:
File.ReadAllText(destination + inputtext)

Then it will be quite an annoying thing to do. But nvm, you can always write custom functions =D
Quote Originally Posted by Hassan View Post


Functions are written to write quick and efficient code. If you're gonna always use:
File.ReadAllText(destination + inputtext)

Then it will be quite an annoying thing to do. But nvm, you can always write custom functions =D
Which is why you put that in a function? S:
What kind of program are you making MTH?
Posts 1–15 of 16 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us