Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Microsoft Support > Windows 2000 Pro / NT Workstation Support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Windows 2000 Pro / NT Workstation Support Find support for Windows 2000 Pro / NT Workstation here

Reply
 
LinkBack Thread Tools
Old 10-18-2009, 12:44 PM   #1 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 7
OS: Windows 2000 Pro Sp4


CMD.exe Batch Files - Writing out first line of files residing in a directory

Apologies if I'm posting in the wrong place but this is my first post

Help! I'm stuck with a simple batch file I've been creating. It should be simple but .... ! I would be ever so grateful if someone could come to my rescue with the variables I need.

I have a directory of text files and I am trying to run these through a batch file so that I can access each file, read the first line of text in each one and write it to another file - ie append each first line, on a separate line in another file, as each file gets read in. However I've got stuck.

Everywhere I've checked I've found a line of code which will pull the first line of text off a specific file but nowhere can I find one where there are many file names (residing in the directory) which have to be handled.

What I have got so far is:

for %%a in ("c:\documents and settings\sh\my documents\file*.txt") do (
set /p var=<####
%var% >> output.fil
)

I need something to replace where I have the 4#s to reflect the different file names. They all start with File and have the extension .txt eg File125.txt, File176.txt, File205.txt etc

As a silver surfer I do my best but I'm way behind you experts!

Thank you in anticipation!

Dalgetty

Last edited by Dalgetty; 10-18-2009 at 12:48 PM.
Dalgetty is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 10-18-2009, 07:12 PM   #2 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

Welcome to TSF!

Give this a try:
Code:
@Echo Off
Setlocal EnableDelayedExpansion
Set _Path=%Userprofile%\My Documents
For /F "Tokens=1 Delims=" %%a In ('Dir /A-D /B "%_Path%\file*.txt"') Do (
Set /p var=<"%_Path%\%%a"
>>output.fil Echo.!var!
)
You can spell out the complete path instead of using the UserProfile variable if you are running this from a different user account.

HTH

Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?

Last edited by TheOutcaste; 10-18-2009 at 07:13 PM.
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-19-2009, 12:19 PM   #3 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 7
OS: Windows 2000 Pro Sp4


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

Jerry,

That was pure dead brilliant!! You're a star! It worked a treat. I have now added %%a between Echo. and !Var! so that the output file has the file name preceding the relevant line of text. However, I find I'm stymied again as I have been unable to get the code to output what I want eg output the name of the as File123 rather than File123.txt. In other words I want to leave off the file extension from the file name but I can't work out how. If this is possible I would be very grateful for your assistance again.

Many many thanks again!

Dalgetty
Dalgetty is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-19-2009, 01:23 PM   #4 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

The ~n loop variable modifier will output the file name only:
>>output.fil Echo.%%~nA - !var!

See For /? for a list of the loop variable modifiers, all the way at the bottom:
Code:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-19-2009, 02:28 PM   #5 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 7
OS: Windows 2000 Pro Sp4


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

Hi Jerry,

When I changed the coding to >>output.fil Echo.%%~nA - !var! as you suggested I found it doesn't work - it produces %~nA + the line of text. Any suggestions what I'm doing wrong?

Kind regards

Dalgetty
Dalgetty is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-19-2009, 02:47 PM   #6 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 815
OS: MS-Dos 6.22 - Win7


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

Drat, my bad. The loop variables are case sensitive. %%A and %%a are not the same, so use a lower case a - %%~n a
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-20-2009, 01:57 PM   #7 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 7
OS: Windows 2000 Pro Sp4


Re: CMD.exe Batch Files - Writing out first line of files residing in a directory

Hi Jerry,

That's ace - a perfect solution for me!

Many thanks

Dalgetty
Dalgetty is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 02:01 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85