从文本文件VBS复制文件 [英] VBS Copy Files from a text file

查看:509
本文介绍了从文本文件VBS复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,大家好我有我正在寻找一种方式来阅读文本文件,然后使用这些位置作为源位置和文件复制到一个单独的目标文件位置的文本文件。

我一直在玩周围,已经看到有关动态数组,但不能似乎明白了如何把数组的内容到变量改为源位置。

例如什么我迄今所做

 暗淡txtfile注册表昏暗的strDestinationFolderstrDestinationFolder =\\\\ SERVER \\ DESTLOGStxtfile注册表=C:\\ WINDOWS \\ TEMP \\ SOFTWARELOG.txt
FSO暗淡了:Set fso =的CreateObject(Scripting.FileSystemObject的)
暗淡F:将f = fso.OpenTextFile(txtfile注册表)做,直到f.AtEndOfStreamWScript.EchoPSTLocation:与& f.ReadLine;我可以在TXT文件阅读这里的每一行
fso.CopyFile strDestinationFolder,f.REadline循环

我也试着摆弄,但不知道从哪里开始,虽然它看起来是最可靠的?

 常量ForReading的= 1设置objFSO =的CreateObject(Scripting.FileSystemObject的)
设置objTextFile = objFSO.OpenTextFile _
(txtfile注册表,ForReading的)
做,直到objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList =斯普利特(strNextLine,,)
WScript.Echo服务器:&放大器; arrServiceList(0)
WScript.Echo服务:&放大器; objTextFile
对于k = 1至UBound函数(arrServiceList)
WScript.Echo vbTab&安培; 服务:&放大器; arrServiceList㈠
下一个
循环

任何指导,请,什么是我应该去这与VBS的最佳方式。

感谢


解决方案

  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

这篇关于从文本文件VBS复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆