Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-02
7
Medium Priority
?
1,169 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 600 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 1400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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