batch windows cd not working when multiple cmd autoload running - windows

I'm starting with batch on windows. I want to set some alias and persist them and also set start configurations, etc. when I open the cmd.
I found that windows when start a new cmd instance looks for a regedit variable under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor path and get the value of AutoRun key.
That is working fine, I defined an alias.cmd file and set up all my DOSKEY.
Perfect! But now I want to add more files, like startup.cmd that change my start directory. For example E:\
I'm adding some images to reference my explanation.
The big problem is that when I run my startup.cmd and run the cd /d E:\ command nothing happens, but the file is read (tried with the #echo prompt).
What is wrong? If literally copy and paste cd /d E:\ after my terminal opens it works... Also, if I try in this cmd files use the alias just declared nothing works as well.
Edit:
I tried with different AutoRun type and ,;| delimiters and still not working.

Related

Batch | Why can't a script running in admin access other scripts? [duplicate]

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

Windows context menu run hidden xcopy

I am trying to add a new option to the context menu for folders in Windows. I have managed to add the option and specify its command as follows:
xcopy.exe "%0\*" "c:\Destination\" /EHY
This code is added to regedit.exe
I have a folder in the c: drive named Destination. I am trying to copy the folder that I right clicked to the Destination folder, without a command prompt window.
What is happening: xcopy is running and copying the content of the folder and in the foreground. Please help me with these two issues:
Run the xcopy command without showing a window.
Copy the folder to a new folder in Destination named after the copied folder.
Thank you.
The command that satisfies the two issues listed is at the very end. First, some notes of explanation.
When you add a shell command to the Windows Registry, you have several variables available to you (such as %1, %L, and %V). Now, you would like a new folder in Destination named after the copied folder. Parameter extensions (such as %~n1) can strip everything from the full path and give you the name of the directory leaf.
However, these are not available when using the shell command from the Windows Registry. The most straightforward way to get a plain directory name is to create a temporary batch script, run it, and delete the batch script afterwards.
The following will copy the selected directory as a sub-directory inside Destination:
cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & call "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This next part requires the use of a third-party utility.
The previous command will open a command window and leave it open as long as copying is in progress. To hide that window, use the tiny utility RunHiddenConsole
The following will copy the selected directory and hide the command window while copying:
"C:\Destination\RunHiddenConsole.exe" cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & "C:\Destination\RunHiddenConsole.exe" /w "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This could certainly be made more flexible and efficient, but the above command at least demonstrates the technique for accomplishing the task.

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

Any command by which i can go directly to the specify path via command line in window

Is there any solution or command by which i can directly go to that bin directory which i saw in the image. Like i set the full path in some variable and execute that variable in cmd and that set the path. Because i need to set that path after every 20 or 30 minute
put this line into a batchfile somewhere in your path (e.g. home.bat):
#cd /d "C:\Program Files\apache-tomcat-7.0.39\bin"
Whenever you type home, it will change your working directory.
cd "Program Files\apache-tomcat-7.0.39\bin"
would seem to work.
As would
pushd "Program Files\apache-tomcat-7.0.39\bin"
altough the latter uses a CMD internal stack but has the advantage that executing a POPD would return to the original directory.

CMD script that goes to a folder

I'm trying to make a CMD script that will do CD Desktop\Crunchyroll\pyscripts and enter it, then I want to be able to type a command. Is there a way I can make a script to start in the Desktop\Crunchyroll\pyscripts directory on my computer?
Put this into a blank text file called MyScript.CMD
Double click it.
#echo off
CMD /K CD /D "%userprofile%\Desktop\Crunchyroll\pyscripts"
To create a symbolic link on Windows, open Command Prompt (as an Administrator). Let's say your username is Jason. And the symlink we will name 'code'.
cd \Users\Jason
mklink /D code Desktop\Crunchyroll\pyscripts
cd code
dir
All your files in pyscripts will be listed. You basically have created a shortcut to your pyscripts.
Personally, I'd simply create a shortcut to cmd.exe and set Properties/shortcut/Startin to Desktop\Crunchyroll\pyscripts.
If you really want to jump to pyscripts from within an existing cmd instance, then create a textfile named 'pyscripts.bat' containing
#echo off
CD "%userprofile%\Desktop\Crunchyroll\pyscripts"
and store it somewhere in your PATH.
Then executing pyscripts from within a cmd instance will jump to the indicated directory and
....
PUSHD
call pyscripts
{do some python}
POPD
...
should allow you to execute python from that directory within a batch file.

Resources