Solved

Renaming Text Files using Batch File

Posted on 2011-03-14
5
534 Views
Last Modified: 2012-05-11
I'm trying to write a batch file that renames text files in a specific folder to specified name.

Ex. datafile03102011.txt --> datafile.txt
      datafile03112011.txt --> datafile.txt
      datafile03122011.txt --> datafile.txt

The reason why I need to do this is because I want to automatically upload delimited text files into MS Access and that is hard when the file name is constantly changing. It's an easy task if the file name stays constant so I want to write a .bat file to automatically convert these files to the same file name.

Any help would be greatly appreciated.
Thanks.

lleblanc65
0
Comment
Question by:lleblanc65
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35132711
simply

ren datafile*.txt datafile.txt

Open in new window

0
 
LVL 3

Expert Comment

by:babesia
ID: 35132867
if you dont known name of the file  and there only one txt file in each folder

FOR /f "tokens=1* delims=;" %%i in ('dir <folder path>\*.txt ') DO
(
ECHO    ren %%i datafile.txt

 )
0
 
LVL 19

Expert Comment

by:Thommy
ID: 35132952
Set variable TheFolder to your specific directory, which contains the txt files

Replace line with your Access Upload command
@echo off
Set TheFolder=c:\TxtDir
for /f %%f in ('dir %TheFolder%\*.txt /B') do (
  ren %%f datafile.txt
  echo "upload datafile.txt to ACCESS"
  REM Replace this line with your Access Upload Command
)

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 125 total points
ID: 35132953
ren takes wildcards, so you don't need a FOR loop.

It does not force replacement of an existing file, so usually the sequence is this:

if EXIST datafile.txt del datafile.txt
ren datafile*.txt datafile.txt

Open in new window

0
 

Author Closing Comment

by:lleblanc65
ID: 35137629
Thanks for the great help. I had no idea it was that simple.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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