?
Solved

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

Posted on 2011-03-15
4
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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