Question:
Batch File to: Rename each file based on a text in each file.?
Jameel
2011-03-24 13:51:41 UTC
Hello,

A batch file to read through all files in a directory:
Rename each file based on a text in each file. Problem is this text is a date in this format: mm/dd/yy.
The date is located in line 1 column 56 in each file. (Fixed location).
I would like the batch to rename each file in this format: mm-dd-yy since the ‘/’ won’t be accepted as part of the file name.
All files renamed in the same directory.

Thanks for your help
Three answers:
Aacini
2011-03-24 17:37:29 UTC
There are two missing details in your question:



- If the batch file will rename ALL the files in the directory, it will also rename the batch file itself, unless you start it in other directory; in such a case, the batch file must start with a CD command to enter to the working directory.



- You don't said if the renamed files must retain its extensions, so I supposed Yes. If the files to rename have a fixed extension (.TXT for example) you must change *.* by *.TXT, eliminate the SET EXT= line and use .TXT instead of !EXT!. In this case the previous detail don't apply.



This batch file do what you need:



@echo off

setlocal enabledelayedexpansion

for %%f in (*.) do (

set /p firstline=< %%f

set thedate=!firstline:~55,8!

set name=!thedate:/=-!

echo ren %%f !name!.txt

)



You must first test that this program show the right ren command and then eliminate the "echo " part.



For further details type SET /? and FOR /?



Regards...



=======================================



UPDATE:



I had modified the batch file so it is ready now to take files with no extension and rename they to .TXT files. To use it you just need to copy it to your disk and execute it, so, why not to test it?
JoelKatz
2011-03-24 20:54:52 UTC
find . -type f | while read FNM; do mv ${FNM} `head -1 ${FNM} | cut -b 56-64`; done



This should work in bash. If on Windows, bash is part of Cygwin.



Before actually running this, you may wish to test it. Replace "mv" with "echo mv" and it will show you the commands it would execute. The '56-64' specifies bytes 56 through 64 of the first line of the file. If you get the wrong piece, you may need to adjust the first and second numbers.
Yahgoogle
2011-03-25 04:09:41 UTC
You can use software "Replace Pioneer" to batch rename your files like that, the procedures:



Assume each column is separated by space or tab.

1. open "Tools->Batch Runner" menu, and drag multiple files from Windows to "Batch Runner" window

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

${FILENAME}.${EXT}

Note: Use '(?:\S+\s+){55}(\S+)' to replace 'XXX' in above expressions

3. check the preview at output file column, and click "File Rename".



Here's link of 66 ways to rename multiple text/data/mp3/image files automatically with "replace pioneer"


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