Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello everyone,
I have been trying to create a Bat that will copy the newest file from directory
- G:\BO\Donnette\CCIEdits\Downloads
and rename it to newest. If anyone can help me do this that would be great.

@echo off
setLocal EnableDelayedExpansionpushd G:\BO\Donnette\CCIEdits\Downloads
for /f "tokens=* delims= " %%a in ('dir/b/od') do (
set newest=%%a
)
copy !newest! x:\mystuff\newest
=====================================
If at first you don't succeed, you're about average.M2

Thanks M2,
I am still abit of a noob to this Bat stuff
so im just curious do i just copy and paste
the code into notepad, save it as a .bat file
then double click on it to run it?

Ok I have ran the bat a few times now and it seems that it only copies the bat file to the new directory. I have made some .txt test files but it doesnt copy them over or rename them to newest.
Any further help on this issue would be great.

If you add the bat file to the directory it will become the newest file. I think it's supposed to be run externally e.g from a different folder.
You might also want to put double quotes around !newest!, you never know what dir /b will throw at you in the way of spaces.

Thanks for tip.
The Bat now copies the newest file from the directory I tell it, but still doesnt rename it as newest. This is the code im using right now (Thanks again to M2 for this.)
::== CCIEditRename.bat
@echo off
setLocal EnableDelayedExpansionpushd G:\BO\Donnette\CCIEdits\Downloads
for /f "tokens=* delims= " %%a in('dir/b/od') do (set newest=%%a)
copy "!newest!" G:\BO\Donnette\CCIEdits\Downloads\Newest
::==

The only thing likely to trip it up os if you have a directory named G:\BO\Donnette\CCIEdits\Downloads\Newest and it's copying into there with the original name.
=====================================
If at first you don't succeed, you're about average.M2

I have changed the directory folder newest to a different name and it stills doesnt rename the copied file to newest.

Ok I have solved the problem.
I forgot to add the file extenison at the end of the file name. So it would just copy the current name not rename it.
Thanks for all the help.
M2 you are the man.

Add ECHO at the beginning of the last line. Before COPY and post result.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |