Solved

Renaming Text Files using Batch File

Posted on 2011-03-14
5
506 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
Comment Utility
simply

ren datafile*.txt datafile.txt

Open in new window

0
 
LVL 3

Expert Comment

by:babesia
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the great help. I had no idea it was that simple.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now