Solved

How do I parse a filename?

Posted on 2004-08-26
3
673 Views
Last Modified: 2010-10-05
Windows 2000 Professional.

I have files like this:

C:\temp
test_001_xxx.txt
test_002_xxx.txt
test_003_xxx.txt

The 001 is the dept code.  

I want to reference the dept code in various sections of my dos script.
0
Comment
Question by:seckel
3 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 11906908
Give this a try:

@echo off

setlocal

for /f "delims=" %%a in ('dir /b test_???_???.txt 2^>NUL') do call :PROCESS "%%a"

goto :EOF

:PROCESS

set FileName=%~1

echo File name:   %FileName%
echo Department: %FileName:~5,3%

Good Luck,
Steve
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 11906940
@echo off
setlocal enabledelayedexpansion

for %%f in (*.txt) do (
      set file=%%f
      set dept=!file:~5,3!
      echo !dept!
)
pause
0
 

Author Comment

by:seckel
ID: 11911876
Steve

You are most wise and knowing when it comes to DOS....
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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