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"



Help
Back to top










