1

I have a path in variable (script parameter) %2. I need to do the following:

  1. Extract the leaf (last folder from the path) to a variable.
  2. Run the following command: robocopy %2 \\somepath\%leaf

I was told this could be done in PowerShell (cause I've tried going with batch file alone and failed miserably) Here's a pseudocode representation of what I'd like to achieve:

set leaf = powershell -command (split-path %2 -leaf)
robocopy %2 \\somepath\%leaf

Any idea how to write this correctly? Thank you.

3
  • Can you give me an example with some mock folder names? I'm having a hard time following you with your usage of %2. Batch scripting is not a strength of mine, while PowerShell is. Commented Feb 5, 2015 at 14:41
  • I'm sorry but that would be of no use to me. Just like you, I can code in PowerShell, not Batch. I need to write the the batch command so that it'll work in my batch script, with the %2 and all. Commented Feb 5, 2015 at 14:53
  • any reason why you can't run robocopy from within powershell. In theory you could use batch FOR statement to get the output from powershell but why?? e.g. $leaf = (split-path %2 -leaf); robocopy %2 \somepath\$leaf Commented Feb 5, 2015 at 14:55

2 Answers 2

2

Whenever you want to set a batch variable to the output of a command, use for /f. Here's an example:

@echo off
setlocal

set "psCommand=powershell -command "(split-path '%~2' -leaf)""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"

echo %leaf%

But this is a terribly inefficient way to retrieve the last folder of a path. Instead of invoking PowerShell, what you should do is this:

@echo off
setlocal

for %%I in ("%~2") do set "leaf=%%~nxI"

echo %leaf%

The %%~dpnxI notation gets

  • d = drive
  • p = path
  • n = name
  • x = extension

It's traditionally intended for files, rather than directories; but it works just as well for directories anyway. See the last couple of pages of for /? in a console window for complete details.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that finally worked (the second option). I have no idea how or why it works, and I've been battling with this for hours. Also, in your first example, why is robocopy part of the powershell command?
@Shaggydog Your question was originally formatted with robocopy on the same line as split-path. I didn't pay close enough attention to whether that was valid PowerShell-ese or not. Edited.
0
FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO ECHO %%~nxb

Batch one-liner. Take the parameter (second parameter here), remove any quotes and re-apply them. Select the drive and path, add '.' then select the name and extension of the result making leaf required.

Obviously, if you require this in a variable,

FOR %%a IN ("%~2") DO FOR %%b IN ("%%~dpa.") DO set "leaf=%%~nxb"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.