I often run into questions concerning simple file copy during a SCCM/MDT Task Sequence.  There are many ways to accomplish this, but it seams most people wind up using the good old XCOPY command, and in general there is nothing wrong with that.

When copying from a DP though, you might run into problems when copying all files and folders in the root on your source, as that points to the root of X:\windows and not the root of the package specified..

Well to work around any issues I created a small VBS script, that takes care of the copying as long as it resides in the root of the source folder. There are of cause ways around the xcopy issues, and as I mentioned other ways to copy files, but this script solution seems to work every time…

So, how to use the script.

1. Copy code from the below script to a .vbs file (or download from the bottom of  the page)

2. Place the script in the folder containing the files/folders you intend to copy, and that folder to a package.

3. In your TS, Add a Run Command Line step, and call the script from the package
image

In this example I created a Package called Files To Copy which contains all the files and folders I want to copy (and of cause also the script, that I call CopFiles.vbs)

– COPY EVERYTHING.

In the Command line: I typed in cscript.exe CopyFiles.vbs c:\TEST

This will copy everything in the package to C:\TEST and remove the CopyFiles.vbs script.

– COPY SELECTED FILES

In case I only want to copy, say two files from the package, the syntax would be:

 cscript.exe CopyFiles.vbs c:\TEST file1.txt file2.txt

This would only copy file1.txt and file2.txt to the destination c:\TEST

– COPY SELECTED FILES AND FOLDERS

In case I only want to copy, one file and one folder from the package, the syntax would be:

 cscript.exe CopyFiles.vbs c:\TEST file1.txt Folder1

– COPY CERTIN FILETYPES USING WILDCARD *

In case I want to copy all TXT files the syntax would be:

 cscript.exe CopyFiles.vbs c:\TEST *.txt

_______________

The script is shown below

' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  File copy relative to script location
' // File:      CopyFiles.vbs
' // Author:	Michael Petersen, Coretech A/S. [email protected]
' // Purpose:   Copy x number of files and folsders in a source folder to a target location
' // Usage: 	Place script i source folder, and define what to copy using arguments. (FIRST ARGUMENT MUST BE TARGET FOLDER)
' //
' //			To copy one or more file(s) and Folder(s)located in the source folder sypplpy TARGET and FILE/FOLDER name(s) (remember extensions on files)
' //			- Cscript.exe CopyFiles.vbs "TARGETFOLDER" "FILE1.XXX" "FOLDER1" "FILE2.XXX" "FOLDER2"
' //
' //			To copy all files and folders located in the source folder only supply TARGET
' //			 - Cscript.exe CopyFiles.vbs "TARGETFOLDER"
' //
' //
' // CORETECH A/S History:
' // 1.0.0     MIP 17/01/2011  Created initial version.
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************

Set oFSO = CreateObject("Scripting.FileSystemObject")

Const OverwriteExisting = True

'Get script location
sScriptLocation = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
sSource = Mid(sScriptLocation,1,Len(sScriptLocation)-1)
WScript.Echo "Source is: " & sSource

'Copy files and folders, or entire source
sArgNumber = WScript.Arguments.Count

If sArgNumber <> 0 Then
	sTargetFolder = WScript.Arguments.Item(0)
	WScript.Echo "Targetfolder is: " & sTargetFolder
	'Make sure the taget is not a file
	If Not (Left(Right(sTargetFolder,4),1)) = "." then
		'If only TARGET exists ad argument, everything will be copied
		If WScript.Arguments.Count = 1 Then 'If only
			oFSO.CopyFolder sSource, sTargetFolder, OverwriteExisting
			oFSO.DeleteFile(sTargetFolder & "\" & WScript.ScriptName)
			WScript.Echo "All files copied to Targetfolder " &  sTargetFolder
		Else
		'If files and folder arguments exist only these will be copied
			For i = 1 To sArgNumber -1
			sFileName =  WScript.Arguments.Item(i)
				If oFSO.FileExists(sFileName) Then
					WScript.Echo "File: " & SFileName & " Copied to: " & sTargetFolder
					oFSO.CopyFile sSource & "\" & sFileName, sTargetFolder & "\" & sFileName, OverwriteExisting
				ElseIf oFSO.FolderExists(sFileName) Then
					WScript.Echo "folder: " & sFileName & " Copied to: " & sTargetFolder
					oFSO.CopyFolder sSource & "\" & sFileName, sTargetFolder& "\" & sFileName, OverwriteExisting
				ElseIf (Left(Right(sFileName,5),1)) = "*" Then
					WScript.Echo "All : " & SFileName & " files Copied to: " & sTargetFolder
					oFSO.CopyFile sSource & "\" & sFileName, sTargetFolder & "\", OverwriteExisting
				Else
				WScript.Echo "ERROR - " & sFileName & ": does not exist in the source folder!"
				End If
			Next
		End If
	Else
		WScript.Echo "ERROR - "	& sTargetFolder & " Is not a valid FolderName. First Argument must be the tagret folder!"
		Wscript.Quit(1)
	End If
Else
	WScript.Echo "ERROR - No Arguments present!"
	Wscript.Quit(1)
End If

 

[download id=”26″]