batch-filecmddirectoryoutputwindows-scripting

Get Directory Path 2 Levels Up and Change the Format (Batch File)


I am writing a large script to migrate many files at once and I will need to be able to get each file directory in a certain format.

If you look in the image below, you can see an example of a file in its directory

Example

The directory here is C:\Users\Work\Desktop\Test Env\Videos on Server\2016\02\1st

But I need it to display as 2016-02-1st which is 2 levels up and has dashes instead of slashes

I use the code below to get the path:

set p=%cd%
echo %p%
pause

Any ideas on how to change the output?


Solution

  • If it's always the 6th, 7th, and 8th:

    for /f "delims=\ tokens=7-9" %%a in ("%cd%") do echo %%a-%%b-%%c