Solved

Copy to SystemRoot using VBS

Posted on 2004-04-20
24
1,179 Views
Last Modified: 2007-12-19
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
Comment
Question by:gbarrientos
  • 10
  • 6
  • 3
  • +1
24 Comments
 
LVL 3

Expert Comment

by:diegojserrano
ID: 10873688
you are using vbs, don't you?
0
 
LVL 1

Expert Comment

by:T_Herman
ID: 10873850
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
 
LVL 1

Expert Comment

by:T_Herman
ID: 10873875
Hmmm... Was that correctly submitted as an answer or is everythign a comment until you accept it?
0
 
LVL 22

Expert Comment

by:cookre
ID: 10875237
Yup, the Answer button was removed a while back - too much misuse of it.
0
 
LVL 1

Expert Comment

by:T_Herman
ID: 10878206
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 10882596
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
 
LVL 1

Expert Comment

by:T_Herman
ID: 10882707
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 10882793
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 10883049
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
 
LVL 1

Expert Comment

by:T_Herman
ID: 10884377
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:T_Herman
ID: 10932360
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
 
LVL 1

Expert Comment

by:T_Herman
ID: 11068679
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
 
LVL 22

Expert Comment

by:cookre
ID: 11071457
T_Herman's resonses were complete and accurate
0
 
LVL 1

Expert Comment

by:T_Herman
ID: 11071503
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 11072025
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 11072073
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
 
LVL 9

Author Comment

by:gbarrientos
ID: 11072081
unnecessary*
0
 
LVL 22

Expert Comment

by:cookre
ID: 11073160
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
 
LVL 1

Expert Comment

by:T_Herman
ID: 11073241
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
 
LVL 1

Accepted Solution

by:
T_Herman earned 500 total points
ID: 11073339
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

16 Experts available now in Live!

Get 1:1 Help Now