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
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″]
Hi Michael, Thanks for this script! It makes copying files in SCCM reliable.
I found I have to pre-create a directory structure if there is more than one sub-directory deep, but other than that it seems to work like a charm.
A few minor typos in comments – but I know your English is better than my Danish! 🙂 Thanks again.
Hi Michael, this is brilliant exactly what I have been looking for! Thanks for this.
Great script, thanks for this – the only successful method I’ve found of getting a file copied to client. Many thanks!
Only thing for me is it doesn’t appear to be able to handle folders with spaces in the names, i.e. “Program Files”,
It throws error in line 43, oFSO.CopyFolder error path not found. But I’ll create a work around, use this script to copy to a temporary folder, then another command line to copy it where i need it!
[…] view source […]
[…] SEE: http://blog.coretech.dk/mip/making-file-copy-easy/ […]
Thanks, Michael. This worked flawlessly.
Hi Petersen,
Thanks for this post. But i failed to create the TS. Can you please help me.
3. In your TS, Add a Run Command Line step, and call the script from the package
I didn;t understand this point.
hi
Are you still having problems with the TS?? mine works flawlessly, if you need any help let me know
Men Thank you so Much!!! Great Script, saved my life. Now i’ll start studying some PowerShell, is AWESOME!!!
The script has a typo in line 55 which results on an error when specifying a folder as source (“error: path not found”). Change
oFSO.CopyFolder sSource & “” & sFileName, sTargetFolder& “” & sFileName, OverwriteExisting
to
oFSO.CopyFolder sSource & “” & sFileName, sTargetFolder & “” & sFileName, OverwriteExisting
(note the additional whitespace after sTargetFolder) and all is well.
[…] Back in the day anytime I needed to copy a file from server to client during a deployment, I was creating a new script from scratch. Eventually, I just started using a basic template. This was all of course, before I found the CoreTech file copy script. […]
[…] needed to copy some folder or some stupid ini to some random folder, but once I found this script: http://blog.coretech.dk/mip/making-file-copy-easy/ I just drop the script into a folder and call it as needed. You basically create a folder with what […]
It works great, thanks!
If I create a Package I need to type on the Command line, what should I type there?
And what is cscript.exe?
Should the folder “TEST” exist on target machine?
Hi Keywan,
1. In Command line type this: cscript.exe CopyFiles.vbs C:UsersPublicDesktopHotKey (Here I am copying folder named Hotkey to C:UsersPublicDesktop. I am copying there because I want this folder exists on anyone’s desktop who logs on)
2. With Cscript.exe, you can run scripts by typing the name of a script file at the command prompt
3. the folder does not have to exist on the target machines. I tested already.
Hi Selim,
Thanks for your replay. You mean the folder does not have to exist on the target machines. but I have just try again with your command line and the Software Center says it is installed but I don`t see any folder there. Is that a permission issue?
Is there a way to copy files and folders to the root of the drive on the machine I’m deploying to? If I specify a folder such as C:test, everything goes into the root of the test folder. If I use C: for the target, files and folders go into the folder c:NameOfFolderOnServer
Hi Marc,
i tryed your script with mdt2013 with the basic usage but i dont see anything on the desitnation machine.
i’am also missing the checkbox “package” in the 2013 “run command line script” task.
sry marc, forget my last post. works as expected – sometimes shit’s realy deep …
Frank,
Can you explain what you did in MDT 2013, because i cant find the package checkbox.
pls help.
I am also having the same issue, not able to find package checkbox in cmd. any suggestion?
Me either, but I am using Deployment Toolkit instead.. Could that be it?
Thank you for the script. How do you create the package after the files are added to the folder?
I keep getting an error when I try to test this. Line: 31 Char: 32 Expected End of Statement.
I believe it has an issue with the following this line but I don’t know what it would be:
WScript.Echo “Source is: ” & sSource
Hi Wayne, I had the same thing. Download the script file instead of copy/pasting the code to a new file, then it should work.
“and that folder to a package”
When you go to “Import OS Package”, it says very specifically:
“The specified folder and all subfolders will be scanned looking for Windows package files (security updates, service packs, language packs, etc.). Each found file (which ends in a .CAB or .MSU extension) will be added.”
So this adds nothing to the Deployment Share. Is this step necessary if it does nothing?