Command Prompt Script command "cd" - windows

Ok so I was making my own little command prompt for my own personal use and I have been fighting to make it work for the past 2 hours. Here is what I have done:
#echo off
set /p labnum="Enter Lab Numnber:"
set labdir=C:\Users\BLAHBLAHBLAH\Dir\Lab-
set labdir2="%labdir%%labnum%"
cd labdir2
:cmd
set /p cmd=">"
%cmd%
cls
goto cmd
I basically want to be able to change the path before each "session" But every time the cd labdir2 command is executed, my computer whines, "The system cannot find the path specified." And I know FOR SURE that the directory exists! I have pasted the text straight from windows explorer. Any and all help is appreciated.
Thank you!

The error is here:
cd labdir2
that changes to a directory called labdir2, but you want to change to a directory indicated by the contents of the variable:
cd %labdir2%
to make sure you can cope with special characters I'd enclose it with double quotes:
cd "%labdir2%"
You might even want to include the /d swith with the cd command to make sure that you also change the current drive. So the final version should be:
cd /d "%labdir2%"

Related

Is there a way to change folder from batch and stay there after execution?

I want to run a batch file from the terminal, once this batch file end its execution I want the terminal to be in the current directory of the batch file, any idea?
I want to execute a batch file and get an output like this:
C:\Temp> batch_file
Moving prompt to another route
C:\Folder1\Files>
Answer completely changed after getting a better understanding of the problem.
I understand that the request is
Command is invoked from a terminal starting in some directory such as c:\temp
The batch file blarg.bat is executed
blarg.bat does stuff in a differrent directory, such as c:\temp\a\b\c
The desire is for the terminal to now be in the c:\temp\a\b\c when the script exits
I was surprised by what impacts this....
I've been taught to always wrap the script in SETLOCAL/ENDLOCAL to keep variables localized, and it can allow somewhat procedural type calling.
So I started with this:
#ECHO OFF
SETLOCAL
ECHO Starting at %CD%
CD "c:\temp\a\b\c"
ECHO Now at %CD%
ECHO:
ECHO This is where other stuff would be done
ECHO:
(ENDLOCAL
EXIT /B 0)
with the expectation that this would end with the calling terminal in "C:\temp\a\b\c". But it didn't.
So I removed the setlocal/endlocal wrapping.
#ECHO OFF
ECHO Starting at %CD%
CD "c:\temp\a\b\c"
ECHO Now at %CD%
ECHO:
ECHO This is where other stuff would be done
ECHO:
And now it gives the desired results
c:\TEMP>blarg.bat
Starting at c:\TEMP
Now at c:\TEMP\a\b\c
This is where other stuff would be done
c:\TEMP\a\b\c>
So it appears that as long as the cd is at the global scope it will leave the terminal at that level. However, there is risk scripting at the global scope.
When you request the help by opening cmd.exe and running pushd /?:
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
path Specifies the directory to make the current directory.
Therefore it will remain in the directory you push to, until you exit the script:
if you create something like example.cmd in C:\Temp with content:
#echo off
pushd "C:\Folder1\Files"
It will then exit at that file location.
As a side note, to return to the folder you started in you require popd, in fact you need to popd for each pushd.

change directory in batch script past a folder without a name

I have a folder without a name and an image where I keep most of my important documents at work (away from prying eyes).
I'm writing a batch script, from command prompt I can change directory to any folder past that folder, but when I duplicate the exact same command in a batch script it doesnt seem to work. I get the error "System cannot find the file specified" error.
here is my script.
#echo on
net use Z: /delete
pause
net use Z: "\\agfs1\IS\Monthly reports"
timeout /t 5
cd "C:\Users\lalderman\Desktop\ \_Monthly reports"
copy /Y "Monthly Report - Lee.txt" Z:\
pause
after the cd it gives me the error. I've tried it with and without quotes.
Well,
First, for clarification, from what I see above, you have a directory with the name, " " (e.g. the directory's name is a space.).
And, I think you are having a conflict in the different ways things are read in the Microsoft platform you are using; namely between how the Command Prompt reads spaces as part of a path versus how the batch script parser is reading those same spaces in the path.
Now, I just tried doing a Change Directory operation in a batch file on WinXP across and under a folder that was just named " ", and it worked as expected.
#echo on
pause
echo "Starting..."
cd "C:\DLS\Tests\ \chkthis"
pause
echo "Did we get it?"
So, now the question is; which platform are you trying to do this on?
HTH.
Use the 8dot3 name. In your desktop directory, type dir /x /ad to see what the 8dot3 name of your "directory without a name" is and use that in your script path.

Batch - Resolve relative path

My batch scripts are located in the following directory:
\\test\public\windows\scripts\32\test.bat
I'm trying to get the path of this folder: \test\public\windows\logs
I've tried using %~dp0 in order to get the path of the script, and i've tried:
SET REL_PATH=..\..\
The thing is that it's not showing me this folder.
SET REL_PATH=..\..\
path points to the path according to you, to verify where it is taking from, please make a
echo %CD%
it will tell you the path the system is looking at.
then set the relative path from %CD% to the path of your script.
Please comment if you get stuck after doing this.
For example:
echo %CD%
gives C:\windows\system32
and you want to execute batch file at c:\test\public\windows\scripts\32\test.bat
you will need to do
SET REL_PATH=%CD%\..\..\test\public\windows\scripts\32\
To move to this path do a:
cd /d %REL_PATH%
To solve UNC path do PUSHD and POPD:
#echo off
pushd \\test\public\windows\scripts\32\
REM do your work
call test.bat
popd
Reference: How to run batch file from network share without "UNC path are not supported" message?
Use the PUSHD command when working with network drives. This will assign an unused drive letter to the UNC path so you can navigate it like normal. Then you can use POPD to release it.
For example:
#ECHO OFF
PUSHD "\\test\public\windows\scripts\32"
REM This will be \\test\public\windows
DIR ".\..\..\"
POPD

Any way to get folder of batch script if it is run from a different location?

I have put my batch script's folder in my PATH environment variable, so I can open up any command prompt and can run it from anywhere. The problem is I have links to other files as relative paths in the script, so I need to get the path of the actual script.
Typing ~dp0 only gives me the path of the location I am in in the command prompt when running the script, and the same for %cd%. I would like the location of the actual script. Is this possible?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
:: build a simple batch file in "%sourcedir%" which is NOT on the path
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO ECHO %%~dp0
)>"%sourcedir%\q24777129s.bat"
:: Force "%sourcedir%" into the path
SET path=%path%;%sourcedir%
:: execute the batch built
q24777129s
GOTO :EOF
This little script should demonstrate that %~dp0 indeed shows the script file's location. In my case, it showed U:\sourcedir\ which is not on the (original) path.
If it doesn't work for you, then we'd need more information, explicitly specifying the procedure you are using - what you are typing, where the script is located and the exact names of the batches/executables involved.

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.

Resources