Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy File Security Problems in windows 7

Status
Not open for further replies.

qamarzaman

Programmer
Nov 14, 2012
6
PK
Dear All,
I am using Windows 7, I want to copy file in "c:\windows" folder through VFP 9.

I am using this command.
COPY FILE testfile.txt TO GETENV("windir")+"\"

But Error show
" Cannot Create File c:\windows\ "

Kindly Help Me.

Thanks
Qamar
 
This is a protected system folder, you can't copy to it without process elevation, as in eg a setup. Why do you have he need to write into the OS system folder?

Bye, Olaf.
 
I agree with what Olaf said about it being a protected folder.

But, in any case, the syntax is wrong. You need to drop the final backslash. In other words, instead of this:

Code:
COPY FILE testfile.txt TO GETENV("windir")+"\"

do this:

Code:
COPY FILE testfile.txt TO GETENV("windir")

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I've seen some places where "windir" doesn't have the trailing backslash. Given that, it should be AddBS(Getenv("windir")), which covers either case.

Also, the TO needs a file name, not just a path.

In an earlier era, I had to debug someone else's code that did this:

Code:
COPY FILE foo.txt TO Getenv("temp")+ lcFilename

The bug report was "I'm getting files in my windows directory with names like "temp47699315.txt". The temp variable was missing the backslash, i.e. "C:\Windows\temp".

That said, DON'T DO THIS! [3eyes]
 
>TO needs a file name, not just a path.
No. It's not documented, but this works:

Code:
If !Directory("D:\temp")
   MkDir D:\temp
EndIf 
Copy File Home()+"BUILDER.APP" to d:\temp

The help says both Filename1 and Filename2 must contain file extensions, which indicates you also need a destination file name, true. But you can also use joker chars as ? and *, The help gives the example COPY FILE *.PRG TO *.BAK, which indicates some automatism on keeping the file name for the destination folder, if it's provided partially only, as in *.BAK and specifying a destination path only, the full source file name is taken for the copied file name.

What indeed causes an error even though D:\temp is not a secured folder is

Code:
Copy File Home()+"BUILDER.APP" to d:\temp\

What works again of course is

Code:
Copy File Home()+"BUILDER.APP" to d:\temp\builder.app

In the end I agree using ADDBS() is always a good thing, in this case you would rather need REMOVEBS, which doesn't exists. Instead you use Justpath, as Justpath removes a trailing backslash, but it also removes a folder name, if there is no trailung backslash. So you need to add an eventually missing backslash first, and then remove it: Justpath(Addbs(GetEnv("Windir"))), but you'd still not write there without being privileged, even as admin user, you need process elevation additional to being an admin, that's UAC security.

Bye, Olaf.
 
Oh, and in case you specify a destination folder only, and the destination folder does not exist, it is'n created, thee also is no error. Instead you get a file with that name, as any folder name also is a valid name for a file, just not having a file extension. This is where it also can get messy.

Bye, Olaf.
 
Yes, that's what I also said initially.

In more detail, if you eg copy redist.txt from HOME() (just as a sample file everyone has) by

Code:
Copy File Home()+"redist.txt" to JustPath(Addbs(GetEnv("windir")))

This will not error, but the builder.app will not be copied to %Windir%\redist.txt but you'll find it in C:\Users\yourwindowsaccountnamehere\AppData\Local\VirtualStore\Windows\redist.txt

The VirtualStore folder is, where all such redirected write access ends up, this is what UAC (user account control) does.

There are few things to put into C:\Windows\, even if you run a setup, this normally installs into C:\Program Files. Hardware drivers may end up in C:\Windows\ subdirectories. If you need to do anything, do it the appropriate way via a setup or hardware driver installation, whatever, but system folders are for the system only. Win.INI, Autoexce.bat etc are things from the past, I don't see why you would have the need to write into the OS system base folder.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top