Link to home
Start Free TrialLog in
Avatar of Samuel-AMC
Samuel-AMC

asked on

Batch not able to use COPY or XCOPY on Windows 8

Hi everyone,

I have no problem using this command on Windows XP, Vista and 7, but don't know
whats up with Windows 8, that even if I right-click on the batch and run it as admin
nothing happend,

I'm trying to copy some files to one place to another. for example:
COPY "C:\Image.png" "C:\Images\Image.png"
I also tried XCOPY to copy the whole folder after seeing that COPY didn't work.
Avatar of Bill Prew
Bill Prew

So what error or other messages are displayed when you run it?  Do you have update permissions to the C:\images folder?  Does it already exist?

At a command prompt, a couple of other things to try.

copy c:\image.png NUL

copy c:\image.png "%TEMP%"

echo.>c:\images\test.txt

Let me know what they display.

~bp
Avatar of Samuel-AMC

ASKER

Hi Bill,

Bill, I have tried everything.
Let me explain, first if I normally run the batch just like that, I get an access denied,
then if I right-click on the batch and run it as an administrator I don't get the same
message, the access denied, but I get another message that point to: the path cannot
be found, but the path does exist, cuz the folder or directorie is there, I manually
created cuz mkdir is also not working.
Is the folder actually on the C: drive, or a mapped network drive?

~bp
No bill, let me explain, I always to everything from a pen drive, and this is how
I do it, suppose the pen drive is in drive E, F, eg, tc,
Then my batch is something like this:

COPY "\Images\Image.png" "C:\Windows\Images\Image.png"
and always work
Well, if you are writing to the c:\windows folder then you will have to Run As Administrator for that to work.  Perhaps it can't see the pen drive.

So, with the pen drive installed, if you do the following, does the DIR work, or fail (using the drive letter the pen drive was assigned when you plugged it in)?

Start <click>
All Programs <click>
Accessories <click>
Command Prompt <right click, Run As Administrator>
DIR F:\ <in the command prompt, using your drive letter>


~bp
Bill, even if i drag and drop the content of the folder with the images from the
pen-drive to the desktop I still have the issue, thats how bad this situation is.
So I don't think the issue here is the pen-drive
Bill, even if i drag and drop the content of the folder with the images from the
pen-drive to the desktop I still have the issue, thats how bad this situation is.
What error does that produce?

~bp
Hey Bill I tried one more time and this time I was able to copy the files
from the desktop to the windows directorie as an administrator.
Sounds like a permissions problem of some sort.

~bp
Yeah, so this means that I might have to first copy the files from the pen-drive
to the desktop and then let the batch script to run the commands from there.
Do yourself a favor don't copy to the windows folder, same with the c:\, c:\program files, c:\program files (x86) folders.  These are protected folders and only trusted installer has full control of these folders. By forcing the changing of permissions to give any 'user' even 'administrator' you are breaking the security model of operating systems >6.0 (vista/7/8)
If you run it from the pen drive and run-as-administrator then i think that will set the path to c:\windows\system32 rather then the current directory of the batch file so it doens't know the specific path because it is looking on the wrong drive IMHO.

Would say put a specific path in there if you need to run it as admin or try using

@echo off
echo drive is now %cd%
cd /d "%~dp0"
echo drive is now %cd%

Steve
to amplify the previous comment use fixed paths rather than relative paths
copy f:\image.png c:\images\image.png /y
(the /y is a default for the question do you want to over-write (y = yes) /n = no)
Sorry if not clear.  As an example this is above batch ran from d:\somedir

drive is now C:\Windows\system32
drive is now D:\somedir
Press any key to continue . . .

i.e. default dir when run as admin is c:\windows\system32
when it does the cd command into the drive and directory based on the batch file filename (%0) then the default path is that dir.

You can also user %~d0 to just get the D: part of this which you can combine with the full path so you know which drive it is running from when the drive gets a different letter.

Steve
Hello friends, sorry for the long delay,
But I had some issues at the moment I was replying to Bill. Thanks for all the help.

Now... ve3ofa, let me explain, I use batch a lot, you have no idea...
I turn on my computer and I start working with batch and or using batch.
So I always use batch with Windows XP, Vista, 7, and so far had no problems.
Now the only OS that I just recently are having problems with admin rights is Windows 8

Steve, let me tell you the way I've been doing it and always work for me on XP, Vista & 7.
for example, let's assume that my pen-drive letter is F:\  and I have an image there which
I want to copy to "C:\" so the images name is: Samuel.png  example: F:\Samuel.png
In conclution in my batch file I never point to any drive letter, all I do is this in my batch:
COPY "\Samuel.png" "C:\Samuel.png" >NUL and believe it or not but never had problems
in Windows XP, Vista, and also Windows 7.
Now let me explain how all of this started...
My batch file create two folders before my batch start ,

Sometimes is this path:
C:\Program Files\Folder

And some other times is this path:
mkdir "C:\Program Files (x86)\Folder

To accomplish this task I use this script:
http://support.microsoft.com/kb/556009

Then there is another folder my batch create, which is in C:\WINDOWS\Images
understood... but if you run-as administrator then the current directory will be system32 dir by default, not the batch file drive, which is why i suggested checking with the %cd% variables.

i havent checked this on 8 yet with a physical usb drive as I've only running  vm's of win8 here at the mo. but it seemed to be doing the same.  using a relative path from the usb then fails as it is relative to system32 not f: or whatever.

quite possibly not your problem here, and will try on an 8 machine later but worth checking!
I seem to have the same effect on Win8 machine, i.e. running as admin and current dir is c:\windows\system32

Steve
I'm lost, so what does this means?
I have to use c:\windows\system32 ?
If yes, how? could you explain Steve?
Thank you guys
No, we can adjust the BAT script on the external drive to work properly, can you post the code so we can suggest the needed changes.

~bp
One suggestion guys, all the time I use batch to install programs, and on some
programs or tools that I made myself, I copy all the contents of these programs
some where in windows, but now with windows 8 things are not working fine.

This is what I tell my batch to do for me:

1.) My batch create a dir for the program and other files.
2.) My batch copy all the files of the program to that folder.
3.) My batch with a .vbs file create a shortcut in the desktop for that program.
4.) Thats it, this works just fine for me on XP, Vista, and windows 7, but not on 8

Now the suggestion I ask for, is for this, should I use an installer program instead?
But the thing is that I am not use to these installer programs, what do you guys think?
Thanks
Post the actual BAT file please.

~bp
Okay Bill, let me post it...
If you do want to explore a free installation tool, take a look at:

http://www.jrsoftware.org/isinfo.php

~bp
Without seeing your script, and building off Steve's earlier comment, if you currently have something like:

copy "myfile.txt" "c:\somefolder"

try this approach:

set MyDir=%~dp0
copy "%MyDir%myfile.txt" "c:\somefolder"


Or, alternately put the following at the top of your script:

pushd %~dp0


and then before you exit do:

popd

~bp
This is the batch file I tried with windows 8:
@ECHO OFF
Echo.

Mkdir "C:\WINDOWS\Images" 2>NUL
XCOPY "\Images" "C:\WINDOWS\Images" /D /E /C /R /I /K /Y /F 2>NUL
COPY "\Images\My Program.ico" "C:\WINDOWS\Images\My Program.ico" 2>NUL
Call "\Images\Shorcut.vbs"

Open in new window


This is the .vbs file that makes the shorcut for my program:
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\My Program.lnk" )
oShellLink.TargetPath = "C:\WINDOWS\Images\My Program.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "C:\WINDOWS\Images\My Program.ico"
oShellLink.Description = "My Program"
oShellLink.WorkingDirectory = "C:\WINDOWS\Images"
oShellLink.Save

Open in new window


This file have to be located in my pen-drive where my program is,
Then I invoke this file from my batch file like this: Call "\Images\Shorcut.vbs"
Bill, the problem I see with my batch on windows 8
is that it fail with: MkDir, XCOPY, COPY, well everything
I get an "ACCESS DENIED" error
Well the most common errors I get with my batch on windows 8 are these:
"Access Denied" and also "The file path could not be found on the system".
Well Bill, I think you've seen my code by now,
Now let me see if I understood your example.

assuming my pen-drive is letter F:\ but like I said earlier, I always ignore the F:\
and I just go like this: COPY "\Images\EE.png" "C:\WINDOWS\Images\EE.png" >NUL
Now with your code would be something like this? ...correct me if I'm wrong.

Set Images=%~dp0
COPY "%Images%EE.png" "C:\WINDOWS\Images\EE.png"
popd

Is this how the code should look like?
Thanks Bill
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, looking at your existing scripts, these adjustments should help.  They will get rid of the "path not found" errors, but will not solve the "access denied" problem.  For that you will either have to run the scripts in elevated privledges (admin) or use a different destination directory to install into (I would suggest the latter).

@ECHO OFF
Echo.
set MyDir=%~dp0
set DestDir=C:\WINDOWS\Images
Mkdir "%DestDir%" 2>NUL
XCOPY "%MyDir%Images" "%DestDir%" /D /E /C /R /I /K /Y /F 2>NUL
COPY "%MyDir%Images\My Program.ico" "%DestDir%" 2>NUL
Call "%MyDir%Images\Shorcut.vbs" "%DestDir%"

Open in new window

strDestDir = Wscript.Arguments(0)
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\My Program.lnk" )
oShellLink.TargetPath = strDestDir & "My Program.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = strDestDir & "My Program.ico"
oShellLink.Description = "My Program"
oShellLink.WorkingDirectory = strDestDir
oShellLink.Save

Open in new window

~bp
Hey guys do not think I've forgotten the question, I came to tell you how things are going, and this is what is happening so far... it seems that I have no problem using my batch code with upgrades of windows 8, I meant, if I upgrade from 7 to 8 there is no problem at all, but the one's I have problem running this code is with laptops that already come with windows 8. and right now I don't have one at the moment to run Bill example.
Bad news, Bill code does not work, it doesn't even create the dir

set MyDir=%~dp0
set DestDir=C:\WINDOWS\Images
Mkdir "%DestDir%" 2>NUL

After trying anf trying, I end up just trying this only part of the code and it doesn't work.
Are you running it as Administrator?

If so, what error(s) are displayed?

~bp
Hey Bill, it does work if I run the batch as admin, but the only thing that does
or create is the dir, but it does not copy anything to the new destination folder.
the other problem or issue is that it doesn't output an error or it does not show
no error messages at all. what I did is add a Pause at the end like I always do, but
it does not show nothing.

And I would like to remove something I said before, I tought that I don't have this
issue when I upgrade from 7 to 8, but I was wrong, the issue still there on all 8's.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bill still nothing...
I also tried adding an slash:

XCOPY "%MyDir%\Images" "%DestDir%" /D /E /C /R /I /K /Y /F
COPY "%MyDir%\Images\My Program.ico" "%DestDir%"

But still the same
@echo off
set drive=%~dp0
set drivep=%drive%
if #%drive:~-1%# == #\# set drivep=%drive:~0,-1%
set DestDir=%drivep%\Images
echo %DestDir%
Mkdir "%DestDir%" 2>NUL

Open in new window

video http://screencast.com/t/XRGexk8IHd5

set DestDir=C:\WINDOWS\Images  (windows is a protected directory cannot write here)
If I could not write there, what other path could I write to?
Hey ve3ofa, that was you in that video?
Yes, It was my video
Protected Areas plus their subdirectories
c:\program files\
c:\program files (x86)
c:\windows
If I run Bill example as admin I have no problem creating the dir, but not able to copy files.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry guys, but is not that I forgot about this question, but anyway, I just wanted to say
that Bill did a great job with his code even if the code is not working with what am doing.
you too dragon, and also ve3ofa, thank you for trying to help me. I will acept this answer,
but will re-open another question base on this question as right now I can't finish the
project. thank you very much guys.