Solved

Batch to move files to different UNC path...

Posted on 2013-01-22
9
869 Views
Last Modified: 2013-01-23
Hello experts,  

I need Script that to move files to different Folders....  It could be batch or vbs, all I need is somethings that works well.  I would prefer something that would read the first three files and move files to the locations individual server.

I have the following files as example....

MHG ICU BKUP SCHED.DOC
MHG CCU BKUP SCHED.PDF
ELA ER BKUP SCHED.DOC
ELA ICU BKUP SCHED.DOC
CHHP ICU BKUP SCHED.DOC
CHHP CCU BKUP SCHED.DOC
CPH ER BKUP SCHED.DOC
CPH PHI BKUP SCHED.DOC

The script should get the first three letter of the files and copy them to the following
MHG*.* TO \\10.X.X.20\emarbackup$
ELA*.* to \\10.X.X.15\emarbackup$
CHH*.* to \\10.X.X.30\emarbackup$
CPH*.* to \\10.X.X.35\emarbackup$



I've used the following VBS to move the files, but doesn't copy to different servers and when there is an file with the 2 leading characters like "PP EMAR BACKUP.DOC" I get an error message and it stops working.

strSourceFolder = "C:\Temp\EMARTEST"

If Right(strSourceFolder, 1) <> "\" Then strSourceFolder = strSourceFolder & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objSource = fso.GetFolder(strSourceFolder)
For Each oFile In objSource.Files
    strFirst3 = Left(oFile.Name, 3)
    If Not fso.FolderExists(strSourceFolder & strFirst3) Then
        fso.CreateFolder strSourceFolder & strFirst3
    End If
    fso.MoveFile strSourceFolder & oFile.Name, strSourceFolder & strFirst3 & "\"
Next 

Open in new window

0
Comment
Question by:epicazo
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 38808220
Are you open to using powershell to do this  ?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 38808228
How do you determine the IP address from the leading 3 characters?

~bp
0
 

Author Comment

by:epicazo
ID: 38808661
I never used PS, but may consider if its something that does the job.  

the leading 3 char are unique and represent our four hospitals so the prefix would always be CHH,CPH,ELA, and MHG -- unless we add a fifth hospital.  I've seen scripts that look into a text file to get variables (e.g. IP numbers) and move files based on that.
0
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 38808829
As a proof of concept, try the batch script below. It's currently in test mode and will only display the robocopy commands it would normally run. Remove the uppercase ECHO in front of line 12 to run it for real.
@echo off
setlocal enabledelayedexpansion
set Source=C:\Temp\EMARTEST
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[BACKUPDATA]"') do set DataStart=%%a
set /a TypeCount = 0
for /f "tokens=1* skip=%DataStart% delims= " %%a in ('type "%~f0"') do (
	set /a TypeCount += 1
	set SourceType[!TypeCount!]=%%a
	set Target[!TypeCount!]=%%b
)
for /l %%i in (1, 1, %TypeCount%) do (
	ECHO robocopy.exe "%Source%" "!Target[%%i]!" !SourceType[%%i]! /mov /r:2 /w:1
)

goto :eof
[BACKUPDATA]
MHG*.* \\10.X.X.20\emarbackup$
ELA*.* \\10.X.X.15\emarbackup$
CHH*.* \\10.X.X.30\emarbackup$
CPH*.* \\10.X.X.35\emarbackup$

Open in new window

robocopy.exe is a default part of Windows since Vista/W2k8, for W2k3, it's part of the W2k3 Resource Kit; it's way better and more reliable than the copy/move/xcopy commands.
Windows Server 2003 Resource Kit Tools
http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en
0
 
LVL 13

Expert Comment

by:Gabriel Clifton
ID: 38810114
for %%a in (*) do set filenm=%%a
if /i %filenm:~0,3%==MHG move "%filenm%" "\\10.X.X.20\emarbackup$\%filenm%" /y
if /i %filenm:~0,3%==ELA move "%filenm%" "\\10.X.X.15\emarbackup$\%filenm%" /y
if /i %filenm:~0,3%==CHH move "%filenm%" "\\10.X.X.30\emarbackup$\%filenm%" /y
if /i %filenm:~0,3%==CPH move "%filenm%" "\\10.X.X.35\emarbackup$\%filenm%" /y
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 38811296
In a BAT script, isn't this as simple as:

@echo off
move MHG*.* \\10.X.X.20\emarbackup$
move ELA*.* \\10.X.X.15\emarbackup$
move CHH*.* \\10.X.X.30\emarbackup$
move CPH*.* \\10.X.X.35\emarbackup$

Open in new window

~bp
0
 

Author Comment

by:epicazo
ID: 38811572
ObDa,

I tested your batch and it seems to work, however, what happens if the file already exists but  the target is of an older date?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 38811591
You can determine that with the robocopy switches.
By default, robocopy will not copy files that already exist in the same version (same date/time and size) in the target folder.
It will overwrite the target if the source file differs in any way.
/XO (eXclude Older) will prevent older source files of copying over newer target files; accordingly, /XN (eXclude Newer) would prevent newer source files from copying over older target files.
0
 

Author Closing Comment

by:epicazo
ID: 38812739
awesome!!  

Thanks
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question