Computing.Net > Forums > Programming > Copy newest file and rename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Copy newest file and rename

Reply to Message Icon

Name: ChrisM205
Date: October 8, 2008 at 06:41:29 Pacific
OS: Windows XP
CPU/Ram: 3.0 GHZ and 1 GB Ram
Product: Dell
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 8, 2008 at 07:12:12 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

pushd 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


0

Response Number 2
Name: ChrisM205
Date: October 8, 2008 at 07:18:03 Pacific
Reply:

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?


0

Response Number 3
Name: ChrisM205
Date: October 8, 2008 at 07:45:51 Pacific
Reply:

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.


0

Response Number 4
Name: Judago
Date: October 8, 2008 at 14:28:51 Pacific
Reply:

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.


0

Response Number 5
Name: ChrisM205
Date: October 9, 2008 at 08:14:35 Pacific
Reply:

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 EnableDelayedExpansion

pushd 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
::==


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: October 10, 2008 at 06:46:09 Pacific
Reply:

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


0

Response Number 7
Name: ChrisM205
Date: October 10, 2008 at 07:12:56 Pacific
Reply:


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


0

Response Number 8
Name: ChrisM205
Date: October 10, 2008 at 09:04:19 Pacific
Reply:

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.


0

Response Number 9
Name: Mechanix2Go
Date: October 10, 2008 at 12:38:50 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Copy newest file and rename

Copy and rename with a batch script www.computing.net/answers/programming/copy-and-rename-with-a-batch-script/14966.html

rename files and folder to lowercase with b www.computing.net/answers/programming/rename-files-and-folder-to-lowercase-with-b/19161.html

Copy Paste Files and Folder www.computing.net/answers/programming/copy-paste-files-and-folder/20150.html