• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1322
  • Last Modified:

Copy to SystemRoot using VBS

Can anyone tell me why this code wont work. Ive been looking everywhere. I want file.txt  to be copied to the temp folder located in the Systemroot which is usually C:\winnt or C:\windows.

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\file.txt" , "%SystemRoot%\temp", OverwriteExisting
0
gbarrientos
Asked:
gbarrientos
  • 10
  • 6
  • 3
  • +1
1 Solution
 
diegojserranoCommented:
you are using vbs, don't you?
0
 
T_HermanCommented:
You have to "Expand" the environment variables.  As diegojserrano asked, I'm assuming you are doing VBScript.  It looked like a VB Script.

For example:

Dim objShell
Set objShell = CreateObject("wscript.Shell")
strSystemRoot = objShell.ExpandEnvironmentStrings("%SystemRoot%")

Then just use the strUserProfile variable in your code:

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\file.txt" , strSystemRoot & "\temp", OverwriteExisting
0
 
T_HermanCommented:
Hmmm... Was that correctly submitted as an answer or is everythign a comment until you accept it?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
cookreCommented:
Yup, the Answer button was removed a while back - too much misuse of it.
0
 
T_HermanCommented:
Woops... quick addendum.  When I said "Then just use the strUserProfile variable in your code:" I meant to say "Then just use the strSystemRoot variable in your code:".  Sorry about that.

0
 
gbarrientosAuthor Commented:
Sounds good but.... when i use the following code i receive

Line: 7
Char: 1
Error: Permission Denied
Code: 800A0046


Dim objShell
Set objShell = CreateObject("wscript.Shell")
strSystemRoot = objShell.ExpandEnvironmentStrings("%SystemRoot%")

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\file.txt" , strSystemRoot & "\test", OverwriteExisting
0
 
T_HermanCommented:
Code wise your problem is solved.  Now you have to deal with user rights.  Whatever account you are using either does not have access to write to the system root directory or the file in question is in use.. in which case you cannot overwrite it.

That is a completely different issue.  If the file is already there and you are certain it is not open, go ahead and remove it then run the code.  If you have user rights to do it the file should be copied.  Be absolutely possitive nothing else has the file open if it exists though or you'll always get that same problem.
0
 
gbarrientosAuthor Commented:
Sorry i forgot to say that i am Adminitrator of the box as well as Domain Administrator. I was able to copy the file simply using the command promt so why wont the script let me do it?
0
 
gbarrientosAuthor Commented:
This code seems to work...however i will still like to know why %systemroot% wont work on the above.

set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run ("cmd /c copy C:\file.txt %systemroot%\test /y")
0
 
T_HermanCommented:
Because "cmd /c copy C:\file.txt %systemroot%\test /y" is being passed to the run function as a string.   It seems Microsoft did not both with expanding environment variables in that function.  When you use them in a batch file, the OS automatically expands them as appropriate.  That's why Microsoft built the capability of expanding them yourself using the shell object.

If you felt adventorous you could write your own function to parse out and expand environment variables and return the complete string.  If you are only using a couple, using the shell object to expand them is much easier.
0
 
T_HermanCommented:
Ummm.. when are you going to close this out and pass out the points to someone?  If it's me, I could use them to ask another question.
0
 
T_HermanCommented:
Yeah, I answered his question and don't understand why you are saying you will close the questiona nd refund his points?

He asked why his couple lines of code wouldn't work.. and I gave hiim specifics on why and even included what to do to make it work.  Then gave him details on why it works that way.  I more then fufilled my side of the bargin in answering the question.  I don't think it's fair that someone asking a question can simply request to have the question closed and you guys go ahead and decide to close it.

I know one person means nothing to you but if you guys close this and refund his points, when I answered his question, I will never use the site again and I'll get as many as my friends to refuse to contribute.  That is a ridiculous way to do things and you're only letting him abuse this site.
0
 
cookreCommented:
T_Herman's resonses were complete and accurate
0
 
T_HermanCommented:
Thank you for the vote of confidence cookre.  I don't mean to come off as being picky or annoying but I truly feel i'm being taken advantage of.  I like to help peopel and have been working with other people on here.  This is the first time I've had this happen.  The author simply stopped commenting and went to the administartors to close the question rather then pay up the points.  Just felt I was being taken advantage of.

Again, thanks for the vote of confidence.
0
 
gbarrientosAuthor Commented:
T Herman the code you submitted didnt work...and its not a permissions issues because i tried it on variouse computers..even computers that have Fat32 file systems. I still get an error.
0
 
gbarrientosAuthor Commented:
BTW T Herman i dont care about the points. But what i do care about is someone submitting code that DOES NOT WORK and requesting or better yet demanding that they get points.  The objective was to copy a file from one place to another using the system root variable. Giving me information about why my code didnt work is unneseracy. Oh and one more thing... " I'll get as many as my friends to refuse to contribute" how childish is that?!
0
 
gbarrientosAuthor Commented:
unnecessary*
0
 
cookreCommented:
T_Herman, Sometimes quwstioners need a bit more elucidation.  I suspect an explicit example of environment variable expansion using WSHShell.ExpandEnvironmentStrings() would resolve the issue.
0
 
T_HermanCommented:
You asked why your code wouldn't work.  I answered that.  The probelm was that VBScript doesn't understand environment variables directly.  I gave you code to fix that problem and you discovered a new problem.  The code I gave you works.. perfectly.  I can not diagnose your code further because I do not have your system.  If you were getting an access denied copying the file it's because the file was being used at the time, or you didn't have access to it.  Which is A)  what I said and B) beyond the scope of your question.

Then you vanished.  You didn't bother asking or expanding on anything else.  Logical assumption, you figured out why the file wasn't being copied correctly.

Childish?  Not at all.  Why should I and my friends waste our times helping people that don't really care?  Had you had the curtisy to just explain to me you were still having issues I could have had you explain more about the system and we could have determined why you weren't able to access the file.  Instead, like a child, you ran and hid and quietly requested the admins to answer the question.

I no longer want your points.  Not worth it.

Cookre, yes.. I know.  I thought I was being detailed enough.  Maybe i'm not cut out for this.  Rude people just get on my nerves.
0
 
T_HermanCommented:
Let me back up even further.  You should have continued to inform me you were having problems.  Since you didn't say anything I figured you saw the problem with your initial code and the improper use of the CopyFile procedure.  I guess it's my fault for making the assumption but you could have said more.  For the record:


Const OverwriteExisting = True
Dim objShell

Set objShell = CreateObject("wscript.Shell")
strSystemRoot = objShell.ExpandEnvironmentStrings("%SystemRoot%")

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\file.txt" , strSystemRoot & "\temp\file.txt", OverwriteExisting

Will fix the issue.  Your question seemed to be more about the %SYSTEMROOT% part not working I figured you had already figured out the other problem.  I apologize for that assumption.  You also had a problem with your CopyFile routine call.  You have to give a source path and destination path.  You only gave "\temp" for the destination and it needed to be "\temp\file.txt", the full path.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now