Solved

take txt file path recreate path then copy file to destination.

Posted on 2011-03-15
4
157 Views
Last Modified: 2014-06-27
Hi All,

I need a script that can take a txt file with full path information of the file I want to copy. Recreate the folder path on a USB drive then copy the file onto the destination drive.

-------------------------------------------------------------------------------------------------------------------
I start off with a txt file that has the full path of files that I will eventually need to copy.

EXAMPLE STARTING FILE:

c:\my documents\test.txt
d:\example\E1\test.doc
\\testserver\test\test1\test3.ppt

-------------------------------------------------------------------------------------------------------------------
First from this file I need to create the folder path (empty folders with same name as origination path) at the destination location. Sometimes it is straight to the drive letter other times it would be to a folder located on the destination drive.

EXAMPLE DESTINATION DRIVE:

Destination drive is F:

F:\my documents
F:\example\E1
F:\testserver\test\test1

EXAMPLE DESTINATION DRIVE & FOLDER:

Destination is F:\copy

F:\copy\my documents
F:\copy\example\E1
F:\copy\testserver\test\test1

-------------------------------------------------------------------------------------------------------------------
Second I will need to copy the file from the txt file into the new destination file path that was created.

EXAMPLE DESTINATION DRIVE::

F:\my documents\test.txt
F:\example\E1\test.doc
F:\testserver\test\test1\test3.ppt

EXAMPLE DESTINATION DRIVE & FOLDER:

F:\copy\my documents\test.txt
F:\copy\example\E1\test.doc
F:\copy\testserver\test\test1\test3.ppt

-------------------------------------------------------------------------------------------------------------------
NOTES:
It will never be a mix of drive letter and drive letter+folder. It will be one or the other.


I have been knocking this around and all my approaches are flawed in that I am using multiple scripts and steps to complete which makes it imposable to turn over to the person responsible for completing the task so in true good management fashion I get stuck with completing someone else job because the manager wants it to be simplified.

Any ideas or help would be greatly appreciated.

Take care!
Killack
0
Comment
Question by:Killack
4 Comments
 

Author Comment

by:Killack
ID: 35142200
Oh and the file path may exceed 256 characters.

Thanks,
Killack
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35143515
Hi, try this.  Specify your input file, as a text file, and specify your destination folder.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1

strInputFile = "FilesToCopy.txT"
strDestinationFolder = "F:\Copy"

If Right(strDestinationFolder, 1) <> "\" Then strDestinationFolder = strDestinationFolder & "\"
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading, False)
While Not objInput.AtEndOfStream
	strLine = objInput.ReadLine
	strDestination = ""
	If Left(strLine, 2) = "\\" Then
		strDestination = strDestinationFolder & Mid(strLine, 3)
	ElseIf Mid(strLine, 2, 1) = ":" Then
		strDestination = strDestinationFolder & Mid(strLine, 4)
	End If
	If strDestination = "" Then
		WScript.Echo "Unable to determine target folder for " & strLine
	Else
		If objFSO.FolderExists(Left(strDestination, InStrRev(strDestination, "\"))) = False Then CreateLocalDirectoryStructure Left(strDestination, InStrRev(strDestination, "\"))
		If objFSO.FolderExists(Left(strDestination, InStrRev(strDestination, "\"))) = False Then objFSO.CopyFile strLine, strDestination, True
	End If
Wend
objInput.Close

WScript.Echo "Finished."

Sub CreateLocalDirectoryStructure(strPath)
	' Check if the path is a local file path
	'WScript.Echo "Argument passed for local creation: " & strPath
	If Mid(strPath, 2, 2) = ":\" Then
		If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
		arrBits = Split(strPath, "\")
		strSubPath = arrBits(0)
		If UBound(arrBits) > 0 Then
			For intBit = 1 To UBound(arrBits)
				strSubPath = strSubPath & "\" & arrBits(intBit)
				'WScript.Echo strSubPath & " is being checked..."
				If objFSO.FolderExists(strSubPath) = False Then
					objFSO.CreateFolder(strSubPath)
					'WScript.Echo strSubPath & " created."
				Else
					'WScript.Echo strSubPath & " exists."
				End If
			Next
		End If
	Else
		WScript.Echo "A local directory path was not passed to the CreateLocalDirectoryStructure procedure."
	End If
End Sub

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now