Solved

Bat File write text file

Posted on 2013-01-21
4
740 Views
Last Modified: 2013-01-22
Hi All -
I am out of my element here ... never worked with .bat files.  I am looking for a quick and dirty way to read a directory and create a text file for each file in the directory... trying to avoid having to do this manually....(there's 2000).

Example:  I have three documents in my directory: pm1.doc, pm2.doc, pm3.doc.   I need the bat file to read thru each one and create a pm1.txt, pm2.txt, and pm2.txt with one line of text inside the text file - the file name (which is variable) and save it in another location on the server.

I'm running this script clientside on Windows XP, but it read and write from folders on the server.


Is there an easy way to do this?   Thanks for you help!!
0
Comment
Question by:mojeaux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 38802603
Try this; just save it as Whatever.cmd, and change the three variables at the beginning.
@echo off
setlocal
set Source=C:\Temp
set Target=D:\Temp
set Mask=*.doc
for %%a in ("%Source%\%Mask%") do (
	echo Processing %%~nxa ...
	>"%Target%\%%~na.txt" echo %%~nxa
)

Open in new window

0
 

Author Comment

by:mojeaux
ID: 38803205
Thank you for the prompt response.  I have a couple questions about the syntax.  

I'm guessing 'set local' is setting it to run on my machine?  

I understand about the source and target variables.  Mask is the variable for the files its reading thru, correct?  

Then you set up a 'for' loop to read thru these files... So what does '%%a' represent?   It looks like these are wild cards?  

Also what does  '%%~nxa ... ' do?

The greater-than sign saves the output to the new text file. txt, correct?


Just would lik a clear understanding of how the loop is processing.   Thanks!
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 38804912
"setlocal" sets the scope of the variables defined in the script, so that they won't be set anymore once the script ends (enter "help setlocal" in a command prompt for details).
The "%%a" variable is the "for" loop variable that contains the current file name.
"%%~nxa" expands to the Name and eXtension of the file, so just "pm1.doc" instead of "C:\Temp\pm1.doc" (enter "help for" in a command prompt for details).
The ">" redirects the output of the "echo" command to the file given (%%~na, so only the file name, "pm1") and the extension ".txt".
0
 

Author Closing Comment

by:mojeaux
ID: 38806043
Thank you for your time and explanation of the code.   It was extremely helpful.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

734 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