batch script to set a variable with the current path location - windows

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%

Related

How to remove last segment in filepath in command prompt

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

set parent=%CD%\.. doesnt evaluate to the folder above as expected

In the batch file below I get current path.. How do I get it to print one level above "current path" ?
set current=%CD%
set parent=%CD%\..
echo %parent%
There is no good way to do this, other than changing the directory temporarily.
Fortunately, there's an easy way to go back a directory and then switch again:
set current=%CD%
pushd ..
set parent=%CD%
popd
echo %parent%
This uses the pushd and popd commands, which are nice.
Note that this will result in parent equaling current if you are on the root directory (EG C:), but the old script does the same.
This should return the full path to the parent folder of the current active directory
for %%a in (..\) do set "parent=%%~fa"
There is no variable for one level above your current path. To get the parent folder, simply cd back one directory, set the parent variable to that current directory, and if desired, cd back to your original directory. Like this:
set current=%CD%
cd..
set parent=%CD%
cd %current%
echo %parent%
pause
Run this from a first level folder or higher.
#echo off
for %%a in ("%cd%") do echo "%%~dpa"
pause

find path of current folder - cmd

I use this script to find out the current folder with its .bat file:
for /f %%i in ("%0") do set curpath=%%~dpi
echo %curpath%
it doesn't work correctly, if the path contains spaces(D:\Scripts\All Scripts -> retrieves only D:\Scripts\, if I place in the folder, whose path doesn't have spaces it retrieves the full path). How can I fix it?
2015-03-30: Edited - Missing information has been added
To retrieve the current directory you can use the dynamic %cd% variable that holds the current active directory
set "curpath=%cd%"
This generates a value with a ending backslash for the root directory, and without a backslash for the rest of directories. You can force and ending backslash for any directory with
for %%a in ("%cd%\") do set "curpath=%%~fa"
Or you can use another dynamic variable: %__CD__% that will return the current active directory with an ending backslash.
Also, remember the %cd% variable can have a value directly assigned. In this case, the value returned will not be the current directory, but the assigned value. You can prevent this with a reference to the current directory
for %%a in (".\") do set "curpath=%%~fa"
Up to windows XP, the %__CD__% variable has the same behaviour. It can be overwritten by the user, but at least from windows 7 (i can't test it on Vista), any change to the %__CD__% is allowed but when the variable is read, the changed value is ignored and the correct current active directory is retrieved (note: the changed value is still visible using the set command).
BUT all the previous codes will return the current active directory, not the directory where the batch file is stored.
set "curpath=%~dp0"
It will return the directory where the batch file is stored, with an ending backslash.
BUT this will fail if in the batch file the shift command has been used
shift
echo %~dp0
As the arguments to the batch file has been shifted, the %0 reference to the current batch file is lost.
To prevent this, you can retrieve the reference to the batch file before any shifting, or change the syntax to shift /1 to ensure the shift operation will start at the first argument, not affecting the reference to the batch file. If you can not use any of this options, you can retrieve the reference to the current batch file in a call to a subroutine
#echo off
setlocal enableextensions
rem Destroy batch file reference
shift
echo batch folder is "%~dp0"
rem Call the subroutine to get the batch folder
call :getBatchFolder batchFolder
echo batch folder is "%batchFolder%"
exit /b
:getBatchFolder returnVar
set "%~1=%~dp0" & exit /b
This approach can also be necessary if when invoked the batch file name is quoted and a full reference is not used (read here).
for /f "delims=" %%i in ("%0") do set "curpath=%%~dpi"
echo "%curpath%"
or
echo "%cd%"
The double quotes are needed if the path contains any & characters.
Use This Code
#echo off
:: Get the current directory
for /f "tokens=* delims=/" %%A in ('cd') do set CURRENT_DIR=%%A
echo CURRENT_DIR%%A
(echo this To confirm this code works fine)

batch file to move and rename folder to a relative path

I have the following batch file in Windows 7 to be executed by a context menu shortcut. My aim is to move and rename a quote folder containing subfolders and files to a different path and rename it with the project number inserted when prompted.
for %%Q in (.) do set quotenumber=%%~nQ
for %%Y in (.\..) do set year=%%~nY
for %%C in (.\..\..\..) do set client=%%~nC
set /P projectnumber="Enter Project number>"
move "c:\myfiles\mainfiles\clients\%client%\quotes\%year%\%quotenumber%" "c:\myfiles\mainfiles\clients\%client%\projects\%year%\%projectnumber%"
I get the error "the process does not have access to the file because it is being used by another process".
Can anybody tell me what I am doing wrong? I am not a programmer and cannot get this to work!
Any help would be greatly appreciated.
Looking at your code I'm assuming that you;re executing it in c:\myfiles\mainfiles\clients\%client%\quotes\%year%\%quotenumber% dir.
And in the last line you try to move the same dir to another place.Which is impossible because the dir is held by the script itself.Try this:
for %%Q in (.) do set quotenumber=%%~nQ
for %%Y in (.\..) do set year=%%~nY
for %%C in (.\..\..\..) do set client=%%~nC
set /P projectnumber="Enter Project number>"
cd ..
move "c:\myfiles\mainfiles\clients\%client%\quotes\%year%\%quotenumber%" "c:\myfiles\mainfiles\clients\%client%\projects\%year%\%projectnumber%"

Retrieving variable value from filename with a batch file

Is there a way with a batch file to instantly MOVE a file when it's downloaded into Directory A into Directory B based on a variable in the file's name?
I have a file naming convention that looks like this: Photo-87654321-1.jpeg
The 87654321 part is the variable. Now, with if statements and such, I can locate the directory, or if it doesn't exist, create the directory and then place the image in there. The problems I'm having is: a) copying that variable string from the file name, b) running this script every time a file is moved into Directory A.
You haven't given enough details about what you want to do with the file when you find the variable number, so I can only improvise.
This script will get the variable name from the files in C:\DirectoryA and then move them into a folder with that name.
:LOOP
for /f "tokens=2 delims=-" %%a in ('dir /b /a-d "C:\DirectoryA"') do (
md "%%~na"
move "%%a" "%%~na"
)
goto :LOOP
This should give you enough details to tweak to your needs, but if you need anything more specific please provide more details.
Note: Given that you want to move files as soon as they are put in DirectoryA, this is on an infinite loop, so you may want to watch your CPU.

Resources