# Batch file redirect problem. (Moved from Vista/7)



## Mid_Life_Crisis (Jan 15, 2010)

I have an odd problem involving security and permissions in Win7 Pro on a Domain. 
I have some batch files that I use for repairing/updating the company software on customer computers. I have run into a problem at some sites because you have to "Run as Administrator" on Vista and Win7 machines or they will fail. I want to keep these batches simple so other techs can use them and so I can e-mail a zip to customers who are not always very savvy. So I need them to run from a desktop folder. When you "Run as Administrator" the working directory changes to the system32 folder and file transfers fail because the "file not found". I am pretty sure I have figured out a workaround, but I cannot test it because my computer, even though the Title bar shows system32, does not change the working directory. The files are found and everything goes without a hitch with no workaround needed. I posted on a Dos site, but the rather Dos knowledgeable fellow who tried to help me appears to be stumped as to why my computer behaves differently than his and my customers'.
Anybody have any ideas?


----------



## Squashman (Apr 14, 2005)

Can you post the batch file here.


----------



## Mid_Life_Crisis (Jan 15, 2010)

Squashman said:


> Can you post the batch file here.


This is a sample. I have several, some more involved, but this is a pretty basic one that has the problem.


```
Set TARGET_DIR=system32
if exist "c:\windows\syswow64" Set TARGET_DIR=syswow64

xcopy mscomct2.new  c:\windows\%TARGET_DIR% /i /d /y

regsvr32 /u "c:\windows\%TARGET_DIR%\mscomct2.ocx"

cd c:\windows\%TARGET_DIR%

rename mscomct2.ocx  mscomct2.old
rename mscomct2.new mscomct2.ocx

regsvr32 "c:\windows\%TARGET_DIR%\mscomct2.ocx"
```


----------



## Squashman (Apr 14, 2005)

Use the PUSHD command at the top of your batch file. Assuming your batch file and all it associated files are sitting in a folder on your desktop.


```
pushd "%~0"
```


----------



## Mid_Life_Crisis (Jan 15, 2010)

Squashman said:


> Use the PUSHD command at the top of your batch file. Assuming your batch file and all it associated files are sitting in a folder on your desktop.
> 
> 
> ```
> ...


Thank you for the reply.
I tried this and it returns an error that the path is invalid and the path it shows includes the batch file.
Can you clarify the "%~0" ?


----------



## Squashman (Apr 14, 2005)

%0 by itself is the name of the batch file.

I missed a character with that modifier.
Try: pushd "%~f0"

That gives you the fully qualified path name of the batch file.


----------



## Excabus (Nov 3, 2010)

I've seen it before where for some odd reason my CMD console starts with it's working directory in Z:\ or some oddball drive that I can only imagine exists as some sort of system function, anyways, usaully when I try to CD out of it, it doesn't go. I usaully have to implicitly tell it to go to the C: drive via, "CD /D". I am on Windows 7 Ultimate x64, so no idea if that makes a difference or not. Perhaps appending that /D after CD will alleviate issues? That is if your experiencing funky starting working directory issues like I have.


----------



## Mid_Life_Crisis (Jan 15, 2010)

I'm testing with this bit of code and the first line continually returns the entire path, including the batch file name along with an error that the directory name is invalid:


```
pushd "%~f0"

rem if batch path equals working path

if /i "%~dp0"=="%cd%\" goto same

  echo not same directory

:same
  echo same directory

pause
```


----------



## Squashman (Apr 14, 2005)

Weird.
The current directory variable shows as the desktop but the batch file is opening to the system directory. So if the current directory is where the batch file is, you would think it would run correctly.


----------



## Squashman (Apr 14, 2005)

This might come in handy but I am not sure how yet.
%CMDCMDLINE% Returns the exact command line used to start the current Cmd.exe.


----------



## AlbertMC2 (Jul 15, 2010)

Hi

If you haven't solved this yet, you can try:


```
echo "%~dp0"
cd /D "%~dp0"

rem if batch path equals working path

if /i "%~dp0"=="%cd%\" goto same

  echo not same directory

:same
  echo same directory

pause
```


----------



## jcgriff2 (Sep 30, 2007)

Mid_Life_Crisis said:


> So I need them to run from a desktop folder. When you "Run as Administrator" the working directory changes to the system32 folder and file transfers fail because the "file not found". .... even though the Title bar shows system32, does not change the working directory.


In Vista & Windows 7, when running a batch file at elevated Admin level, the current directory will be *%windir%\system32*.

I ran batch script *1.bat /B] from c:\users . . .

Running at elevated Admin level - 


Code:


[FONT=Lucida Console]echo "%~dp0"  =  "c:\users\"
echo  %cd%    =   [color=red]c:\windows\system32[/color]
echo "%~f0"   =  "c:\users\1.bat"
echo "%~dp0"  =  "c:\users\"[/FONT]


Running non-elevated -


Code:


[FONT=Lucida Console]echo "%~dp0"  =  "c:\users\"
echo  %cd%    =   [color=red]c:\users[/color]
echo "%~f0"   =  "c:\users\1.bat"
echo "%~dp0"  =  "c:\users\"[/FONT]

Are you by any chance running the batch script from an x86 app (like a file manager) on an x64 system?

Regards. . . 

jcgriff2

`*


----------



## Mid_Life_Crisis (Jan 15, 2010)

jcgriff2 said:


> Are you by any chance running the batch script from an x86 app (like a file manager) on an x64 system?
> 
> Regards. . .
> 
> ...


I place a folder on the desktop, go into the folder, which is done with Windows Explorer, I assume, and right click on the batch file. 
It is a Win7 64 bit system.


----------



## AlbertMC2 (Jul 15, 2010)

Hi

I ran the script mentioned in Post 11 on a Windows XP, Windows 7 32bit and 64bit as administrator all from the desktop - and they all worked. 
Both Windows 7 computers have UAC enabled - I haven't tried with UAC disabled.


----------



## Mid_Life_Crisis (Jan 15, 2010)

I didn't consider if UAC might be the key. My computer has it disabled and does not change directories. I'll need to enable it and see if there is a difference.
Good thought.


----------



## jcgriff2 (Sep 30, 2007)

UAC enabled here - Windows 7 x64 - https://www.techsupportforum.com/posts/3398397/

Since elevation necessary, consider compiling; check box "Add Vista Administrator Manifest" - 

http://www.f2ko.de/programs.php?lang=en&pid=b2e

Regards. . .

jcgriff2

`


----------

