Solved

Batch to move files to different UNC path...

Posted on 2013-01-22
9
863 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 52

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
 
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
Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now