How to remove last segment in filepath in command prompt - windows

I am working on a batch application where I have to get the current directory path via command prompt.
I can get the file location as:
C:\Users\Username\Downloads\Images\dance.png
or current directory as:
C:\Users\Username\Downloads\Images\stock_images\
I have to get the desired directory path as:
C:\Users\Username\Downloads\Images\
I have looked around on stackoverflow for the solution but could not find something useful.
So, how can I get parent directory of a file or subdirectory using batch script?

I've posted it a number of times, but can't locate it for the moment
#ECHO OFF
SETLOCAL
SET "name=C:\Users\Username\Downloads\Images\stock_images\dance.png"
FOR %%a IN ("%name%") DO FOR %%b IN ("%%~dpa.") DO ECHO Grandparent=%%~dpb&ECHO parent=%%~nxb

Related

Getting parent directory not working in windows 8 batch file

i have a folder structure as
c:\name\myname
I wrote a batch file
test.bat
#echo off
echo %CD%
echo %CD%\..
set k=%CD%
set c=%k%\..
echo %k%
echo %c%
pause
Output
c:\name\myname
c:\name\myname\\..
c:\name\myname
c:\name\myname\\..
Expected Output
c:\name\myname
c:\name
c:\name\myname
c:\name
I have installed windows 8 OS. I want to get parent directory from current directory. May be i don't know the correct syntax to get parent directory.
There are two ways to obtain a reference to a file/folder: arguments to batch files and for command. This second option is what we will use here. As it is not clear what parent do you need, let's see how to obtain each
1 - Get parent of current active directory
for %%a in (..) do echo %%~fa
get a reference to the parent of the current active folder inside the for replaceable parameter %%a and once we have the reference, get the full path to it
2 - Get parent of the folder holding the batch file
for %%a in ("%~dp0.") do echo %%~dpa
same idea. %0 is a reference to the current batch file, so %~dp0 is the drive and path where the batch file is stored. This value ends in a backslash, so, to get a reference to the folder an aditional dot is added. Once we have the reference to the folder holding the batch file in %%a, %%~dpa will return the drive and path where the element referenced in %%a is stored. As %%a is the folder holding the batch file, %%~dpa is the parent, the folder where is stored the folder with the batch.

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.

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Batch file: for parameters

I am having trouble understanding what does following windows batch file do, can somebody explain:
for /f %%i in ("%0") do set curpath=%%~dpi
cd /d %curpath%
/*Some other code...*/
cd /d %curpath%
%0 is the full path to the .bat file itself (if run from another directory) and ~dpi is a modifier to extract the drive and directory from a path omitting the file name, so this snippet sets the current drive & directory to the one in which the batch file lives.
I can't see the reason for using a FOR, %~dp0 does the same thing in one go.

batch script to set a variable with the current path location

How can I set a variable with the current location? For instance, if I get in c:\test and want to set the variable to test and if I get inside c:\test\test2 the variable will be set to test2.
I'm thinking about using a for to get inside a lot of folders and check if some file exists, if the correct file exist I want to set the current folder to a variable so I can copy the path and copy the folder.
The main problem deals with copying the rest of the files is the same folder as the .inf file.
The current directory is in the "shadow" variable cd.
You could try
set "var=%cd%"
%~dp0
This expands into the drive & path of the currently running batch file. I usually surround my batch files with something like:
#echo off
pushd %~dp0
...
popd
Edit: It seems I didn't understand the OP. My example gets the location of the currently running script, not the "Current Directory". +1 to jeb.
I think there is a little confussion here. %CD% always have the current directory, so you don't need to add anything to have it. However, by rereading your original question, I think that you need the LAST PART of the current directory, that is, the name of the current location excluding all previous locations. If so, then you may use this:
set i=0
:nextdir
set /a i+=1
for /f "tokens=%i% delims=\" %%a in ("%CD%") do if not "%%a" == "" set lastdir=%%a& goto nextdir
echo Current location: %lastdir%

Resources