Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with VBScript using wildcards, move and rename

Status
Not open for further replies.

runnerlk

MIS
Jul 19, 2002
41
US
I have a simple VBScript that looks for a file called test.txt replaces the "|" with a "," and names the file test2.txt.
What I want to do is have the script look at any .txt files in the current directory, create the outputfile in a different directory and name it "ProcessedFile" with a datestamp.

Here is what i have done thus far.

InputFilePath = "test.txt"
OutputFilePath = "Test2.txt"

ForReading = 1

Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutputFile = FSO.CreateTextFile(OutputFilePath,False)
Set InputFile = FSO.GetFile(InputFilePath)
Set InputStream = InputFile.OpenAsTextStream(ForReading)

Total = 0

Do While Not InputStream.AtEndOfStream
Line = InputStream.ReadLine
Line = Replace(Line, "|", ",")
OutputFile.WriteLine(Line)
Total = Total + 1
Loop

msgbox "Total Lines Processed: " & Total

InputStream.Close
OutputFile.Close

Any help would be greatly appreciated.
 
simply" wrap your code in a loop and change the output file name in every loop. In order to differentiate between output files, include a counter (intNum) in the file name to guarantee it is unique.

Code:
Set FSO = CreateObject("Scripting.FileSystemObject")

strInputDir = "C:\some\path\to\files"
strOutputDir = "D:\another\path"

ForReading = 1
ForWriting = 2
intNum = 0

for each InputFile in FSO.GetFolder(strInputDir).Files
   if (lcase(right(InputFile.name, 4)) = ".txt") then
      intNum = intNum + 1  
      strOutputFile = strOutputDir & "\ProcessedFile_" & intNum & "_" & replace(date(), "/", "_") & ".txt"
      Set InputStream = FSO.OpenAsTextStream(InputFile.path, ForReading, true, 1)
      Set OutputStream = FSO.OpenAsTextStream(strOutputFile, ForWriting, true, 1)

      Total = 0
      Do While Not InputStream.AtEndOfStream
         Line = InputStream.ReadLine
         Line = Replace(Line, "|", ",")
         OutputStream.WriteLine(Line)
         Total = Total + 1
      Loop

      msgbox "Total Lines Processed: " & Total

      InputStream.Close
      OutputStream.Close
   end if
next

-Geates

 
thanks for the quick reply. I changed the paths and ran from the cmd line and nothing happened. Was there something else I needed to do?
Again, thanks so much
 
Couldn't get it to work. tried using the GetFile method in gtghe SetFile line and the CreateFile method for the Set OutPutFile still doesn't work.

Set FSO = CreateObject("Scripting.FileSystemObject")

strInputDir = "C:\TEST\"
strOutputDir = "C:\TEST2\"

ForReading = 1
ForWriting = 2
intNum = 0

for each InputFile in FSO.GetFolder(strInputDir).Files
if (lcase(right(InputFile.name, 4)) = ".txt") then
intNum = intNum + 1
strOutputFile = strOutputDir & "\ProcessedFile_" & intNum & "_" & replace(date(), "/", "_") & ".txt"
Set InputFile = FSO.GetFile(InputFile.path, ForReading, true)
Set OutputFile = FSO.CreateTextFile(strOutputFile, ForWriting, true, 1)

Total = 0
Do While Not InputStream.AtEndOfStream
Line = InputStream.ReadLine
Line = Replace(Line, "|", ",")
OutputStream.WriteLine(Line)
Total = Total + 1
Loop

msgbox "Total Lines Processed: " & Total

InputStream.Close
OutputStream.Close
end if
next
 
Try this:
Code:
Set FSO = CreateObject("Scripting.FileSystemObject")

strInputDir = "C:\TEST\"
strOutputDir = "C:\TEST2\"

ForReading = 1
ForWriting = 2
intNum = 0

for each InputFile in FSO.GetFolder(strInputDir).Files
   if (lcase(right(InputFile.name, 4)) = ".txt") then
      intNum = intNum + 1
      strOutputFile = strOutputDir & "\ProcessedFile_" & intNum & "_" & replace(date(), "/", "_") & ".txt"
      Set InputFile = FSO.OpenTextFile(InputFile.path, ForReading)
      Set OutputFile = FSO.OpenTextFile(strOutputFile, ForWriting, true)

      Total = 0
      Do While Not InputFile.AtEndOfStream
         Line = InputFile.ReadLine
         Line = Replace(Line, "|", ",")
         OutputFile.WriteLine(Line)
         Total = Total + 1
      Loop

      msgbox "Total Lines Processed: " & Total

      InputFile.Close
      OutputFile.Close
   end if
next
 
Perfect that worked. The only other thing the end users are asking for is to have a log file of the number of lines processed rather than a msgbox.
 
If you are stuck with the logfile part, just do it the same way as with the OutputFile, with one exception... for a logfile, you want to append to the file instead of overwriting. So, add an extra constant definition:
Code:
ForReading = 1
ForWriting = 2
[highlight #FCE94F]ForAppending = 8[/highlight]

And when you open it, use this argument:
Code:
Set LogFile = FSO.OpenTextFile(strLogFile, [highlight #FCE94F]ForAppending[/highlight], true)

Otherwise it's the same. I will leave the defining of LogFile, strLogFile, and choosing the text to add to the logfile up to you. Feel free to post your code if you hit a roadblock.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top