Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return only the current directory name, not full path

22 views
Skip to first unread message

Jim Helfer

unread,
Nov 5, 2009, 1:57:09 PM11/5/09
to

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

Tom Lavedas

unread,
Nov 5, 2009, 2:14:52 PM11/5/09
to

Maybe this will serve ...

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

Jim Helfer

unread,
Nov 5, 2009, 4:51:15 PM11/5/09
to

> 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.

Tom Lavedas

unread,
Nov 5, 2009, 5:09:19 PM11/5/09
to

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

Todd Vargo

unread,
Nov 5, 2009, 5:19:14 PM11/5/09
to

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/?

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Tom Lavedas

unread,
Nov 5, 2009, 10:46:39 PM11/5/09
to

Oop, I hate when that happens.

Thanks for fixing that.
___________________
Tom Lavedas

foxidrive

unread,
Nov 6, 2009, 12:06:09 AM11/6/09
to
On Thu, 5 Nov 2009 17:19:14 -0500, "Todd Vargo" <tlv...@sbcglobal.netz>
wrote:

>Jim Helfer wrote:
>>
>> > Maybe this will serve ...
>

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

Jim Helfer

unread,
Nov 9, 2009, 12:06:39 PM11/9/09
to
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

"Tom Lavedas" <tglb...@cox.net> wrote in message
news:5b881dce-8ec3-4a29...@m35g2000vbi.googlegroups.com...

Tom Lavedas

unread,
Nov 9, 2009, 12:46:17 PM11/9/09
to
On Nov 9, 12:06 pm, "Jim Helfer" <JimHel...@newsgroup.nospam> wrote:
> 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

Jim Helfer

unread,
Nov 9, 2009, 4:01:21 PM11/9/09
to
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

"Tom Lavedas" <tglb...@cox.net> wrote in message

news:3dfc1c04-a913-40f6...@n35g2000yqm.googlegroups.com...

billious

unread,
Nov 9, 2009, 10:23:35 PM11/9/09
to

"Jim Helfer" <JimH...@newsgroup.nospam> wrote in message
news:uTrRM$XYKH...@TK2MSFTNGP02.phx.gbl...

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

Tom Lavedas

unread,
Nov 10, 2009, 8:53:44 AM11/10/09
to
On Nov 9, 10:23 pm, "billious" <billious_1...@hotmail.com> wrote:
> "Jim Helfer" <JimHel...@newsgroup.nospam> wrote in message
>
> news:uTrRM$XYKH...@TK2MSFTNGP02.phx.gbl...
>
>
>
> > "Tom Lavedas" <tglba...@cox.net> wrote in message

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

billious

unread,
Nov 10, 2009, 10:35:13 AM11/10/09
to

Oh - I think I get what the OP wants - much easier if there was an
example....

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

becomes

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

How about

This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "delims=" %%i in ( ' dir /b /s /a:d ' ) do (
[4] for /f "delims=" %%j in ( "%%i" ) do (
[5] set second=%%~dpj
[6] for /f "delims=" %%k in ( "!second:~0,-1!" ) do (
[7] ECHO MD "%%~dpk%%~nxi\%%~nxk" 2>nul
[8] ECHO MOVE "%%i\*.*" "%%~dpk%%~nxi\%%~nxk\"
[9] ECHO RD "%%i" 2>nul
[10] )
[11] )
[12])
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO keyword needs to be removed to activate the MD/MOVE/RD It is there
as a safety measure to show what the process WOULD do until
you have verified that it will do what you require

The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.


%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

Naturally, this would have the usual sensitivity to poison characters and
would be better run where there is no ...\dir2\dirx instance where thee is
an existing ...\dirx\dir2


Jim Helfer

unread,
Nov 10, 2009, 10:38:58 AM11/10/09
to

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

billious

unread,
Nov 10, 2009, 10:50:23 AM11/10/09
to
billious wrote:
> Tom Lavedas wrote:
[snip]

I should add that this should be run with current directory=the dir2 level

To run from the dir1 level, the following command is likely to work:

for /f "delims=" %d in ( ' dir /b /a:d ' ) do cd %d&call
\path|to\above\batch&cd ..

(note %d not %%d as run expected from command level. OR convert it to a
batch line and include it if preferred...)


Jim Helfer

unread,
Nov 10, 2009, 10:46:56 AM11/10/09
to

>>
>
> 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.

billious

unread,
Nov 10, 2009, 10:54:15 AM11/10/09
to

Typo on my part. Should be

for /F ....

not

for /D ...


foxidrive

unread,
Nov 10, 2009, 4:57:12 PM11/10/09
to

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


Jim Helfer

unread,
Nov 10, 2009, 5:51:19 PM11/10/09
to

"billious" <billio...@hotmail.com> wrote in message
news:PvGdnWHVy4o3EWTX...@westnet.com.au...

Aha! That looks like it give me what I need.

Thanks
Jim

0 new messages