CMD Copy function not working!

Hi,

I have a command that works fine in command prompt but not when ran from a batch file.

copy G:\DO_NOT_uSE\electRa_M3.lnk "%userprofile%\Desktop"

As I said, works fine in CMD and says 'copied 1 file successfully '
however when I run it from a batch file, nothing happens? I have tried variations with xcopy aswell.

I also run this from a batch file : xcopy g:\win "C:\win" /I /E /H
which works fine! im confused, can someone help please.

Reece
LVL 2
peggiegregAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
Did you try using quotes for all paths?

copy "G:\DO_NOT_uSE\electRa_M3.lnk" "%userprofile%\Desktop"

Open in new window


Also, did you try running it as administrator?
TemodyPickalbatros, IT ManagerCommented:
all you need Just add line like
copy "G:\DO_NOT_uSE\electRa_M3.lnk" "%userprofile%\Desktop"    (all with '' '')

no administration privilege require
wakatashiCommented:
How are you running the batch file?  Just by double-clicking it when logged on as the user, or as a Scheduled Task, a server logon script, or something else?  

The problem is almost certainly related to what the %userprofile% variable gets expanded to.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

peggiegregAuthor Commented:
still no luck, I tried it with quotes (just copied exactly what you put into it) and it didn't work in the .bat file. however it works fine in CMD!?

I then tried the same command like this:  copy "G:\DO_NOT_uSE\electRa_M3.lnk" "C:\"
that worked fine in the batch file.

so the command works
the batch file works when going to C:\
but it doesn't work when your running as a .bat to the desktop.
Zephyr ICTCloud ArchitectCommented:
I also think it might have something to do with the %userprofile% ... What does display when you run "echo %userprofile" from a bat file?? Put a pause in the file to keep it open after the command...

As an alternative you might be able to do something like this, maybe... :

@echo off
: start
copy "G:\DO_NOT_uSE\electRa_M3.lnk" "%systemdrive%/documents and settings/%username%/desktop"

Open in new window

joinaunionCommented:
That's because when you use percent value it only shows the contents of that folder. So the command is incorrect.

You will have more success doing it like this,
copy G:\DO_NOT_uSE\electRa_M3.lnk  C:\Users\username\Desktop

Replace username with what the name of the computer is. It also may be user instead of users.


To explain percent. If you click start then in the search type in %userprofile% is a call function to display contents of the user not a copy function.Same with %systemdrive%
peggiegregAuthor Commented:
Oh right well I will try that in a little bit and let you know how I get on. The reason I didn't manually enter the users path is because this script is designed for multiple users to get this icon. So the user will change. But this is what confuses me. If the % stuff doesn't work then why does it work fine in CMD? What does a batch file do different to executing a command in command prompt?
NVITEnd-user supportCommented:
In the .bat file, add this as the first line:
>> "%temp%\debug_bat.txt" echo "%userprofile%\Desktop"

Run the .bat then examine "%temp%\debug_bat.txt".
What does "%userprofile%\Desktop" show as?
NVITEnd-user supportCommented:
Also, as Wakatashi said, how are you running the .bat? The answer can affect you for good or bad.
peggiegregAuthor Commented:
I added that code into my .bat file and the debug.bat.txt came up with the following:

"C:\Users\Reecew\Desktop"
"C:\Users\Reecew\Desktop"
"C:\Users\Reecew\Desktop"
"C:\Users\Reecew\Desktop"
"C:\Users\Reecew\Desktop"
"c:\"

And im running the .bat by double clicking on it, right clicking and running as admin, double clicking from c:\
wasn't aware that you could run it in an alternative manor?

thanks for all the help so far guys
NVITEnd-user supportCommented:
@peggiegreg

> "C:\Users\Reecew\Desktop"
The paths in debug.bat.txt seems to be fine, assuming you're the user logged on user Reecew double-clicking and running .bat file. As you mentioned, that's what you want it to do, i.e. "...script is designed for multiple users". Just had to make sure %userprofile% is working as expected.

> wasn't aware that you could run it in an alternative manor?
If you're just running it via double-click, then that's fine. When running via scheduled task, other factors come into play. We had to make sure.

At this point, I can't say why a simple COPY command doesn't work in the .bat:
copy G:\DO_NOT_uSE\electRa_M3.lnk "%userprofile%\Desktop"

Open in new window


When run via the .bat, does the COPY command throw an error? If so, what? If you have @ECHO OFF at the top of your .bat, try removing it temporarily. Then run the .bat and look for errors.
peggiegregAuthor Commented:
@ NewVillageIT (NVIT)2015

it is beyond me why it doesn't work. so I changed the .bat to the following:

____________________________________________________________________________________
copy "G:\DO_NOT_uSE\electRa_M3.lnk" "%userprofile%\Desktop"

Pause
____________________________________________________________________________________

turns out it says :

'\\svr01\Userdata\Reecew\Desktop\New PC Install'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

C:\Windows>copy "G:\DO_NOT_uSE\electRa_M3.lnk" "C:\Users\Reecew\Desktop"
        1 file(s) copied.

C:\Windows>Pause
Press any key to continue . . .


Which to me implies It thinks its copied across fine? I might just accept this wont work and move on :(
NVITEnd-user supportCommented:
> 1 file(s) copied
Looks like it worked... but the file isn't there? On the desktop?
peggiegregAuthor Commented:
the file is 100% not there. what's annoying is that I didn't set these PC's or domain up. so I can only assume its the PC's at fault. it should work. the logic is there, so I am extremely confused!

could use team viewer if you wanted to take a look yourself!
joinaunionCommented:
After reading and re reading it appears UNC paths are not supported.
Read caspers post here to use the pushd command.
http://www.tomshardware.com/answers/id-1623383/desktop-shortcut-unc-path.html

Definition of pushd
https://technet.microsoft.com/en-us/library/bb490978.aspx
http://ss64.com/nt/pushd.html

Post back results.
peggiegregAuthor Commented:
Hi Joinaunion,

I don't think I understand what it is asking me to do, I tried a couple things and none worked so I don't know if I am doing them wrong. what should I type into the batch file?

sorry if I am being a melon
NVITEnd-user supportCommented:
Peggie...

'\\svr01\Userdata\Reecew\Desktop\New PC Install'
 CMD.EXE was started with the above path as the current directory.
 UNC paths are not supported.  Defaulting to Windows directory.

1. Open a CMD prompt
2. Type:
C:\WINDOWS\system32\cmd.exe /k c:\folder\batfilename.bat

Open in new window


Of course, adjust folder\batfilename.bat per your needs.

Result?
NVITEnd-user supportCommented:
I can only assume its the PC's at fault
Do other PC's work fine?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peggiegregAuthor Commented:
still no luck, I cant invest enough time into this. I am going to accept the answers which I believe could have led to the solution with the time invested
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 7

From novice to tech pro — start learning today.