Computing.Net > Forums > Windows Server 2003 > xcopy batch command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

xcopy batch command

Reply to Message Icon

Name: under_score
Date: October 23, 2008 at 09:17:27 Pacific
OS: server 2003
CPU/Ram: yes
Manufacturer/Model: dell
Comment:

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.


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: itguru
Date: October 23, 2008 at 21:20:31 Pacific
Reply:

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:

http://www.ss64.com/nt
http://www.ss64.com/nt/syntax.html


Report Offensive Follow Up For Removal

Response Number 2
Name: under_score
Date: October 24, 2008 at 06:47:02 Pacific
Reply:

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

Response Number 3
Name: Curt R
Date: October 24, 2008 at 07:16:45 Pacific
Reply:

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

Response Number 4
Name: itguru
Date: October 24, 2008 at 07:34:06 Pacific

Response Number 5
Name: under_score
Date: October 24, 2008 at 07:44:21 Pacific
Reply:

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

Related Posts

See More



Response Number 6
Name: jefro
Date: October 24, 2008 at 14:13:49 Pacific
Reply:

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

Response Number 7
Name: Mechanix2Go
Date: October 25, 2008 at 04:27:44 Pacific
Reply:

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 EnableDelayedExpansion

for /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

Response Number 8
Name: under_score
Date: October 28, 2008 at 05:52:11 Pacific
Reply:

Thanks for the input M2G, I'll give it a try and let you know.

Tim


Report Offensive Follow Up For Removal

Response Number 9
Name: under_score
Date: October 29, 2008 at 10:26:04 Pacific
Reply:

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 EnableDelayedExpansion

for /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

Response Number 10
Name: Mechanix2Go
Date: October 29, 2008 at 12:40:30 Pacific
Reply:

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

Response Number 11
Name: under_score
Date: October 29, 2008 at 13:07:57 Pacific
Reply:

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

Response Number 12
Name: Mechanix2Go
Date: October 29, 2008 at 13:24:31 Pacific
Reply:

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 EnableDelayedExpansion

if 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

Response Number 13
Name: under_score
Date: October 31, 2008 at 08:37:57 Pacific
Reply:

Okay, I'm having a slight problem. This code:

@echo off
setLocal EnableDelayedExpansion

for /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

Response Number 14
Name: Mechanix2Go
Date: November 14, 2008 at 12:17:06 Pacific
Reply:

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
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 Windows Server 2003 Forum Home


Sponsored links

Ads by Google


Results for: xcopy batch command

starting file batch www.computing.net/answers/windows-2003/starting-file-batch/3461.html

New to Windows 2003 Server www.computing.net/answers/windows-2003/new-to-windows-2003-server/7542.html

FTP UTF8 issue www.computing.net/answers/windows-2003/ftp-utf8-issue/4808.html