VB脚本处理保存原始名称的文件夹中的所有文件

时间:2014-11-09 22:01:56

标签: vbscript

我有一个简单的VBScript,它从txt文件中删除了2列。见下文。

Dim fso, tsIn, tsOut, TheLine

Set fso = CreateObject("Scripting.FileSystemObject")
Set tsIn = fso.OpenTextFile("c:\test\file.txt")
Set tsOut = fso.CreateTextFile("c:\test\Output.txt", True)

Do Until tsIn.AtEndOfStream
TheLine = tsIn.ReadLine
If InStr(1, TheLine, ",") > 0 Then
    TheLine = Left(TheLine, InStrRev(TheLine, ",") - 2)
End If
tsOut.WriteLine TheLine
Loop

tsIn.Close
tsOut.Close
Set tsIn = Nothing
Set tsOut = Nothing
Set fso = Nothing

我正在寻找它为文件夹中的所有文件执行此操作,并为每个具有相同名称但后跟日期的文件创建输出文件。任何帮助将不胜感激。

这是我更新的代码:

Dim fso, tsIn, tsOut, TheLine, f

today = Year(Date) & Right("00" & Month(Date), 2) & Right("00" & Day(Date), 2)

For Each f In fso.GetFolder("C:\test").Files
tsOutName = fso.GetBaseName(f) & today & fso.GetExtensionName(f)
tsOutPath = fso.BuildPath(f.ParentFolder, tsOutName)

Set tsIn = f.OpenAsTextStream
Set tsOut = fso.CreateTextFile(tsOutPath, True)

Do Until tsIn.AtEndOfStream

TheLine = tsIn.ReadLine
If InStr(1, TheLine, ",") > 0 Then
TheLine = Left(TheLine, InStrRev(TheLine, ",") - 2)
End If
tsOut.WriteLine TheLine
Loop

tsIn.Close
tsOut.Close
Next

1 个答案:

答案 0 :(得分:0)

改变这个:

Set tsIn = fso.OpenTextFile("c:\test\file.txt")
Set tsOut = fso.CreateTextFile("c:\test\Output.txt", True)

Do Until tsIn.AtEndOfStream
...
Loop

tsIn.Close
tsOut.Close

进入这个:

today = Year(Date) & Right("00" & Month(Date), 2) & Right("00" & Day(Date), 2)

For Each f In fso.GetFolder("C:\test").Files
  tsOutName = fso.GetBaseName(f) & today & "." & fso.GetExtensionName(f)
  tsOutPath = fso.BuildPath(f.ParentFolder, tsOutName)

  Set tsIn = f.OpenAsTextStream
  Set tsOut = fso.CreateTextFile(tsOutPath, True)

  Do Until tsIn.AtEndOfStream
  ...
  Loop

  tsIn.Close
  tsOut.Close
Next