Solved

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

Posted on 2008-10-02
7
1,147 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

734 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