Solved

How do I parse a filename?

Posted on 2004-08-26
3
649 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Script to 9 64
Copy portion from one log file (fileA) into a new log file (fileB) 8 51
USB backup batch file help needed. 9 84
Build VS2015 project in bat command 3 80
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now