Home / Microsoft / Microsoft DOS / Batch file copies to wrong folder on x64
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Batch file copies to wrong folder on x64  (Read 517 times)
Linux711
Topic Starter
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« on: January 26, 2012, 02:58:56 PM »

I have a batch file that I converted to exe. It has a line that copies a file like this:

copy filename.dat %systemroot%\system32

On windows 7 32-bit, it works fine, but on x64 it copies to the sysWOW64 folder for some reason even though I explicitly specify system32. It causes the file to not work because only system32 is in the path variable and sysWOW64 is not, so it can't be accessed from anywhere. I need to figure out how to force it to copy to system32 (i don't want to mess with the path). I can make it work by manually copying it with explorer, but I need the program to do it.
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

patio
Moderator
Genius



Thanked: 1069
Posts: 11,351

Experience: Beginner
OS: Windows 7


Maud' Dib

« Reply #1 on: January 26, 2012, 03:12:27 PM »

You will need to "mess with the path"...
IP logged

   
"
All generalizations are false, including this one.  "
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #2 on: January 26, 2012, 03:56:13 PM »

And check if you are using the 64 or 32 bit cmd.exe. (You did know there are two?) The 32 bit one redirects system32 to SYSWOW64  - read the Wiki about wow64 at http://en.wikipedia.org/wiki/WoW64
 
64 bit... %windir%\system32\cmd.exe
32 bit... %windir%\syswow64\cmd.exe

IP logged


Proud to be European
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,878

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #3 on: January 26, 2012, 04:41:26 PM »

http://bc-programming.com/blogs/2009/12/windows-x64-fileregistry-redirection/

Basically, your batch file is now being run always as a 32-bit program because the executable is 32-bit and thus it will always be susceptible to redirection. (so either don't convert to a exe, or write a bunch of workaround script to check what the system's bit width is).
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Linux711
Topic Starter
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #4 on: January 31, 2012, 12:14:26 PM »

Thanks BC. Your blog is very helpful. Here is what I wrote from the information on your blog. Will this work (I don't have a system with 7 64 to test it right now)?

Code: [Select]
if %PROCESSOR_ARCHITECTURE%==x86 (
copy file.dat %systemroot%\system32
) else (
copy file.dat %systemroot%\sysnative
)
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #5 on: January 31, 2012, 12:51:11 PM »

Am I missing something here? The OP has written a batch file and "compiled" it to make a 32 bit executable (well, a 32 bit wrapper which unwraps the batch and makes cmd.exe (on a 64 bit system, the 32 bit cmd.exe) run it. So he is living in a 32 bit world with a 32 bit sky, and 32 bit birds singing in 32 bit trees. In that 32 bit world, file system direction ensures that the right "system32" folder is "seen" by apps, birds, trees, etc.

In other words this line

Quote
copy filename.dat %systemroot%\system32

is executed correctly.

Why is there any need to penetrate the 64 bit system?

 
IP logged


Proud to be European
Linux711
Topic Starter
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #6 on: January 31, 2012, 12:55:13 PM »

Because on the x64 system, this line. . .

copy filename.dat %systemroot%\system32

Copies to sysWOW64 and not the real system32 where I need it to.
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #7 on: January 31, 2012, 12:59:51 PM »

Quote
Because on the x64 system, this line. . .

copy filename.dat %systemroot%\system32

Copies to sysWOW64

Yes, I know it does!

not the real system32 where I need it to.

Why do you "need" it to?
IP logged


Proud to be European
Linux711
Topic Starter
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #8 on: January 31, 2012, 01:21:39 PM »

Because the PATH environment variable only points to system32 and not sysWOW32, so when I enter it into command prompt from anywhere, it doesn't work if it's in sysWOW64. Plus my program is designed to operate from system32, not anywhere else. At this point, you probably want to know what it's for. It is a program that adds an item to the right-click menu of any file. It allows you to delete any file even if it's protected by trusted installer. That's one of the things in 7 that makes my angry because I like to do a lot of system modding and I can't when everything is blocked from deletion by the admin.
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #9 on: January 31, 2012, 02:37:49 PM »


if /i "%programfiles%"=="%programfiles(x86)%" (
    REM This is a 32 bit command prompt
    %systemroot%\sysnative\cmd.exe /c copy filename.dat %systemroot%\system32
) else (
    REM This is a 64 bit command prompt
    copy filename.dat %systemroot%\system32
)


IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #10 on: January 31, 2012, 03:47:56 PM »

Updated comments to be a bit clearer


if /i "%programfiles%"=="%programfiles(x86)%" (
    REM On a 64 bit system this is a 32 bit command prompt
    %systemroot%\sysnative\cmd.exe /c copy filename.dat %systemroot%\system32
) else (
    REM On a 64 bit system this is a 64 bit command prompt
    REM On a 32 bit system this is the only command prompt
    copy filename.dat %systemroot%\system32
)

IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #11 on: February 01, 2012, 12:11:13 AM »

Updated comments to be a bit clearer (again)


if /i "%programfiles%"=="%programfiles(x86)%" (
    REM On a 64 bit system this is a 32 bit command prompt
    REM So invoke the 64 bit cmd.exe to do the copy
    %systemroot%\sysnative\cmd.exe /c copy filename.dat %systemroot%\system32
) else (
    REM On a 64 bit system this is a 64 bit command prompt
    REM On a 32 bit system this is the only command prompt
    copy filename.dat %systemroot%\system32
)

IP logged


Proud to be European
Linux711
Topic Starter
Adviser



Thanked: 49
Posts: 921

Certifications: List
Computer: Specs
Experience: Experienced
OS: Windows XP

Programming Blog 1
« Reply #12 on: February 01, 2012, 10:11:24 AM »

Thanks for the help. Can't try it right now bc I'm not near my x64 comp. I tested my code and it didn't work so I'm hoping yours will. If it doesn't, I am just going to change my whole program to run in the windows folder instead of system32. I think that will work.
IP logged

YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #13 on: February 01, 2012, 12:02:40 PM »

I tested my code and it didn't work so I'm hoping yours will.



Using exactly the same batch, on:

my 64 bit Windows 7 system the results were

32 bit command prompt: file was copied to C:\Windows\System32 (not C:\Windows\SysWOW64)
64 bit command prompt: likewise

On my 32 bit Windows XP system the result was the file was copied to C:\Windows\System32




« Last Edit: February 01, 2012, 12:30:32 PM by Salmon Trout » IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #14 on: February 02, 2012, 12:28:37 AM »

My suggested solution exploits the following:

In a 32 bit command environment the environment variable %programfiles% has the same value as %programfiles(x86)%. In a 64 bit environment it does not. WOW64 recognizes Sysnative as a special alias used to indicate that the file system should not redirect the access. Thus a 32 bit command prompt can select the 64 bit version of cmd. exe. Note that 64-bit applications cannot use the Sysnative alias as it is a virtual directory not a real one, and is not seen by 64 bit programs.

IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #15 on: February 02, 2012, 11:11:24 AM »

Linux711, do you realise that in a 64 bit version of Windows, the "system32" folder in the PATH, that 32 bit programs see, is really SysWOW64?
IP logged


Proud to be European
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Batch file copies to wrong folder on x64 « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.112 seconds with 19 queries.