Solved

Renaming Text Files using Batch File

Posted on 2011-03-14
5
526 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to dynamically set the form action using jQuery.

803 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