Jump to content

Recommended Posts

Posted

My question pertains to a simple batch file that will run on Windows 2000 and later. Basically, I want to know if there is some way of finding the last directory name in a path and assigning only that last directory name as a variable. Here's what I'm talking about:

Say my path is "c:\test path\folder1\Last Directory" and this path is found in the batch file while doing a "For /R (c:\test path) %%d IN (*.pdf) ...etc etc." Say a PDF is located in "c:\test path\folder1\Last Directory" and that particular PDF can now be represented as %1. Say I have a compression operation that will compress the PDF file, but it needs the name of the directory (in this case "Last Directory") the PDF file is in as a name for the final compressed file. So the file would end up being called "Last Directory.zip." How can I create a variable that gets only the last directory in a path? Stuff like %~dp1 or %~p1 doesn't work because it grabs the whole path, not just the last directory name. And %~n1 doesn't work because then it using the filename of the PDF and not the directory name.

What I would like to do is: SET LASTDIRNAME=%VAR (where %VAR is the solution to my problem above) and then I could do this when naming my zip files during the batch:

zip "%~dp1%1" "%LASTDIRNAME%.zip"
; With the variables removed it would look like this:
; zip "c:\test path\folder1\Last Directory\somefile.pdf" "Last Directory.zip"


Posted

If you want to assign a variable in a batch file try this:

do_whatever.exe > c:\temp\data.txt

set /p DATA=<c:\temp\data.txt

del c:\temp\data.txt

echo Data: %DATA%

-John

Posted

There is multiple solutions... here is one :

You need a %~dp one to change the current dir to the one of you file

Then you go to its parent (CD ..) and assign the current dir to a var.

And now you can do a classic set substitution to remove the parent part of the path.

@echo off
setlocal enableextensions enabledelayedexpansion
rem the path contains spaces, so it MUST be quoted :
set myfile="c:\test path\folder1\Last Directory\somefile.pdf"
call :getLastDir %myfile%
echo Last Dir is : %LastDir%
goto :eof
:getLastDir
set mypath=%~dp1
pushd "%mypath%"
cd ..
set mypath=!mypath:%cd%=!
set LastDir=%mypath:\=%
popd
goto :eof

With some testing, you will find this solution buggy when the %~dp1 folder does not exist.

Thus here is a shorter one (two lines, no need for delayedexpansion, and working even with nonexistent target) :

You need to nest two %~ substitutions, second one inside a for loop.

First one is a %~p (or %~dp) to extract the path (excluding the last part of it, in your case, the file name + extension) with its trailing backslash.

Then you remove this backslash. (with classic set substitution)

And now you can use for on the remaining part to do a %~n substitution.

@echo off
setlocal enableextensions
rem the path contains spaces, so it MUST be quoted :
set myfile="c:\test path\folder1\Last Directory\somefile.pdf"
call :getLastDir %myfile%
echo Last Dir is : %LastDir%
goto :eof
:getLastDir
set mypath=%~p1
for %%v in ("%mypath:~0,-1%") do set LastDir=%%~nv
goto :eof

++

Posted
There is multiple solutions...
Here's one of them, without a for loop!
@ECHO OFF &SETLOCAL
SET "VAR=C:\test path\folder1\Last Directory\somefile.pdf"
CALL :DAD "%VAR%"
ECHO/The `Parent Folder` is %P2%
ENDLOCAL &GOTO :EOF
:DAD
SET "P1=%~p1¨"
SET "P1=%P1:\¨=%"
:LOOP
SET "P2=%P1:*\=%"
IF "%P1%" EQU "%P2%" GOTO :EOF
SET "P1=%P2%"
GOTO :LOOP

Posted

Thanks! I think I have everything I need with the three posts here. Preferably I would like to do this without using a FOR/CALL routine. I was on the verge of doing the solution in the first post where text would pipe into a file, then be parsed for the last section after the / but that parsing process is way beyond my abilities, for now. I think Yzöwl has it, but the other solutions I can integrate into what I'm trying to do as well, just not for my original problem.

Posted
Here's one of them, without a for loop!

I just didn't knew that wildcards are accepted this way :w00t:

Why such things aren't said in "manpages" ?

@quijibo > you're welcome. in fact, i answered you because seeing your question answered a problem of mine :)

++

Posted

Yeah it seems like something a lot of people would need to do in their scripts, but I just couldn't find any specialized exe or routine for doing it.

  • 1 month later...
Posted (edited)

In case anyone looks this thread up in the future, I came up with another way of doing this. It took me some long, hard thought to come up with something that is ultimately so simple. It helps to know all of your built-in variables.

@ECHO OFF
CD /D "%~dp1"
FOR %%G IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF '"%CD%"'=='"%%G:\"' SET LD=%%G
IF NOT '"%LD%"'=='""' GOTO :NEXT
CALL :LASTDIR "%CD%"
:LASTDIR
SET LD=%~nx1
:NEXT
ECHO %LD%
PAUSE

The first line assumes that you're relying on a %1 variable (which represents either a file or a directory) that has been passed from a previous routine. The quotes from line four are not part of the LD variable when it is created in the :LASTDIR section. The %~nx1 ensures that any directories named like folder_name.stuff.more.stuff are not truncated after the word name. This works for any directory, even ones that end up just root drive letters like C: (that is what line two does). If you make this into a .bat file, you can test it by running the batch file in any directory, even when you have no predefined %1 variable. When there is no %1, the LD variable will always return the last directory in the path that the batch file is running from. I honestly still can't wrap my head around the syntax of most of the solutions posted above since I'm no command line guru, so I came up with this one because I understand it 100%. Thanks to Delprat for making me think about this again.

Edited by quijibo

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...