Solved

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

Posted on 2011-03-15
4
159 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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

10 Experts available now in Live!

Get 1:1 Help Now