How to change to directory to your homedrive?(C:\, D:\, etc..) - windows

I am writing this batch file which searches for specific a specific file. Now every time the code cd /D "%HOMEDRIVE%" is executed, it of course starts to search in that directory. But what I get is file not found.
I tried doing cd /D "%HOMEDRIVE% in the command line but it only replies where the cmd is run (e.g C:\Users\onlYUs)
How do I fix this? There is an environment variable named HOMEDRIVE whose value is C:. But it does not change to that directory. And by the way the reason why I needed that because if an instance that your homedrive is set to D: or E: it can still search for the file. Any help would be greatly appreciated!

You can't change to a directory without providing a path to a directory, and %HOMEDRIVE% only contains a drive letter. Without the backslash, it's the equivalent of typing C: at the command prompt, which only changes the drive.
You need to add the trailing path separator (backslash) to make it a directory path instead, because you're wanting to change to the root directory of that drive.
This does not work:
cd /D %HOMEDRIVE%
This does work (note the trailing backslash):
cd /D %HOMEDRIVE%\

An alternative way is pushd %HOMEDRIVE%\ which allows the batch to later popd back to the drive and directory that were initially current.

Related

To change drive and directory at the same time

Hi presently I am using command prompt frequently. Every time I need to access different folders of different drives so I need to change drive and directory at the same time For this I got a solution:
C:> CD /D D:\JAVA it's working fine. But why we are using "/D"
So please explain me why we are using /D.
CD /D ... changes the current drive in addition to changing folder.
Source CD Change Directory - Select a Folder (and drive)
Syntax
CD [/D] [drive:][path]
CD [..]
Key
/D : change the current DRIVE in addition to changing folder.
...
Changing the Current drive
Enter the drive letter followed by a colon.
C:> E:
E:>
To change drive and directory at the same time, use CD with the /D
switch.
C:> cd /D
E:\utils E:\utils\>
cd /? Will answer that for you.
The alternative to cd /d d:\java is d: then cd java. If you then changed to another drive (say C:) and ran dir d: you would still see the contents of the java directory. i.e. The OS remembers what directory each drive is currently looking at. Likewise you can just run C:>cd d:\otherDir c:>dir d: and see the contents of otherDir.
By using the command -
CD [/D] [drive:][path]
on cmd it means to jump to a different directory where /D simply means to jump to another directory. This command also gives freedom in using different drives unlike the cd [drive:][path] command.
By using the above command it is also possible to move to the same previous working directory in a specific Drive just by writing Drive name followed by a colon. For eg:
C:\Users>cd /D G:\FUN
G:\FUN>C:
C:\Users>

How to change directory to run .bat files from different drive?

I have some .bat files that I run from the local directory (e.g. C:\Users\pozna001). However, when I try and change the directory to a data drive using cd F:\nrcs_project to run the .bat files from a different location, I see that the command prompt does not recognize the cd command. How can I change the directory in the command prompt so that I can run these .bat files from a different drive (i.e. a data drive connected to a server)?
CD /D F:\nrcs_project
Use the /D switch to change current drive in addition to changing current directory for a drive.
If you need to go from a device to another (in your case from C:\ to F:\, you need to type F: before/after you entered your cd command, so it will go on the F device. Otherwise, you can use the /D parameter of the cd function.
So to sum up,
$C:\Folder> F:
$F:\>cd F:\whatever
$F:\whatever>...
or
$C:\Folder> cd F:\whatever
$C:\Folder> F:
$F:\whatever>...
or
$C:\Folder> cd /D F:\whatever
$F:\whatever>...
You first need to type F: to change to the F:\ drive, then you can use the CD command
Or you can use the /D switch to do it all in one shot:
CD /D F:\nrcs_project
the command will look like following:
F: && cd nrcs_project
Both solution above are correct.
The fastest is:
cd /d f:\nrcs_project
But you could change drive first, then the directory.
Use cd /? and it will show you all params.
you can also use:
pushd "F:\nrcs_project"
this will allow also to return to the previous direcotry with popd
Mind that it is a good practice to enclose paths with double quotes in case of special characters,spaces and so on..

Starting batch file command prompt from the directory location in which the batch file resides in

I am writing a batch file that uses some of the files within it's parent directory (lets say Folder1).
C:\User\Steve\Foder1\
Now I want to make the whole Folder_1 relocatable so that I can copy paste the folder anywhere on my/someone else's computer and run batch script.
D:\User\Random_guy\Folder1\
The question is how do I start batch file's command prompt to (D:\User\Random_guy\Folder1) it's parent directory without writing another batch script to do that.
Start your batch file with:
pushd %~dp0
That will set the current directory to be the folder containing the batch file. Then in the batch file, make sure all your paths are relative to the current directory.
However, if your batch file changes to other directories in the course of its execution and you would still like to be able to refer to the batch's home folder's contents without knowing the exact path, use the same %~dp0 as the path to the file(s) you want to use. For instance, a FileA from the same folder as the batch file would be addressed as
"%~dp0FileA"
Note the absence of a \ before the FileA. This is because the %~dp0 already includes a trailing \ and so the entire thing will evaluate to a correct path all right. (Although if you do put another backslash, like "%~dp0\FileA", it should work as well, because Windows usually disregards multiple consecutive backslashes when in the middle of a path.)
%CD% in a batch file gets the current directory/directory batch is being run in.
So - if I've got this right, you want to run
`C:\steve\folder1\yourbat.bat`
which should copy C:\steve\somefiles to D:\Random_guy and set the current directory to D:\Random_guy\folder1 ?
#ECHO OFF
SETLOCAL
SET subdir=folder1
SET destdir=%~1
IF NOT DEFINED destdir ECHO Require destination username&GOTO :EOF
SET destdir=%~1\%subdir%
:: Ensure destination exists
ECHO MD "%destdir%" 2>nul
SET "sourcefrom=%~dp0.."
ECHO COPY "%sourcefrom%\filestocopy" "%destdir%\..\"
ECHO CD /d "%destdir%"
Note that the MD, COPY and CD commands are merely ECHOed, you'd need to remove the ECHO keyword to execute them.
Run the batch with a parameter of D:\Random_guy

Changing the path of a batch file (.bat)

How can we change the path of a batch file (.bat) ?
I've got a feeling it is about changing the current working directory.
To do that, use the command cd to change current working directory.
#echo off
C:
cd C:\Windows\System32\
If you change both the current drive and the current directory you'll need the /D switch:
cd /D D:\Foo
as in adding a temporary extra path to %PATH%?
try:
set path=%PATH%;c:\folder
where c:\folder is what you want to add
if you want a permanent path set use a tool called "setx"
otherwise you could call something directly without using CD etc using something like "c:\folder\program.exe"
Firstly, make sure you change the drivename to.
then use cd command
c:
cd "c:\windows\"
cd /d d:\foo
or, if you want to jump back to the source directory
pushd d:\foo
with
popd
you switch back to the directory before the pushd
Are you talking about changing the path of a command within the batch file? If yes then pass the path as an argument.
Hope I understood your question.

How to execute programs in the same directory as the windows batch file?

I have in the same folder a .bat and a .exe file.
I couldn't call the .exe file from the .bat unless I put the full absolute path to it.
Is there a way to don't specify the path?
Try calling the .exe with %~dp0, like this: %~dp0MyProgram.exe.
%0 contains the full path to the called .bat file.
~dp says to get the drive and path, including trailing \.
I solved this by changing the working directory using pushd at the start of the script and restoring is at the end of the script using popd. This way you can always assume the working directory is the same as the location of the bat file.
pushd %~dp0
ProgramInSameFolderAsBat.exe
popd
As Stephen C said, to properly support paths with spaces we can use:
start "%~dp0" "myfile.exe"
or with arguments:
start "%~dp0" "myfile.exe" -my_arguments

Resources