?
Solved

Copying a file from source location to system32

Posted on 2008-11-06
5
Medium Priority
?
338 Views
Last Modified: 2012-05-05
Hello. I need to copy an .exe from it's source location to c:\windows\system32.  For example, I have a package that contains sleep.exe, several images, and a hta. This package will be deployed through SMS and each source will be different. I tried using the below but I cannot get it to work properly. The only copying I have done is from a static source.

Any advice would be appreciated?
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "sleep.exe" , "C:\windows\system32", OverwriteExisting

Open in new window

0
Comment
Question by:Lorrec
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22901124
Hi, if the VBS that you have above is in the same folder as the files you want to copy, you can obtain the VBS file's current folder, and tack the required files onto that....

Regards,

Rob.
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Sleep.exe"
objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting
strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Image1.jpg"
objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting
strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "MyHTA.hta"
objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting

Open in new window

0
 

Author Comment

by:Lorrec
ID: 22904417
Thank you for the post.

Do I leave this line as is or I am suppose to change it? I am not familiar with it.
strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Sleep.exe"

I found an hta that displays a notification to users. It allows users to postpone an installation. However, it uses wscript.sleep which is causing problems. The hourglass appears each time wscript.sleep runs.

I decided to create and call a .bat file inside the hta to handle sleep. The .bat file is using sleep 1 and does not produce the hourglass. The only problem is that since sleep.exe is not native to XP, I am having to copy it during the package.

So, in the hta, I have a vb script that looks like the below. Since I have not done a copy from a dynamic source, I am confused on how to get your advice to work. When I run it now,  it says wscript required. I am assuming that means I am not setting something properly.
          Dim Act   : Set Act = CreateObject("Wscript.Shell")
          Dim Fso   : Set Fso = CreateObject("Scripting.FileSystemObject")
          Dim Uname : Uname = Act.ExpandEnvironmentStrings("%UserName%")
          Dim SD    : SD = Act.ExpandEnvironmentStrings("%Temp%")
          Dim HrCnt	:	HrCnt = 0
          Dim SecCnt   : SecCnt = 1
          Dim MinCnt : MinCnt = 15
        '/-> HTA DO NOT SUPPORT THE STANDARD SLEEP THAT 
        '/-> WSCRIPT USES THESE ARE MY WORK AROUND 
        '/-> SLEEP 1 SECONDS
        Const OverwriteExisting = TRUE
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Sleep.exe"
        objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting
            Function Rst1()
              Dim Ts : Set Ts = Fso.OpenTextFile(SD & "\Rest1.bat", 2, True)
                Ts.WriteLine "Sleep 1" : Ts.close
                Act.run(SD & "\Rest1.bat"), 0 , True 
                If Fso.FileExists(SD & "\Rest1.bat") Then
        	        Fso.DeleteFile(SD & "\Rest1.bat")
                End If
            End Function

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22909216
Ah, OK, I wasn't aware that your VBS code was in a HTA.  This is why the WScript object is not supported, and also why you can't use WScript.Sleep or WScript.ScriptFullName.

So, with a HTA, the current path is returned by document.location, so we can get this value, and use that to find finds files in the current path.

I have now changed
strFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Sleep.exe"
to
        strFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "Sleep.exe"

after getting strHTAPath with
          If Mid(document.location, 6, 3) = "///" Then
                strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
          Else
                strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
          End If  

So you can try this.

Regards,

Rob.
          Dim Act   : Set Act = CreateObject("Wscript.Shell")
          Dim Fso   : Set Fso = CreateObject("Scripting.FileSystemObject")
          Dim Uname : Uname = Act.ExpandEnvironmentStrings("%UserName%")
          Dim SD    : SD = Act.ExpandEnvironmentStrings("%Temp%")
          Dim HrCnt	:	HrCnt = 0
          Dim SecCnt   : SecCnt = 1
          Dim MinCnt : MinCnt = 15
          '=== Get the path to the current HTA ===
	    If Mid(document.location, 6, 3) = "///" Then
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
	    Else
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
	    End If          
          '=======================================
        '/-> HTA DO NOT SUPPORT THE STANDARD SLEEP THAT 
        '/-> WSCRIPT USES THESE ARE MY WORK AROUND 
        '/-> SLEEP 1 SECONDS
        Const OverwriteExisting = TRUE
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "Sleep.exe"
        objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting
            Function Rst1()
              Dim Ts : Set Ts = Fso.OpenTextFile(SD & "\Rest1.bat", 2, True)
                Ts.WriteLine "Sleep 1" : Ts.close
                Act.run(SD & "\Rest1.bat"), 0 , True 
                If Fso.FileExists(SD & "\Rest1.bat") Then
        	        Fso.DeleteFile(SD & "\Rest1.bat")
                End If
            End Function

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 22909239
However, on another note, you don't need to use any third party sleep program in a HTA.  You can trick a HTA into waiting, by using a Shell command to run
ping 127.0.0.1 -n 1

where the -n 1 is now many seconds (roughly) it will wait.

Regards,

Rob.
          Dim Act   : Set Act = CreateObject("Wscript.Shell")
          Dim Fso   : Set Fso = CreateObject("Scripting.FileSystemObject")
          Dim Uname : Uname = Act.ExpandEnvironmentStrings("%UserName%")
          Dim SD    : SD = Act.ExpandEnvironmentStrings("%Temp%")
          Dim HrCnt	:	HrCnt = 0
          Dim SecCnt   : SecCnt = 1
          Dim MinCnt : MinCnt = 15
          '=== Get the path to the current HTA ===
	    If Mid(document.location, 6, 3) = "///" Then
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
	    Else
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
	    End If          
          '=======================================
        '/-> HTA DO NOT SUPPORT THE STANDARD SLEEP THAT 
        '/-> WSCRIPT USES THESE ARE MY WORK AROUND 
        '/-> SLEEP 1 SECONDS
        'Const OverwriteExisting = TRUE
        'Set objFSO = CreateObject("Scripting.FileSystemObject")
        'strFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "Sleep.exe"
        'objFSO.CopyFile strFile, "C:\windows\system32", OverwriteExisting
 
		HTASleep 3
		
            Sub HTASleep(intSeconds)
				Dim objShell : Set objShell = CreateObject("WScript.Shell")
				objShell.Run "ping 127.0.0.1 -n " & intSeconds, 0, True
            End Sub

Open in new window

0
 

Author Closing Comment

by:Lorrec
ID: 31514169
I apologize for the delay in my response. I was out of town. I really appreciate your response. The sleep that you supplied worked better. Thank you very much for the assistance.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Integration Management Part 2
Screencast - Getting to Know the Pipeline

850 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