smokin5s Posted November 10, 2010 Report Share Posted November 10, 2010 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 Quote Link to comment Share on other sites More sharing options...
greg1647545532 Posted November 10, 2010 Report Share Posted November 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
smokin5s Posted November 10, 2010 Author Report Share Posted November 10, 2010 I'm not good at this stuff, where would I put ForAppending at? Quote Link to comment Share on other sites More sharing options...
greg1647545532 Posted November 10, 2010 Report Share Posted November 10, 2010 Replace: Set NewFile = fso.CreateTextFile("c:\test1\FileList.txt", True) With: Set NewFile = fso.OpenTextFile("c:\test1\FileList.txt",ForAppending,True) Quote Link to comment Share on other sites More sharing options...
smokin5s Posted November 10, 2010 Author Report Share Posted November 10, 2010 didn't work and I did remove the space you left in Appending Quote Link to comment Share on other sites More sharing options...
greg1647545532 Posted November 10, 2010 Report Share Posted November 10, 2010 What'd it do? (the space after Append was put in by the board, not sure why, so I wrapped it in code blocks. Good catch.) Quote Link to comment Share on other sites More sharing options...
smokin5s Posted November 10, 2010 Author Report Share Posted November 10, 2010 absolutely nothing. well it just doesn't display any results. Quote Link to comment Share on other sites More sharing options...
greg1647545532 Posted November 10, 2010 Report Share Posted November 10, 2010 gah. Replace "ForAppending" with "8" (no quotes) Apparently the constant isn't defined in windows. Quote Link to comment Share on other sites More sharing options...
smokin5s Posted November 10, 2010 Author Report Share Posted November 10, 2010 you're the man! Thanks Greg! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.