Solved

# Bat File write text file

Posted on 2013-01-21
Medium Priority
778 Views
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
Question by:mojeaux
• 2
• 2

LVL 86

Expert Comment

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
for %%a in ("%Source%\%Mask%") do (
echo Processing %%~nxa ...
>"%Target%\%%~na.txt" echo %%~nxa
)

0

Author Comment

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 86

Accepted Solution

oBdA earned 2000 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

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

## Featured Post

Question has a verified solution.

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

This is an article on how to answer questions, earn points and become an expert.
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
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…
###### Suggested Courses
Course of the Month8 days, 21 hours left to enroll