Solved

Batch to move files to different UNC path...

Posted on 2013-01-22
9
868 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 53

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 83

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 53

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 83

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

805 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