且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从文本文件VBS复制文件

更新时间:2023-09-20 14:20:46

  WScript.EchoPSTLocation:&放大器; f.ReadLine
fso.CopyFile strDestinationFolder,f.REadline

您code相呼应,从文件中读取一行,然后尝试到目标文件夹复制到的下次的行从文件中读取。

如果你想要做多件事情与你需要读线分配给一个变​​量,然后使用该变量的文件中读取一行。此外,您还需要切换 CopyFile 方法的参数。来源至上,那么目的地。另外,如果你想在目的地是文件夹,它需要一个尾部的反斜杠(否则你会尝试覆盖一个文件,它会引发错误的文件夹)。

 做,直到f.AtEndOfStream
  行=修剪(f.ReadLine)
  WScript.EchoPSTLocation:与&线
  如果fso.FileExists(线),再fso.CopyFile线,strDestinationFolder&安培; \\
循环

TRIM()占虚假领先/在读行尾部的空格,它总是一个好主意,请检查您是否尝试做之前的文件确实存在任何与它。

编辑::用于检测现有目标文件,并追加一个流水号到文件名尝试是这样的:

 基本名称= fso.GetBaseName(行)
延长= fso.GetExtensionName(线)destinationFile = fso.BuildPath(strDestinationFolder,基本名称和放大器;&AMP。扩展名)I = 1
做虽然fso.FileExists(destinationFile)
  文件名=基本名称和放大器; I和。 &安培;延期
  destinationFile = fso.BuildPath(strDestinationFolder,文件名)
  I = I + 1
循环fso.CopyFile线,destinationFile

Hiya Guys I have a text file with locations of files I'm looking for a way to read the text file and then use those locations as a source location and copy the files to a seperate destination.

I've been playing around and have seen about dynamic arrays but cant seem to understand how to put the contents of the array into variables to read as source location.

example of what I have done so far

Dim TxtFile

dim strDestinationFolder

strDestinationFolder = "\\SERVER\DESTLOGS"

TxtFile = "c:\windows\temp\SOFTWARELOG.txt"


Dim fso:    Set fso = CreateObject("Scripting.FileSystemObject")
Dim f:  Set f = fso.OpenTextFile(TxtFile)

Do Until f.AtEndOfStream

WScript.Echo "PSTLocation: " & f.ReadLine   ; I can read each line here in the txt file 
fso.CopyFile strDestinationFolder, f.REadline  

Loop

I've also tried playing with, but not sure where to start though it looks the most reliable?

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(TxtFile, ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
WScript.Echo "Server: " & arrServiceList(0)
WScript.Echo "Service: " & objTextFile
For k = 1 to UBound(arrServiceList)
WScript.Echo vbTab & "Service: " & arrServiceList(i)


Next
Loop

Any Guidance please as to what is the best way I should go about this with vbs.

Thanks

WScript.Echo "PSTLocation: " & f.ReadLine
fso.CopyFile strDestinationFolder, f.REadline

Your code echoes one line read from the file, and then tries to copy the destination folder to the next line read from the file.

If you want to do more than one thing with a line read from a file you need to assign the read line to a variable and then use that variable. Also, you need to switch the arguments of the CopyFile method. Source comes first, then destination. Plus, if you want the destination to be a folder, it needs a trailing backslash (otherwise you'd try to overwrite a folder with a file, which raises an error).

Do Until f.AtEndOfStream
  line = Trim(f.ReadLine)
  WScript.Echo "PSTLocation: " & line
  If fso.FileExists(line) Then fso.CopyFile line, strDestinationFolder & "\"
Loop

The Trim() accounts for spurious leading/trailing spaces in the read line, and it's always a good idea to check if a file actually exists before you try to do anything with it.

Edit: For detecting an existing destination file and appending a running number to the file name try something like this:

basename  = fso.GetBaseName(line)
extension = fso.GetExtensionName(line)

destinationFile = fso.BuildPath(strDestinationFolder, basename & "." & extension)

i = 1
Do While fso.FileExists(destinationFile)
  filename = basename & i & "." & extension
  destinationFile = fso.BuildPath(strDestinationFolder, filename)
  i = i + 1
Loop

fso.CopyFile line, destinationFile