Solved

# XCOPY or FORFILES

Posted on 2005-04-26
Medium Priority
4,275 Views
Hi there.  I simply need to copy a file from a directory on one machine to a directory on another box for processing.  We receive these files daily except for Saturday.  There are many other files in the source directory, and I'm unsure how to specify that I get only the most recent file.  I assumed I'd just use either xcopy or forfiles within a .bat file and then run it w/windows scheduled tasks.  Again, my problem is I don't know how to tell it to retrieve the most recently dated file.  For example, right now it would copy \\server1\bb\filename_04252005 to \\server2\g$\mssql\tools\filename_04252005. Can anybody tell me the best way to do this? 0 Question by:RLLewis • 15 • 8 • 2 • +3 28 Comments LVL 16 Expert Comment ID: 13866401 Maybe this link might help. http://www.ericphelps.com/batch/samples/recent.txt I know this doesn't address the xcopy or forfiles, but would it be a solution to just map the server path as a drive and save it's info there? Just an idea. net use h:\ "\\server2\g$\mssql\tools\"
0

LVL 1

Author Comment

ID: 13866576
i'm a bit puzzled on this one, still having a hard time doing the copy. i will let you know as i progress
0

LVL 30

Expert Comment

ID: 13867230
You could maintain a date stamp on the destination directory and utilize XCOPY's ability to copy files modified on or after that date. The code would look something like this:

@echo off

setlocal

set sDir=\\server1\bb\
set dDir=\\server2\g$\mssql\tools\ set cutoffParm= for /F "delims=" %%a in ('type "%dDir%lastStamp.txt" 2^>NUL') do set cutoffParm=/D:%%a echo xcopy %cutoffParm% "%sDir%*.*" "%dDir%*.*" for /F "tokens=2-4 delims=/ " %%a in ('date /t') do echo %%a-%%b-%%c>"%dDir%lastStamp.txt" I have placed an echo statement prior to the xcopy for testing. Run the test twice to see how the date parameter is being utililized. If the process works you'll want to delete the lastStamp.txt file on your destination directory and remove the echo. Good Luck, Steve 0 LVL 25 Expert Comment ID: 13867254 You can use the /D switch with XCOPY to copy the most recent files. For example: @echo off net use h:\ "\\server2\g$\mssql\tools\"
set fileToCopy=\\server1\bb\filename_04252005
xcopy "%fileToCopy%" /d /y

Hope that helps. :-)
0

LVL 25

Expert Comment

ID: 13867261
Sorry, that's incomplete, this is what I ment instead:

@echo off
net use h:\ "\\server2\g$\mssql\tools\" set fileToCopy=\\server1\bb\filename_04252005 xcopy "%fileToCopy%" "h:\" /d /y 0 LVL 1 Author Comment ID: 13867749 all of this is very helpful, and i do really appreciate it. i'm still struggling, though, as every bit of this expects a filename, except for the first trip to eric phelps site, which get's the most recently created file. see, i'd be getting a new file daily -- there are TONS of other files in my source directory, i just want to go in for the most recent file of this type, copy it locally, load it to sql and begin my processing. i did use this to find the most recent file, but i'm as of yet unable to copy as needed: @echo off for /f "delims=" %%x in ('dir /od /a-d /b *.*') do set recent=%%x echo %recent% 0 LVL 30 Expert Comment ID: 13867935 If that's all you need to do then why not just do this: @echo off for /f "delims=" %%x in ('dir /od /a-d /b *.*') do set recent=%%x if not "%recent%" xcopy "%recent%" "\\server2\g$\mssql\tools"

0

LVL 1

Author Comment

ID: 13868052
and what's the right way to dir to another directory to get the file, also using unc -- like what if i want to get the file from here:  server1\bb  ?
0

LVL 6

Expert Comment

ID: 13868078
basically what  you have posted works to find the oldest file correct???

So now you just need to copy.  Does a connection between the machines exist already or do you need to use a command such as net use?  Looks like InteractiveMind suggested that already but you may need to specify a password and then disconnect like

@echo off
for /f "delims=" %%x in ('dir /od /a-d /b *.*') do set recent=%%x
xcopy %recent% x:\
net use x: /delete

You could optionally use saved or existing credentials saving yourself from showing off your password but I just did this as and example.
0

LVL 1

Author Comment

ID: 13868502
does anybody have a good link to some windows cmd information?  i just keep getting more questions as i step through this.  for example, on the net use - lets assume i get to the directory i need to copy the file from, how do i tell it how to retrieve the most recent file when the file has no exention?  this is the file naming convention:  filname.out.20050425 -- i open it w/wordpad and/or notepad and ultimately, i load just fine into sql -- but how do i specify properly such that it retrieves this and not one of the other files there?
0

LVL 1

Author Comment

ID: 13868686
net used successfully, but xcopy failed --

lC:\MINE\Apps\CDW>equity_nam
Local name
Remote name       \\hdb-01\Bloomberg
Resource type     Disk
Status            OK
# Opens           8
# Connections     2
The command completed successfully.

xcopy was unexpected at this time.
0

LVL 1

Author Comment

ID: 13868836
alright, the xcopy is working now, though i need to have it do so silently such that it doesn't ask me whether or not i want to overwrite.  BUT, the net use is not working.  see here it suggests to me that it got the remote box 'hdb-01\bloomberg' -- but it's copying the most recent file found on my own local machine, it's not bringing anything local from the remote server.

C:equity_namr.bat
1 File(s) copied
C:\MINE\Apps\CDW>equity_namr
Local name
Remote name       \\hdb-01\Bloomberg
Resource type     Disk
Status            OK
# Opens           6
# Connections     2
The command completed successfully.

Overwrite \\sqlserverch03\g$\equities\equity_namr.bat (Yes/No/All)? n 0 File(s) copied C:\MINE\Apps\CDW> C:\MINE\Apps\CDW>equity_namr Local name Remote name \\hdb-01\Bloomberg Resource type Disk Status OK # Opens 6 # Connections 2 The command completed successfully. 0 LVL 30 Expert Comment ID: 13869192 Here are modifications to my code to suppress output/prompts: @echo off for /f "delims=" %%x in ('dir /od /a-d /b *.*') do set recent=%%x if not "%recent%" xcopy /Y "%recent%" "\\server2\g$\mssql\tools">NUL
0

LVL 23

Expert Comment

ID: 13871531
sorry, not had too much time to go thru this, but it looks like a candidate for robocopy.exe perhaps?

http://www.jsifaq.com/SUBB/tip0600/rh0609.htm
http://www.dynawell.com/support/ResKit/

it's been around forever, but it still works on recent OSs.
hth,
Danny
0

LVL 1

Author Comment

ID: 13874581
i will check it out, thx danny
0

LVL 1

Author Comment

ID: 13885006
still not happening.  i'm sorry - lemme re-state what it is i am needing to do.  all i need is to get the most recently created file from one server/directory, and copy it to another server/directory on a daily basis.  i have a dts job that will then pick it up and load/process the data.  i spend all my time on the sql side of things, i've not really done too many windows cmd/bat jobs.   at least not any exciting ones.  does anybody have any thoughts?
0

LVL 30

Expert Comment

ID: 13885312
I'm confused over your specifications. Let me outline what I think you want and you tell me if it is correct or incorrect.

1) You want to find the most recently changed file in the \\server1\bb\ directory and all it's subdirectories?

2) Then you want to copy the file to \\server2\g$\mssql\tools\ Is that correct? 0 LVL 1 Author Comment ID: 13885341 yes, except i don't care to traverse the subdirectories on \\server1\bb\. i just want the most recently created file in this dir copied to \\server2\g$\mssql\tools\
0

LVL 30

Expert Comment

ID: 13885666
Does this code work? If not why?.

@echo off

set recent=

for /f "delims=" %%x in ('dir /od /a-d /b "\\server1\bb\*.*"') do set recent=%%x

if not "%recent%"==""  xcopy "\\server1\bb\%recent%" "\\server2\g$\mssql\tools" 0 LVL 1 Author Comment ID: 13885797 perfect. let me ask you one more thing, steve. the source directory has a whole bunch of different files of diff dates and diff naming conventions. the one i care about is this one: filename.dif.20050427 i didn't realize until running this just now that the timing that each of the files is received varies a great deal. your code worked beautifully, but it brought me this one: filename.rpx.20050427 how could i include the 'dif' in the for /f such that it only brings me the most recent dif file? 0 LVL 30 Accepted Solution SteveGTR earned 500 total points ID: 13885938 Try this: @echo off set recent= for /f "delims=" %%x in ('dir /od /a-d /b "\\server1\bb\*.*" ^| findstr ".dif."') do set recent=%%x if not "%recent%"=="" xcopy "\\server1\bb\%recent%" "\\server2\g$\mssql\tools"
0

LVL 1

Author Comment

ID: 13886041
beauty.  that is exactly what i'm looking for.  thank you very much, steve
0

LVL 1

Author Comment

ID: 13959668
Hey steve one question for you on this --- lets say we haven't run for four or five days, so maybe there's a handful of files sitting there for retrieval, OR, more likely, somebody has dropped several files in on the same day.  so, the last time it's run, i've picked up filname.cax.20050505, but now I've got to go in there and get filname.cax.20050506, filname.cax.20050507, filname.cax.20050508 and filname.cax.20050509.  is this a real pain in the butt?  just to be able to retrieve multiple files from the last one retrieved?
0

LVL 30

Expert Comment

ID: 13959736
One way to do this would be to create a file with a timestamp of that last time the process was run. Then you'd have to compare the suffixes of the files (encoded as a date) against the timestamp and collect all files that are great than that. When you were finished, you'd update the timestamp value.

You could run into a problem with this however because of the timing of when files are dropped and the process was run. If you ran the process at 6PM and files were dropped after that time, you'd miss the files because the time dates would be the same, not greater. You could get around this by collecting files greater than or equal to the time stamp or by maybe using the date/time of the file instead of the encoded date value.

It would be involved, but doable.
0

LVL 1

Author Comment

ID: 13959847
what do you mean by 'it would be involved, by not doable'
you know, maybe i should open another question, steve, i just was really hoping to hear from you on this as we've gone down this road just a bit before.  let me know if you'd like me to open another, and please let me know how i might be able to create that file w/the timestamp.
0

LVL 30

Expert Comment

ID: 13960066
I had to check my message to see if I'd made a major type... I didn't, but have before ;)

I said "It would be involved, but doable."

Yes, this would be a new question.
0

LVL 1

Author Comment

ID: 13960184
yep, don't know how i missed that.  i'm going to open another question, steve.  hopefully you can help me get through this one...
0

LVL 1

Author Comment

ID: 13961691
hi again steve.  i opened another question, but i believe i stuck it in sql area, instead of dos/windows.  i don't know how to change that yet, but i've asked for assistance.  http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21417604.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
###### Suggested Courses
Course of the Month16 days, 16 hours left to enroll