Solved

Renaming Text Files using Batch File

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
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…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

932 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

8 Experts available now in Live!

Get 1:1 Help Now