Solved

Batch to move files to different UNC path...

Posted on 2013-01-22
9
857 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 28

Expert Comment

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

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 51

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

758 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