Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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