?
Solved

Renaming Text Files using Batch File

Posted on 2011-03-14
5
Medium Priority
?
554 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
[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
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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

800 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