Solved

Batch file to pull last folder name from a unc path for a specific file

Posted on 2008-10-02
7
1,117 Views
Last Modified: 2008-10-02
I want to create a batch file that can read through a file containing a list of files

the files are in the format of

\\servername\folder1\folder2\folderN\filename.xtn

The folder depth is variable

I want to write a batch file that will find FolderN for each filepath listed in fileX.txt
0
Comment
Question by:sollus
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22623616
Unless I'm going mad I'm sure I saw an answer from sirbounty here a second ago?!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22623629
Perhaps you just need more sleep... :^)
I read the Q wrong and as such had my comment removed.  Besides, I remembered you've got the helm at this time of day...haha!
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 150 total points
ID: 22623683
Try this.  Handles upto 10 deep directories at the moment, needs some
@echo off

for /f "delims=\ tokens=1-10" %%a in (fileX.txt) do call :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j"
goto :eof

:process
set prevfolder=
set lastfolder=

:next
if "%~1"=="" goto foundit
 set prevfolder=%lastfolder%
 set lastfolder=%~1
 shift
goto :next
:foundit
 if "%prevfolder%"=="" set prevfolder=%lastfolder%
 echo Folder is %prevfolder%

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 43

Expert Comment

by:Steve Knight
ID: 22623688
Powers of being able to get such things done eh!  I wasn't going mad then!

Steve
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 350 total points
ID: 22625940
This runs slow but should handle directories of any depth.



@echo off

setlocal enabledelayedexpansion
 

set list=fileX.txt
 

for /F "tokens=* usebackq" %%G in ("%list%") do (

 set parent=%%~dpG

 for /F "tokens=*" %%H in ("!parent:~0,-1!") do echo %%~nH

)

Open in new window

0
 

Author Comment

by:sollus
ID: 22626273
Great, thanks for the answers. They both work, but I found Shift-3's answer to be more complete.

If you have the time I would appreciate if you could explain the working of the code.

Specifically how are these instructions interpreted
"=%%dpG"
"("!parent:~0,-1!")"
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 22627114
%%G represents each line in the text file.  %%~dpG is the disk (in this case just \\) and path from each of these lines. !parent:~0,-1! removes the last character (a trailing backslash) from this path.  %%~nH is the last folder in the path.

See these pages for more information:
http://www.ss64.com/ntsyntax/parameters.html
http://www.ss64.com/ntsyntax/varsubstring.html
http://www.ss64.com/nt/for_cmd.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now