Loop thru directory and rename files

October 12, 2010 at 08:43:29
Specs: Windows 2003 Server
Hi, I'm trying to write a script file and looking for some help. I have a directory that has 3 text files in it. I need to loop thru the directory, and for each file append today's date to the file name. For example, I have file W800.txt. I want to rename this: W800_10122010.txt or some other acceptable version of the date. I can do individual files but can't figure out how to loop thru the directory and rename multiple files. The code I'm using is:

set SourceFileLocation=C:\Autos\
FOR %%T in (%SourceFileLocation%*.txt) DO REN %%T "_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%.txt"

The script is going to be called by another procedure, so we don't want to manually enter in anything to execute.

All this code does is renames the first file in the directory with just the date and then errors out "A duplicate file name exists or the file cannot be found". Any help would be greatly appreciated!!!


See More: Loop thru directory and rename files

Report •


#1
October 12, 2010 at 10:36:38
set SourceFileLocation=C:\Autos\
FOR %%T in (%SourceFileLocation%*.txt) DO REN "%%T" "%%~nT_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%.NEW"
del *.txt
ren *.new *.txt


Report •

#2
October 12, 2010 at 10:43:11
Thanks much for the reply! That seems to be working. Another question if I may....how would I add code in after the renaming to:
1. Move the files to another location
2. Create a new "dummy" or shell file

I almost have it but my looping isn't working very well.


set SourceFileLocation=C:\Autos\
FOR %%T in (%SourceFileLocation%*.txt) DO REN "%%T" "%%~nT_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%.NEW"
del *.txt
ren *.new *.txt

I added in:

rem Moving files
FOR %%G IN (%SourceFileLocation%"*.txt") DO MOVE %%G %DestinationFileLocation%

rem making files
echo 2>%SourceFileLocation%Car1.txt
echo 2>%SourceFileLocation%Car2.txt
echo 2>%SourceFileLocation%Car3.txt




Report •

#3
October 12, 2010 at 12:05:31
Afyer I posted my hint I noticed some minor bugs and code optimizations so see the following, more readable in my opinion.

About making files please explain better what you want to achieve since I admit I don't understand.

set SourceFileLocation=C:\Autos\
set TimeStamp=%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%_%time:~3,2%_%time:~6,5%

pushd %SourceFileLocation%
for %%T in (*.txt) do ren "%%T" "%%~nT_%TimeStamp%.new"
ren *.new *.txt

move *.txt "%DestinationFileLocation%"
popd


Report •

Related Solutions

#4
October 12, 2010 at 18:21:50
I got it working! Here's my code. Thanks for the assist, it made the difference!

set SourceFileLocation=C:\Autos
set DestinationFileLocation=C:\Autos\Archive

echo Echoing Parameters that are passed IN
echo Source:%SourceFileLocation%
echo Destination:%DestinationFileLocation%


rem :Rename
FOR %%T in (%SourceFileLocation%*.txt) DO REN "%%T" "%%~nT_%date:~10,4%_%date:~4,2%_%date:~7,2%_%.txt"


rem Moving files
FOR %%G IN (%SourceFileLocation%"*.txt") DO MOVE %%G %DestinationFileLocation%

rem Making dummy files
echo 2>%SourceFileLocation%Ford.txt
echo 2>%SourceFileLocation%Dodge.txt
echo 2>%SourceFileLocation%GM.txt


Report •

Ask Question