Go Premium for a chance to win a PS4. Enter to Win

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,180 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 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

972 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