Solved

VBScript - Path not found - Replace file script

Posted on 2012-03-23
14
1,242 Views
Last Modified: 2012-03-30
Hey guys,

I'm a bit flat out at the moment and I just need this script to work around a problem we are having with our OSD image through SCCM.

It is supposed to be a simple script that is called from a batch file in a client build task sequence. It is supposed to delete the default user profile in the C:\Users directory and copy a working Default user profile from the source directory where the script is run from. At this point I get a "path not found" error when I execute it, regardless of whether it is run off the network or locally on the PC.

This is what I have:

strDeleteFolder = "C:\Users\Default\"
strSourceFolder = "Default"
strDestFolder = "C:\Users\"
Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.DeleteFolder(strDeleteFolder)
objFSO.CopyFolder strSourceFolder, strDestFolder, True
set objFolder = objFSO.GetFolder(strDeleteFolder)
objFolder.Attributes = objFolder.Attributes XOR 2

WScript.Quit

Open in new window


Obviously when I am testing this I am logged in, so I have copied the directories and renamed them to Default1 so I can delete the the target directory. When this is deployed through the task sequence the C:\Users\Default directory will be unlocked and this fix can take place (I can do it manually).

If someone could help me out on where I am going wrong here, it would be greatly appreciated.

Cheers,
G_M
0
Comment
Question by:G_M
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 37756882
strSourceFolder should have full folder path like

strSourceFolder="C:\Default"
0
 

Author Comment

by:G_M
ID: 37756913
This isn't possible. The script is packaged in SCCM and the directory path needs to be flexible. I have already attempted to run script with a full file path in the strSourceFolder value as part of my troubleshooting. This made no difference.

I thought I possibly overlooked code required for handling hidden folders with subdirectories
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 37756926
Which line is the error showing up on?
0
 
LVL 9

Expert Comment

by:Dan Arseneau
ID: 37756985
Remove the backslash when dealing with folders.

strDeleteFolder = "C:\Users\Default\" should be
strDeleteFolder = "C:\Users\Default"
0
 
LVL 10

Expert Comment

by:Kezzi
ID: 37757628
I have edited on of my template scripts to suit your needs.  It will log every action it does to the c:\windows\temp location with the same name as the script.  It will also pass the exit code (if there is an error) to SCCM for the advert status.  

The code has an extra vb command you are not using, which is why you have problems.  It uses the GetParentFolderName(Wscript.ScriptFullName) to make sure that the folder name has the full unc path to query and copy.

My scripts have lots of checking in it, so if it cant find the source folder or destination folder or a file within those folders cannot be accessed, it will log it.

As this folder needs to be hidden on the destination, i have ammended my copy function to set the destination folder to hidden.  If you are going to use this script in future for copies of folders that do not require the folder to be hidden, set the attribute feature to 0 instead of 2.
Copy-folder-from-source.vbs
0
 

Author Comment

by:G_M
ID: 37760490
@DanArseneau: If I remove the backslash I get permission errors, overcame that before this issue came along ;o)

http://samuelhaddad.com/2009/06/26/vbscript-permission-denied-800a0046-solution/

@prashanthd: There are no line errors the code runs, but doesn't execute properly.

@Kezzi: Thank you for your comprehensive script. The script does not seem to do what I require though. It only copies the files in the directory, not the subfolders. Also, it doesn't remove the C:\Users\Default directory, thus keeping the old data contained within.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37763942
Have you tried replacing
strSourceFolder = "Default"

with
strSourceFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Default"

That should use <scriptcurrentfolder>\Default

Regards,

Rob.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:G_M
ID: 37764290
@RobSampson: I have tried the mod you suggested but I still get Path not found.

My current script (although less detailed than Kezzi's) deletes the C:\Users\Default1 folder but then errors on line 13, saying "path not found". Not sure if it is talking about strSourceFolder or strDestFolder or both. This is what I have so far:

' Deletes "C:\Users\Default" profile folder and replaces it with new Default profile.

Set objFSO = CreateObject("Scripting.FileSystemObject")
strDeleteFolder = "C:\Users" & "\Default1\"
strSourceFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Default"
'strSourceFolder = objFSO.GetParentFolderName(Wscript.ScriptFullName) & "\Default1\"
strDestFolder = C:\Users

Set objShell = CreateObject("WScript.Shell")   
strRun = "cmd /c rd /s /q """ & strDeleteFolder & """"   
objShell.Run strRun, 1, True  

objFSO.CopyFolder strSourceFolder, strDestFolder, True
set objFolder = objFSO.GetFolder(strDeleteFolder)
objFolder.Attributes = objFolder.Attributes XOR 2

WScript.Quit

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37764295
strDestFolder appears to be missing quotes....but you will need the trailing slash as well, it should be
strDestFolder = "C:\Users\"


Regards,

Rob.
0
 

Author Comment

by:G_M
ID: 37764297
sorry that was a troubleshooting edit, doesn't work with quotes or the trailing backslash either
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 37764329
OK, can we try this for some debugging? It will output the variables to a file at C:\DebugLog.txt so you can see what's happening if it fails.

One thing I noticed though, you had
set objFolder = objFSO.GetFolder(strDeleteFolder)

which would fail since you deleted the folder, so you can't use GetFolder on it. I've changed that.

Regards,

Rob.

' Deletes "C:\Users\Default" profile folder and replaces it with new Default profile.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\DebugLog.txt", True)

strDeleteFolder = "C:\Users\Default1\"
strSourceFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Default"
'strSourceFolder = objFSO.GetParentFolderName(Wscript.ScriptFullName) & "\Default1\"
strDestFolder = "C:\Users\"
If Right(strDestFolder, 1) <> "\" Then strDestFolder = strDestFolder & "\"

Set objShell = CreateObject("WScript.Shell")   
strRun = "cmd /c rd /s /q """ & strDeleteFolder & """"   
objLog.WriteLine "Running " & strRun
objShell.Run strRun, 1, True  

objLog.WriteLine "Copying " & strSourceFolder & " to " & strDestFolder
objFSO.CopyFolder strSourceFolder, strDestFolder, True
objLog.WriteLine "Binding to " & strDestFolder & objFSO.GetFolder(strSourceFolder.Name) & " to set attributes."
Set objFolder = objFSO.GetFolder(strDestFolder & objFSO.GetFolder(strSourceFolder.Name))
objFolder.Attributes = objFolder.Attributes XOR 2
objLog.WriteLine "Done"

WScript.Quit

Open in new window

0
 

Accepted Solution

by:
G_M earned 0 total points
ID: 37764545
@RobSampson: Your script worked for everything except applying the attribute. I just changed it back to what I had before. Because the strDeleteFolder gets replaced during the objFSO.CopyFolder process, the path is valid again to apply the attribute to.

Thank you Rob. This is the working final code.

Cheers
G_M

' Deletes "C:\Users\Default" profile folder and replaces it with new Default profile.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("C:\DebugLog.txt", True)

strDeleteFolder = "C:\Users\Default1\"
strSourceFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Default1"
strDestFolder = "C:\Users\"
If Right(strDestFolder, 1) <> "\" Then strDestFolder = strDestFolder & "\"

Set objShell = CreateObject("WScript.Shell")   
strRun = "cmd /c rd /s /q """ & strDeleteFolder & """"   
objLog.WriteLine "Running " & strRun
objShell.Run strRun, 1, True  

objLog.WriteLine "Copying " & strSourceFolder & " to " & strDestFolder
objFSO.CopyFolder strSourceFolder, strDestFolder, True
set objFolder = objFSO.GetFolder(strDeleteFolder)
objFolder.Attributes = objFolder.Attributes XOR 2
objLog.WriteLine "Done"

WScript.Quit

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37768219
OK cool. Thanks for that.  I just wasn't sure you were copying exactly the same folder back, since I though you were copying a folder called Default1.   Glad it works anyway.  I'm not sure what the original problem was though, out of that solution ;-)

Regards,

Rob.
0
 

Author Closing Comment

by:G_M
ID: 37786218
Rob provided a great solution to a frustrating problem. Cheers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now