Windows %CD% environment variable not being updated - windows

I have this simple batch file (test.bat):
echo %CD%
pause
No matter where I run this .bat file from (i.e. C:\some\dir\test.bat), my system tells me that I'm in C:\ . This is obviously neither the intended nor the desired behavior, and if I try the exact same script on a different machine, it behaves as expected, giving me current directory of the .bat file. I've been googling for hours now and haven't been able to find anything relevant to my issue. Perhaps I missed up a windows setting somewhere? I have no idea, anyone have any ideas?

Found the issue: there was an AutoRun registry entry that would change directory to C:\ whenever a shell was opened. facepalm

Related

How to make a batch script to execute from anywhere?

I would like to run my batch script from anywhere, for example:
C:\>test123
And that command line executes my batch script C:\Documents\test123.bat.
I tried to do it with environment variable path, but it doesn't seem to work.
Add your bat to PATH and make sure PATHEXT contains .BAT
How to add your program to the PATH env variable
Note, that the current working directory will be whereever you called it from.
If you want relative paths to where the bat is stored, use %~dp0
Please post the output of echo %PATHEXT% %PATH% and tell us the exact location of your bat file and more error info why it failed in your case?
Adding to C:\Windows\System32
Worked for Me.
System info:
Windows 10 with Admin access.

Scheduling automated scripts suite run from schedular

So I tried and tried but couldn't figure out this one for some reason.
how can I run a task from a desired directory instead of System32 directory where cmd.exe is.
so, when I schedule a task and try to run it ..
command prompt suppose to go to "c:\users\aaa\bbb\ccc" and then pass the argument.
Instead, It's starts at c:\Windows\System32 and fails.
Could anybody help me with this please?
I really appreciate it.
Thank you.
EDIT --
so, now I have a run.bat file with following content in it ...
C:\Users\aaa\bbb\ccc\dd (location to my testrunner.bat file)
testrunner.bat Scripts/all.suite website-address ie (command for the task I wanna perform)
net stop schedule (since window is poping up and going away way to fast, I added this to stop it (not working))
type run.bat
#echo off
cd C:\Users\aaa\bbb\ccc\dd
rem this will show all files in dir
rem is the file you're expecting listed?
dir
rem notice how you can make comments with a leading rem(ark)
#echo starting scripts\all.suite
rem you have to change this to have the full path using Windows X:\dir\dir conventions
c:\home\Scripts\all.suite website-address
#echo done running scripts\all.suite website-address
#echo shutting down
net stop schedule
So its still not clear exactly to me your goal. The reason I added the cd c:\... command is that will **C**hange **D**irectory to the path specified.
This is what you need so you can "run a task from a desired directory instead of System32".
Copy everything from the first #echo off to the last net stop and using notepad, paste it into a file, fix command names and paths website-urls, etc, then save that file to c:\temp\testrunner.bat.
Open a cmd.exe window and test that the script works. Just paste c:\temp\testrunner.bat on to cmd-line and hit enter. If that works, then made an entry in the scheduler to run c:\temp\testrunner.bat . I don't know the specifics of running a script for scheduler, so look for clues on the input screen. Is the an option to run 'now'?
If the .bat file doesn't work from the command-line, then you have to fix the file before you try running it in the scheduler. As your command Scripts/all.suite website-address is a little vague, you'll do better to post a new question asking for help to fix the .bat file and use a sample command that people will be able to use on their PCs at home.
IHTH.

Exporting Entire Registry To Relative Path

Using the following code
REGEDIT /E C:\output.reg
A registry dump file is created in C directory. It overwrites automatically when a file with the same name exists. When I try changing the output directory to relative path like this
REGEDIT /E output.reg
it wouldn't work.
No file is created but processing takes just as long as usually. Which code can export the whole registry using relative path?
Like requested, the .bat code including debug code:
ECHO %CD%
REGEDIT /E output.reg
PAUSE
Command line output:
C:\Users\Username\Desktop\New_folder>ECHO C:\Users\Username\Desktop\New_folder
C:\Users\Username\Desktop\New_folder
C:\Users\Username\Desktop\New_folder>REGEDIT /E output.reg
C:\Users\Username\Desktop\New_folder>PAUSE
Press any key to exit . . .
Folder before (and after) .bat file execution:
The syntax of the command given in your question is already fine. If you supply a relative path for the output file name then the file will be created relative to the current working directory. This is easily verified from an interactive command prompt.
Whatever is going wrong with your batch script, the problem is not as you have currently surmised. Some of the more obvious explanations include:
The working directory is not what you think it is. Verify it by adding debug code to your script to emit the working directory.
The file is locked for some reason and so regedit cannot write to the file.
You don't have sufficient rights to write to the working directory.
Update
Thanks a lot for your question update. I tried to re-create your scenario (I'm on Windows 7 but I doubt that matters) and indeed I find the same issue as you. The command works fine from an interactive console window, but not when executing in a batch script.
I've no idea why this is, but here a simple enough workaround is to supply a full path:
REGEDIT /E %cd%\output.reg

Making Any Command in CMD work in a .BAT as an .EXE

Redirecting CMD Commands To An EXE File
Long story short, basically I have the problem that every time I run BCDedit on the .BAT that I converted into an .EXE it never worked and I thought that the reason why it wasn't work was because it wasn't on Path but my Path was fine and even if it was on Path is set by default thanks to foxidrive.
Now my main problem is that is there a way I can convert it to an .EXE file with every single command working, is there a technique I could use to get BCDedit working as an .EXE file?
All of the commands do work it just BCDedit and I say if every single command would work so if somebody has a similar command problem like me they know how to figure it out, it works perfectly as batch but is there a way I can convert it and make BCDedit still work as an .EXE?
Please answer As Soon As Possible and if you want to take a look at my Path and Batch the visit the link at the top.
Thank you so much!
Compile this batch file and see if it works:
#echo off
bcdedit /?
pause
Then try this version:
#echo off
c:\Windows\System32\bcdedit.exe /?
pause
Just as the SysWow64 Redirection link you listed states. If you want a x86 application to call the 64-bit system directory you need to call the bcdedit application as follows.
%SystemRoot%\SysNative\bcdedit
This is because of Windows backwards compatibility measures, it is looking at C:\Windows\SysWOW64 instead of C:\Windows\System32. Another alternative would be to compile the script as both x86 and 64-bit, but then you could only use each on the respective systems.
Update for Comments
Even though the path is correct, an x86 application will execute commands or batch scripts against the C:\Windows\SysWOW64\cmd.exe which will cause the path redirection issue. Example to illustrate my point. On a 64-bit version of Windows, open C:\Windows\SysWOW64\cmd.exe and using Windows explorer create two unique files , one is C:\Windows\System32\unique_test64.txt and the other in C:\Windows\SysWOW64\unique_test86.txt. Now on the x86 command prompt enter if exist C:\Windows\System32\unique_test86.txt echo true. You will see that it is being redirected to SysWOW64 and will print true. And if you enter if exist C:\Windows\System32\unique_test64.txt echo true, nothing will be found.
Screenshot Example: http://i.imgur.com/K4kkI29.png

Windows Script Host cannot find the file on startup

I am having here a vbs-file which starts a batch script. The batch should run invisible, so i use this vbs-script:
WScript.CreateObject( "WScript.Shell" ).Run "loop.bat",0,0
The vbs is registered to the run-key in the registry-
Everytime on startup it tells me that he cannot find the specified file.
Maybe the Windows Script Host is starting to late for my vbs-script?
What should I do?
vbs with delayed start? This needs another loop, but my script shall be invisible.
vbs in the startupfolder? Better taking the registry. For other reasons I cannot use the startupfolder.
Thanks for your answers :)
I know it's old... But i thought I'd put this in in case anyone stumbles here.
if you add something to run, when it starts unless otherwise specified it's working directory is C:\Windows\System32 so if you point to a file, "mybat.bat" it will assume it's in c:\windows\system32\mybat.bat
that's why you need to specify the whole path in the vbs.
if you are in a bat file then add this to the top of your file:
PUSHD %~dp0
Martyn

Resources