MDT Scripting: Handling Files

I’ve always wanted to blog about undocumented features and today’s that day. Recently, I’ve been demonstrating how you can save time on scripting by using the various classes in the ZTIUtility.vbs when creating your custom deployment scripts. However, there’s no documentation or examples explaining how to access the file handling functions in the Utility class. In fact the MDT scripts use the oFSO.DeleteFile and oFSO.MoveFile methods to manage files rather than the available file handling functions. So here’s my article on the undocumented file handling methods.

There’s a list of File System Object functions in the ZTIUtility.vbs from lines 3872 to 4065. These can be called by using oFileHandling property and the relative functions as the methods. I could not find any information on Google or anywhere on oFileHandling.

To Delete a file.

I’ve created a file called C:\Test.txt that I intend to delete. The function DeleteFile(sFile) will attempt to delete a target file and create a log file entry displaying either success or failure.

To begin with, create an MDT Scripting Template (as per previous blogs). Then add this custom code.

Dim sTargetFile
sTargetFile = "C:\test.txt"

oFileHandling.DeleteFile sTargetFile

Run the script twice. The first time, your script will be deleted and the second time, you will get an error. You will find this logged under MININT\SMSOSD\OSDLOGS\ and the log file will have the same prefix as your script. This will also be logged in the BDD.log file.

There’s a list of other file handling functions below. I’ve also written some simple code examples to help you get started. You can use these examples to manage all your basic file management tasks.

To Move a File.

Function MoveFile(sFile,sDest)
Usage Example:

Dim sFile, sDest

sFile = "C:\Folder1\test.txt"
sDest = "C:\Folder2\"

'// Move a file.
oFileHandling.MoveFile sFile, sDest

To Copy a File.

Function CopyFile(sFile,sDest, bOverwrite)
Usage Example:

Dim sFile, sDest, bOverwrite

sFile = "C:\Folder1\test.txt"
sDest = "C:\Folder2\"
bOverwrite = True

' // Copy a file.
oFileHandling.CopyFile sFile, sDest, bOverwrite

To Copy a Folder.

Function CopyFolder (sSource, sDest, bOverwrite)
Usage Example:

Dim sSource, sDest, bOverwrite

sSource = "C:\Folder1"
sDest = "C:\Folder2"
bOverwrite = True

' // Copy a folder.
oFileHandling.CopyFolder sSource, sDest, bOverwrite

To Move a Folder.

Function MoveFolder (sSource, sDest )
Usage Example:

Dim sSource, sDest

sSource = "C:\Folder1"
sDest = "C:\Folder2\"

' // Move a folder.
oFileHandling.MoveFolder sSource, sDest

To Delete a Folder and Subdirectorys.

Function RemoveFolder(sPath)
Usage Example:

Dim sPath
sPath = "C:\Folder1"

' // Delete a folder and subfolders.
oFileHandling.RemoveFolder sPath

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in MDT 2010, Scripting and tagged , , . Bookmark the permalink.

15 Responses to MDT Scripting: Handling Files

  1. Patrick says:

    This guide is great, but I think I am missing something. I have tried every combination and I can’t even get the delete file script to work. It always says “File not found” no matter what I do. Here is an example from the command line:

    cscript \\davisp-w7\ds$\scripts\aaa.wsf
    Microsoft (R) Windows Script Host Version 5.8
    Copyright (C) Microsoft Corporation. All rights reserved.
    Microsoft Deployment Toolkit version: 6.0.1763.0
    Delete File: %SystemDrive%\test.txt
    FAILURE (Err): 53: Delete File: C:\test.txt – File not found
    aaa processing completed successfully.

    I can see the file with no issues. I have tried several different locations and using environment variables, as well as absolute paths. Any ideas?

    Like

    • Patrick says:

      I figured it out, just some typos 🙂 These guides are great, you should definitely make some more! There is just not enough information available out there. I was trying to come up with all kinds of work arounds and batch files until I discovered this site.

      You definitely do the name Scriptimus Prime justice 😀

      Like

  2. Thanks for the guide. I do have a question. I would like to use delete folder script and delete “c:\minint” and “_smstasksequence” folders from WinPE if found and the launch litetouch.vbs. I remember seeing it somewhere but could not get it to work. Have you done this before? Maybe you have a sample?
    Thank you.

    Like

  3. Rob says:

    Hello Andrew,

    What would the procedure be to copy a file from the deployment share to the PC when installing Windows? I’m thinking of having our custom wallpaper copied down to the PC and then using group policy preferences to set it as the default.

    Like

    • Peter Walilko says:

      IMO it would be easier to drop such files into the $OEM$ folder and let the deployment copy them over. Custom MDT scripts would be needed if you had to wrap more logic around it, like different files for different models, for example.

      Like

      • In that scenario that’s a good suggestion. However, there are many times when you want to copy files during your deployment that you don’t want on every machine. You may have data from a users share or team drive. This article is just to demonstrate generic scripting methods using the deployment template..

        Like

  4. Martony says:

    Could you explain?
    I have the same problem:
    Delete File: %USERPROFILE%\Desktop\Test.lnk
    FAILURE (Err): 53: Delete File: C:\Users\Administrator\Desktop\Test.lnk – File not found
    aaa processing completed successfully.

    When I use C:\Users\Administrator in my scripts it works, I can’t figure out why it doesn’t with %USERPROFILE%…

    Like

    • I’m pretty sure you have to expand the environment variable first. Like so:

      Dim sUserProfile
      sUserProfile = oShell.ExpandEnvironmentStrings(“%USERPRFOFILE%”)

      oFileHandling.DeleteFile sUserProfile & “\Desktop\Test.lnk”

      Like

  5. Martony says:

    I used specialfolders(userprofile).

    Like

  6. Thanks for the marvelous posting! I seriously enjoyed reading it, you will be
    a great author.I will be sure to bookmark your blog and may come back very soon.

    I want to encourage you to continue your great work,
    have a nice morning!

    Like

  7. Gyz says:

    Hi Andrew, Could you please explain how to delete only files&subfolders? I want to keep the main/parent folder..

    Like

  8. Jonathan R. says:

    Hi Andrew can we rename a file ? it seems like the moveFile method take the dest folder so we can’t use it to rename.

    Like

Leave a comment