![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Windows 2000 Pro / NT Workstation Support Find support for Windows 2000 Pro / NT Workstation here |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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. |
|
|
|
| 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 |
|
|
#2 (permalink) |
|
TSF Enthusiast
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!
)
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. |
|
|
|
|
|
#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 |
|
|
|
|
|
#4 (permalink) |
|
TSF Enthusiast
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? |
|
|
|
|
|
#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 |
|
|
|
|
|
#6 (permalink) |
|
TSF Enthusiast
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? |
|
|
|
![]() |
| Thread Tools | |
|
|