How to print current directory name via MS-DOS commands - windows

I need to print current directory name after CD command.
Is it possible to do? Thanks!
echo off
echo.
echo This batch program deletes some files
echo.
pause
cd "D:\Folder1\"
pause

Just use cd with no arguments supplied. From cd /?:
Type CD without parameters to display the current drive and directory.

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.

Create a batch (.bat) file to move to a specific directory and output (echo) all files listed?

Create a bat file to move to a specific directory and output (echo) all files listed?
My current setup:
cd %CD%\src\main\orders
echo %dir%
I need to apply the following:
Extend upon the current directory; \src\main\orders is the
extension.
Then print the files listed in the full path / directory.
Any ideas?
Thanks
A single batch-file/cmd line seems capable of doing what you've laid out in your question.
#CD /D "src\main\orders" 2>NUL&&Dir /B/A-D
Open a Command Prompt window and enter both CD /? and Dir /? to find out how those two commands are used.
You can remove the first character, # if running the command from the Command Prompt.
In Powershell, you could do this:
Get-ChildItem -Path "PATH TO DIRECTORY"
If you want to view files in subfolders, add the -Recurse switch

Change batch file directory in system32 to CMD command execution point

So basically I have a .bat that is inside my System32 folder.
This batch file accepts a parameter input, this input is a file.
I wanted it to be that I could open my Command Prompt, and for example do
batchfile text.txt
And it would pass test.txt into batchfile.bat. Obviously for my terminal to do this it needs to be in System32.
That is where my issue is. Because the batch file is in System32, when executing the command, it changes my directory to System32.
However the parameter I give it is a file. And when the command is executed and it changes the directory to System32, obviously it can no longer access the file.
How can I get around this?
Before you change directories, expand the filename to its fully-qualified filename and then reference it that way:
#echo off
for %%F in (%1) do set USR_REQFNM=%%~dpnxF
C:
cd \Windows\System32
echo The requested file is "%USR_REQFNM%"
goto ENDIT
REM Cleanup
:ENDIT
set USR_REQFNM=

Command Prompt Script command "cd"

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%"

using space in path for copying files using batch script

I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"

Resources