return only the current directory name, not full path

J

Jim Helfer

For a batch file, I need something like %CD% but only the part of the string
that is the current directory name, not the drive letter of path,

for example:

for %CD% = F:\CAD-Drawings\70004,

I want '7004.'

I guess either a utility/command that would do this directly, or a
suggestion on how I could parse the path myself is what I am looking for. I
started by just trying to right a DOS batch file on a windows 7 command
prompt, but I have 2003 servers with Powershell installed that I could use
if that would have to capabilities I am looking for.

Thanks for any suggestions.

Jim Helfer
WTW Architects
Pittsburgh PA
 
T

Tom Lavedas

For a batch file, I need something like %CD% but only the part of the string
that is the current directory name, not the drive letter of path,

for example:

 for %CD% = F:\CAD-Drawings\70004,

 I want '7004.'

 I guess either a utility/command that would do this directly, or a
suggestion on how I could parse the path myself is what I am looking for.I
started by just trying to right a DOS batch file on a windows 7 command
prompt, but I have 2003 servers with Powershell installed that I could use
if that would have to capabilities I am looking for.

Thanks for any suggestions.

 Jim Helfer
 WTW Architects
Pittsburgh PA

Maybe this will serve ...

for %%a in ("%cd%") do set "CurDir=%~na"
echo For example: %CurDir%
_____________________
Tom Lavedas
 
J

Jim Helfer

Maybe this will serve ...

for %%a in ("%cd%") do set "CurDir=%~na"
echo For example: %CurDir%
_____________________
Tom Lavedas

Thanks, but I'm afraid I don't understand the function of "%~na" here.
 
T

Tom Lavedas

 Thanks, but I'm afraid I don't understand the function of "%~na" here. 

Type FOR/? at a command prompt and it will explain most of it.
However, there is one little trick. The HELP says it returns the file
name, but in fact, it actually returns the string following the last
backslash and before the next period - which is almost certainly what
you are looking for. Try it, you might like it ;-)
_____________________
Tom Lavedas
 
T

Todd Vargo

Jim said:
Thanks, but I'm afraid I don't understand the function of "%~na" here.

The code is missing a %. It should be this.

for %%a in ("%cd%") do set "CurDir=%%~na"
echo For example: %CurDir%

For further help, please use /? to review help on the FOR command.

FOR/?
 
T

Tom Lavedas

The code is missing a %. It should be this.

for %%a in ("%cd%") do set "CurDir=%%~na"
echo For example: %CurDir%

For further help, please use /? to review help on the FOR command.

FOR/?

Oop, I hate when that happens.

Thanks for fixing that.
___________________
Tom Lavedas
 
F

foxidrive

The code is missing a %. It should be this.

for %%a in ("%cd%") do set "CurDir=%%~na"
echo For example: %CurDir%

For a more robust solution include the x below so that if a folder has a
period in the name then it will still work.

@echo off
for %%a in ("%cd%") do set "CurDir=%%~nxa"
echo For example: %CurDir%
 
J

Jim Helfer

Ah thanks! I overlooked the "~" functions. This should be what I need,
just need to fiddle with the command so I can grab each directory name and
pass it properly to a copy command to move the folders to their new home.

Thanks
Jim Helfer
 
T

Tom Lavedas

Ah thanks!   I overlooked the "~" functions.  This should be what I need,
just need to fiddle with the command so I can grab each directory name and
pass it properly to a copy command to move the folders to their new home.

Thanks
Jim Helfer

Your original request just said the last one. If you want them all,
then you might want to use a parsing, something like this ...

@echo off
call :sub "%CD:\=" "%"
goto :EOF

:Sub
echo For example %2
if not '%3'=='' (shift & goto :Sub)
_____________________
Tom Lavedas
 
J

Jim Helfer

Oh, I think I may have phrased that badly. I don't need each folder name in
the path (Dir1\Dir2\Dira), I need each subdirectory name at a particular
level, eg:

Dir1\Dir2\Dira
Dir1\Dir2\Dirb
Dir1\Dir2\Dirc

Returning me "Dira", "Dirb", and "DirC" (What I am doing here is
re-ordering the folder organization, from "File Type, then Project Number"
to "Projcet Number, then file type")

Thanks!
Jim Helfer
 
T

Tom Lavedas

Since you appear to be looking for the subdirectory names from the current
directory,

dir /b /a:d

or, to process as a list

for /d "delims=" %%i in ( ' dir /b /a:d ' ) do echo\%%i

Unfortunately, a simple blank directory listing is not recursive (will
not return folders two levels or more below the current one). Adding
the /S switch to make it recursive provides the information, but then
it is back to the parsing problem as the result is returned in an
absolute address form, not relative to the current folder.

Further, you attempt to make it recursive fails because the /D (or /R)
switches cannopt be constructed to use a statement in its set. That
cannot be done without the /F switch, which is not compatible with
either the /D or /R switches.

One though I have is to assign a drive letter to the active directory
using SUBST and then do a 'dir /s /ad /b' and parse the results,
something like this ...

@echo off
setlocal> %temp%\_tmp.txt
subst z: %1
pushd z:
for /f "tokens=2 delims=:" %%a in ('dir /ad /b /s') do (
set "_temp=%%a"
call set _temp="%%_temp:\=" "%%"
call set _temp=%%_temp:~3%%
call echo %%_temp%%
)>> %temp%\_tmp.txt
sort < %temp%\_tmp.txt
del %temp%\_tmp.txt
popd
subst z: /d

However, though this gives a list, it is redundant and probably would
be hard to use to do the job the OP is planning. Though what that job
is is not too clear to me.
_____________________
Tom Lavedas
 
J

Jim Helfer

However, though this gives a list, it is redundant and probably would
be hard to use to do the job the OP is planning. Though what that job
is is not too clear to me.
_____________________
Tom Lavedas

I don't actually need to recurse into the subdirectories, I just need the
folder name , so I can use it in a copy command to move the directory
structure contained there to another location.

Here's a description of the actual task, sorry for not being as clear as I
could be initially:

I have a group of folders that represent data for the office's projects.
Currently they are in this form:

\Cad-Drawings\Project1
\Cad-Drawings\Project2
(..etc)
\Correspondence\Project1
\Correspondence\Project2
(..etc)
\GraphicFiles\Project1
\GraphicsFiles\Project2

The goal is to reverse this sorting arrangement, from "by FileTypes, then by
Project Number" to "by Project Number, then by File Types",

Project1\Cad-Drawings
Project1\Correspondence
Project1\GraphicsFiles
(..etc)

I thought I could run a batch file in each top-level directory
(\Cad-Drawings), which contains a list of directories which are all project
numbers, and read through them in a batch file to copy them to a new
location

In \Cad-Drawing directory, for each subfolder:
XCopy %DirectoryName \%DirectoryName\Cad-Drawing /S/E

Thanks for your assistance
Jim Helfer
 
J

Jim Helfer

Since you appear to be looking for the subdirectory names from the current
directory,

dir /b /a:d

Ah yes! Please pardon my clumsily worded descriptions. I am looking for a
list of subdirectory names so I can subsequently use a batch file to copy
each directory to another location

or, to process as a list

for /d "delims=" %%i in ( ' dir /b /a:d ' ) do echo\%%i

This, however, I can't seem to make work. It returns a ' "delims=" was
unexpected at this time ' error.
 
F

foxidrive

Here's a description of the actual task, sorry for not being as clear as I
could be initially:

I have a group of folders that represent data for the office's projects.
Currently they are in this form:

\Cad-Drawings\Project1
\Cad-Drawings\Project2
(..etc)
\Correspondence\Project1
\Correspondence\Project2
(..etc)
\GraphicFiles\Project1
\GraphicsFiles\Project2

The goal is to reverse this sorting arrangement, from "by FileTypes, then by
Project Number" to "by Project Number, then by File Types",

Project1\Cad-Drawings
Project1\Correspondence
Project1\GraphicsFiles
(..etc)

I thought I could run a batch file in each top-level directory
(\Cad-Drawings), which contains a list of directories which are all project
numbers, and read through them in a batch file to copy them to a new
location

In \Cad-Drawing directory, for each subfolder:
XCopy %DirectoryName \%DirectoryName\Cad-Drawing /S/E

This does as you ask

here M:\ is the target which could be made c:\target\folder\path etc

@echo off
setlocal
for /f "delims=" %%a in ('dir /a:d /b') do (
for /f "delims=" %%b in ("%%~pa.") do (
echo folder is "%%~nxa"
echo parent is "%%~nxb"
xcopy /s/h/e "%%~nxa\*.*" "M:\%%~nxa\%%~nxb\"
)
)
pause
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top