Solved

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

Posted on 2008-10-02
7
1,157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

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 …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

632 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