Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch to move files to different UNC path...

Posted on 2013-01-22
9
Medium Priority
?
882 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 57

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 85

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
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). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

705 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