Question:
How do I create a batch file to replace the first 5 letters on a filename?
anonymous
2013-05-01 21:25:12 UTC
I am trying to create a batch file that can auto run and rename files that are downloaded into our system each week. Here's an example:

Each week the files download like this:
ONCTR-MMDDYY.log
I need them to rename to read:
12-MMDDYY.log

Obviously MMDDYY is the date in which the file will be used "051313" would be May 13th, 2013.

Is there a way to do this? The rename codes I have found and tried only replace the 1st 2 letters with the "12" and nothing else, leaving me with "12CTR-MMDDYY.log" Our system will not recognize that and I want to make this as automated as possible.

Any help you can give me would be great!!!! Thanks!
Three answers:
Jake
2013-05-03 09:44:41 UTC
echo off



REM BATCH RENAME:

REM ONCTR-MMDDYY.log

REM TO:

REM 12-MMDDYY.log



set fileType=c:\users\username\*.log

REM ONLY MASS RENAME THE FILES IN THIS DIRECTORY WITH THIS EXTENSION



set newPrefix=12

REM THE NEW PREFIX IS SUPPOSED TO BE 12



set delimiter=-

REM THE PART BETWEEN THE ONCTR AND THE MMDDYY.LOG IS THE DELIMITER

REM IN OUR CASE, IT'S A HYPHEN (-)



for /f "Tokens=1,2 delims=%delimiter%" %%a in ('dir /b "%filetype%"') do (

ren "%%a%delimiter%%%b" "%newPrefix%%delimiter%%%b"

)
Yahgoogle
2013-05-03 08:04:57 UTC
Application "Replace Pioneer" is highly recommended, supporting flexible batch file rename, following is procedures for your reference:



1. Launch Replace Pioneer and open "Tools->Batch Runner" menu

2. Drag all you files from windows file browser to "batch runner" window

3. check option of "set output filename", and change the following entry to:

${FILENAME}{replace,'ONCTR','12'}

or

${FILENAME}{replace,'^.*?-','12-'}

4. click "File Rename", done.



Here are 66 other ways to rename multiple files automatically
Darsh
2013-05-02 06:07:55 UTC
The following links contain walkthroughs on how to find a file, rename it, only effect some of the name and get the current date. You can use this knowledge to accomplish your goal in a way such as this:



Step 1. Get file name into a variable

Step 2. Get last four characters (file extension, i.e. .log)

Step 3. Get date into a variable

Step 4. Replace name with date

Step 5. Add your number to the name (can vary these two steps)

Step 6. Re-add file extension to variable

Step 7. Actually rename file with finalized variable



http://stackoverflow.com/questions/1192476/windows-batch-script-format-date-and-time

http://stackoverflow.com/questions/13518755/find-file-name-from-string-in-batch-program

http://www.tech-recipes.com/rx/630/using-variables-in-windows-batch-files/

http://en.kioskea.net/forum/affich-20320-dos-script-to-rename-files

http://www.dostips.com/DtTipsStringManipulation.php

http://superuser.com/questions/111850/any-way-to-get-the-first-few-characters-of-filename-in-dos-batch-file-programmin


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...