Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm new to this site so forgive me if this post is not in the correct place, it was a tossup between here and the dos forum.
My question is related to the xcopy command for a batch file I'm trying to create. I have a folder with hundreds of files which have been created over the last year that I need to rename and place in another location. The current naming convention is xyzlogdate.xls and I have a script written to xcopy xyz*.* them to the new location as xyz.timestamp.xls which is causing each one to be overwritten as it appears the copy command gives all of the files the same timestamp. I was relying on the time stamp to make the file name unique. I'm currently using a for statement. Is there a way using xcopy to make the files unique given that my time stamp idea is not working?
Here is an example of my script:
for /f "tokens-1-5 delims=:" %%d in ("%time%") do ECHO F|xcopy /y E:\reports\share\xyz*.* N:\reports\share\xyz.%%d%%e%%f.xls
Can anyone help? I'd like to use xcopy rather than some third party utility that would have to be loaded.
Well the MS-DOS Forum is a no no as W2K3 has no MS-DOS, being related to OS/2|NT4.
All the NT Family have CMD.exe (NT Command Prompt) which has a similar look and feel to MS-DOS COMMAND.COM, but then so does UNIX.
Maybe ask the moderator to move your post to the Programming Forum ??
Also as W2K3 is similar to XP the following website may give some pointers:
Report Offensive Follow Up For Removal
Thanks for the feedback but a command list really isn't that helpful. What I need is some insight into the structre of the FOR loop.
I do appreciate the help though.
Report Offensive Follow Up For Removal
There's a gent who hangs out on here and helps people who is the best batchfiler I know. If anybody can help you, he can.
His name on this site is: Mechanix2Go
You can message him directly through the private message function (under the "My Computing.Net" link).
I suggest you ask him for help as I feel quite certain, if what you want to do can be done, he's the man to help you write the batchfile to do it.
Report Offensive Follow Up For Removal
Curt R, I pm'd mechanix and asked him to take a look. We'll see what he says. itguru, what I would like to do is embed multiple do statements in a single for statement. For instance(in pseudo)
For files do count+1 and xcopy files xyz*.* as xyz.timestamp.count.xls.
It seems like that would loop through all of the files, add 1 to a counter for each file and rename it with the time stamp and the count. However I get too many parameters error when I try to do multiple do's. Seems like there is probably a way to do this but I've been searching the internet for a couple of days now and cannot find anything. The problem is that without the counter it just keeps overwriting the files because it appears to use the same time stamp on all of them like it is only grabbing them once of it is doing it so quickly that miliseconds are too long to make it unique.
Report Offensive Follow Up For Removal
See also robocopy.
"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10
Report Offensive Follow Up For Removal
xcopy is a very cool tool, but not what's needed for this.
The script below will copy to the dest directory, renaming sequentially.
This will work ONCE. If you need to run more than once and have the bat "figure out where it left off" it'll take a bit more doing.
============================================
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set /a N+=1
echo copy %%a f:\dest\%%~Na!N!%%~Xa
)
=====================================
If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
Thanks for the input M2G, I'll give it a try and let you know.
Tim
Report Offensive Follow Up For Removal
M2,
First off thanks for the help, it was extremely helpful and helped me solve the problem I had.
The code did not really work as expected. It went through and did the sequential rename and acted like it copied the files over but for some reason it did not.
I then changed the code to read:
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set /a N+=1
echo F|xcopy /y %%a n:\backup\%%~Na!N!%%~Xa
)After making the modification in bold it copied and moved the file while renaming the files successfully. Since I would like to understand what you did with your script would you mind describing in detail what your commands are doing? Perhaps I didn't change something that I needed to.
Thanks again to M2 and everyone else who responded to my question!
Report Offensive Follow Up For Removal
OOPS, I should have said that to activate the bat, you need to edit out the ECHO before COPY.
I'm working on one which will figure out where it left off.
=====================================
If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
ahhh, now that makes sense. I'm interested to see the script for the one that picks up where it left off. However, I don't really need it because I have one that I run nightly that moves files from point a to point b and there won't be duplicates any more because it only looks for ones created that day.
I think you should go ahead and post it though because I think this could help others looking for that solution.
Prior to posting here I searched the internet for several days looking for a solution so I think this thread can help a lot of people.
Thanks,
Tim
Report Offensive Follow Up For Removal
Note that this keeps track of last num used by writing it to %temp%\# , so in the unlikely event you already have such a file, delete it.
============================================
:: copy to dest, appending 5 digit seq num to filename@echo off
setLocal EnableDelayedExpansionif exist %temp%\# set /p N=<%temp%\#
for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set /a N+=1
call :sub1
copy %%a f:\dest\%%~Na!S!%%~Xa > nul
)echo !N!> %temp%\#
goto :eof:sub1 pad to 5 places
set S=!N!
:pad
if !S:~4^,1!'==' set S=0!S!& goto :pad
goto :eof
=====================================
If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
Okay, I'm having a slight problem. This code:
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set /a N+=1
echo F|xcopy /y %%a n:\backup\%%~Na!N!%%~Xa
)copies the file and appends the sequence number. However, I want to sort through file names and also rename them in addition to adding the sequence number. For instance, lets say I have a folder full of files named abc.timestamp.xls, def.timestamp.xls, and xyz.timestamp.xls. I only want to copy xyz.timestamp.xls and I want to copy it over as xyza.sequencenumber.xls. It seems there should be a way to do this but I don't understand the code well enough. What is this code doing? %%~Na!N!%%~Xa
Thanks,
Tim
Report Offensive Follow Up For Removal
What is this code doing? %%~Na!N!%%~Xa
filename sequential_number extension
=====================================
If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

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