?
Solved

How do i get folder name of variable in batch script?

Posted on 2008-02-08
5
Medium Priority
?
2,738 Views
Last Modified: 2008-02-09
I'm have a Windows batch script with a variable like this:

SET SOURCE=C:\user\folder1

How do I pull out just the folder name "folder1" from %SOURCE%?
0
Comment
Question by:bemara57
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 20856163
Here is how to do it with that exact folder name -- I'm working on a way to make it more generic:

@echo off
SET SOURCE=C:\user\folder1
for /f "tokens=1-3 delims=\" %%D in ('@echo %SOURCE%') do set SUBFOLDER=%%F
echo %SUBFOLDER%
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 20856241
Got it!  This will work with a path of any length:

@echo off
SET SOURCE=C:\user\folder1
for /f %%D in ('@echo %SOURCE:\=.%') do set SUBFOLDER=%%~xD
set SUBFOLDER=%SUBFOLDER:.=%
echo %SUBFOLDER%
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 1000 total points
ID: 20856257
... removing the space delimiter so that folder names with spaces will also work:

@echo off
SET SOURCE="C:\user\folder1\subfolder2\subfolder3"
for /f "delims=" %%D in ('@echo %SOURCE:\=.%') do set SUBFOLDER=%%~xD
set SUBFOLDER=%SUBFOLDER:.=%
echo %SUBFOLDER%
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1000 total points
ID: 20856492
I have this one in my toolkit:

@echo off
set source=%cd%
set folder=
for /f "tokens=*" %%a in ("%source%") do set folder=%%~na
echo The users folder name is %folder%

Replace the %cd% (current directory) with your c:\user\folder1 etc.

Of course if your script already sets

SET SOURCE=C:\user\folder1

Why not do it like this:

SET USERFOLDER=folder1
SET SOURCE=c:\user\%USERFOLDER%

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 20856704
thanks for the split of points.

Steve
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question