Jump to content

any VBS coders in the house?


smokin5s

Recommended Posts

so I'm trying to write an easy vbs file that will take the contents of a directory and write them into a txt file... this part is easy, but for some reason, my code erases whatever was in the txt file before it was ran and I want it to just append to the end of the file... can someone please help?

 

On Error Resume Next

Dim fso, folder, files, NewsFile,sFolder

Set fso = CreateObject("Scripting.FileSystemObject")

sFolder = "C:\test"

If sFolder = "" Then

Wscript.Echo "No Folder parameter was passed"

Wscript.Quit

End If

Set NewFile = fso.CreateTextFile("c:\test1\FileList.txt", True)

Set folder = fso.GetFolder(sFolder)

Set files = folder.Files

For each folderIdx In files

NewFile.WriteLine(folderIdx.Name)

Next

NewFile.Close

Link to comment
Share on other sites

Instead of CreateTextFile, use OpenTextFile. It takes a few more arguments, one of which will be ForWriting and another will be True for "create if it's not already there"

 

Note that you can also write the contents of a directory to a text file in the command line by just doing this:

 

dir >> directory_contents.txt

 

The >> operate redirects the output to somewhere else, in this case to a text file of your choosing.

 

And you can get as fancy as you want with the dir command.

 

dir /a /q >> directory_contents.txt

 

eta: sorry, you'll want to use "ForAppending", not "ForWriting". My bad.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...