We help IT Professionals succeed at work.

Need a bat file to copy specific files from an autonumbered directory

electricd7
electricd7 used Ask the Experts™
on
Hello.

I have a process that creates a numbered directory in the path "C:\CSIRP\Storage\".  The number is generated automatically and is different each day.  For example today's path is "C:\CSIRP\Storage\68".  Inside that directory there are approximately 100 files.  I need a batch file that will look at the root directory, "C:\CSIRP\Storage\", find the highest numbered subdirectory, today will be 66 tomorrow will be 67.  Then once I find the correct directory, I need to copy 4 files to path "\\servername\sharename".  The 4 files I need are ALWAYS named:

**CF0364, **DMCIF, **LMCIF, and **NMCIF

Where ** is the same as the number of the directory.  The batch will be scheduled to run multiple times daily, so before it copies the files, it should check the "\\servername\sharename" path for the existance of the files, and abort if the files already exist (no need to copy them twice.)

Please help if you can.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
This is doable.  Will the number of the subfolder always be 2 digits or could it be 1, 2, 3 etc in length?

~bp

Author

Commented:
The number of digits for subfolders could grow to 4 digits, but probably not past.  
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Give this a try and see if it meets your needs.  It basically looks at all the folders contained in C:\CSIRP\Storage, selects the one with the latest creating date, and then copies the 4 files involved to the share.

~bp
@echo off
set BaseDir=C:\CSIRP\Storage
set DestDir=\\servername\sharename
for /F "tokens=*" %%A in ('dir /ad /od /tc /b "%BaseDir%"') do set NewDir=%%A
for %%A in (CF0364 DMCIF LMCIF NMCIF) do (
  if not exist "%DestDir%\%NewDir%%%A" copy "%BaseDir%\%NewDir%\%NewDir%%%A" "%DestDir%\%NewDir%%%A"
)

Open in new window

Author

Commented:
Almost.  I turned echo back on and it actually sets the NewDir variable = 3, when it should be 66?  I see it loop through and do a "set NewDir=" for each number sequentially, but it does 1, 2, 4, 5, ....65, 66, 3 for some reason.  The only one I see out of place is 3.  I don't quite understand the previous dir command to know why the 3 is listed out of order though?

Author

Commented:
I also think I told you wrong on the file names :(  The ** that preceeds the file name does not corespond to the current number listed in the directory.  I think it might be a day of the month number, but its definately not the same.  I think the best way to get the most current directory might be to list out the dir command by date modified and just take the newest one?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
You can try a few DIR commands at a prompt to see if we can find the right options to find the folder you are looking for.  you can see what the various options do by doing:

DIR /?

at a command prompt.  Here's what I used in my first effort:

dir /ad /od /tc /b "%BaseDir%"

/AD indicates we only want to list directories (not files)

/OD says to order the listing by DATE

/TC says to use the date created (rather than the default of date modified)

/B says to return just the names of the directories, not the dates, etc.  This option is helpful when processing the output of the DIR in a script.

Try the following at a command prompt and see if you get the right folder (newest) last.

dir /ad /od "C:\CSIRP\Storage"

If so then we could just remove the /TC from my prior script.

If not then we could actually process each folder name, and look for the one with the highest numeric value, not that hard to do, just a few more lines of code.  Let me know if we need to go that route.

~bp

Author

Commented:
Bingo...removing the /TC gets the most current folder.  Now I need to change the second part of the script to get the correct filenames as the numbers don't corespond as I told you they did (sorry).  The number preceeding the file appears to be arbitrary but it appears to always be 2 digits (though i am not 100 positive this is always the case.)

Author

Commented:
Yea it appears it is always a 2 digit number that preceeds the file name.  Its just not related to the directory name.  
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Are the files actually named **CF0364, **DMCIF, **LMCIF, and **NMCIF with no extension, and where the ** will vary?  But there will only be one of each of those in any given days folder, right?  SO once we know the folder to copy files from for today, then we could just copy any file matching those 4 patterns, right?

~bp

Author

Commented:
That is correct.  The numbers will vary from day to day on the filenames, but they will match for all 4 files (ie, all 4 files will be preceeded with a "02" for a given day).  They do not have a file extension.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Try this change to the copy part of the script.  It assumes the files have no extension currently.

~bp
for %%A in ("%BaseDir%\%NewDir%\??CF0364" "%BaseDir%\%NewDir%\??DMCIF" "%BaseDir%\%NewDir%\??LMCIF" "%BaseDir%\%NewDir%\??NMCIF") do (
  if not exist "%DestDir%\%%~nA" copy "%%~fA" "%DestDir%"
)

Open in new window

This is a bit long winded but looks to work. Using archive bits on the files after moving them so they are not caught again unless they change (although the 1st time it is run it will copy everything, or do a DIR command to take the archive bit off the whole lot, and another to add it to the latest directory)
As long as you don't mind all the files in the latest directory being copied this should work.
The next time it is ran in the day unless the any of the files in Storage have changed (hence resetting the archive bit) the copyme file will be empty and it will not copy.

@echo off
MKDIR c:\CSIRP\Temp
dir /s /a:a /b c:\CSIRP\Storage\ > c:\CSIRP\Temp\copyme.txt
FOR /F "tokens=*" %%a in (c:\CSIRP\Temp\copyme.txt) do xcopy /Y /M "%%a" \\servername\sharename)
Test your restores, not your backups...
Top Expert 2016
Commented:
Here's a slightly more compact version, that also checks the actual names of the subfolders looking for the one with the highest value.  You may want to stick with the current approach but if not this is an option.  It also uses a couple of PUSHD (CD) commands to reduce the wordiness of the code.

~bp
@echo off
setlocal EnableDelayedExpansion
set BaseDir=C:\CSIRP\Storage
set DestDir=\\servername\sharename
set NewDir=0
pushd "%BaseDir%"
REM for /F "tokens=*" %%A in ('dir /ad /od /tc /b') do set NewDir=%%A
for /D %%A in (*) do (
  if %%A GTR !NewDir! set NewDir=%%A
)
pushd "%BaseDir%\%NewDir%"
for %%A in ("??CF0364" "??DMCIF" "??LMCIF" "??NMCIF") do (
  if not exist "%DestDir%\%%~nA" copy "%%~nA" "%DestDir%"
)
popd
popd

Open in new window

Author

Commented:
bp:

I changed that bit, but it looks as if its still corelating the directory name to the file name.  Here is an excerpt from the copy loop:

C:\Documents and Settings\Administrator\Desktop>(if not exist "\\servername\sharename\66DMCIF" copy "C:\CSIRP\Storage\66\66DMC
IF" "\\servername\sharename\66DMCIF" )
The system cannot find the file specified.

In this example the directory is correct, but the file name is actually '29DMCIF' for that day.

Author

Commented:
bp:  Latest script works, but grabs the directory by largest number and not by modified date (even after removing the /TC in your dir listing).  We are sooooo close! :)
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
==> Latest script works, but grabs the directory by largest number and not by modified date (even after removing the /TC in your dir listing).  We are sooooo close! :)

I thought that was what we wanted, are you saying we were better off with the latest modified date folder, even though it's name may not be the largest number?

~bp

Author

Commented:
well if i go into a command prompt and do a pushd basedir and then do a dir /AD /OD/ B I get the following results:

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

This is actually correct.  72 is the latest folder and is the correct folder for this script at the moment.  However, when running the actual script, for some reason it chooses 100 as the %NewDir% variable.  I don't know why it does, but if I run it as written (after removing /TC from the dir command) it does a pushd to /100 instead of /72

Author

Commented:
OK just realized that the dir command was actually REM'd out.  I changed the script as follows and it appears to work.  Why was it REM out in the first place?

#@echo off
setlocal EnableDelayedExpansion
set BaseDir=\\CSISQLADMIN.boi.internal\c$\CSIRP\Storage
set DestDir=\\boifp1.boi.internal\c$\test
set NewDir=0
pushd "%BaseDir%"
for /F "tokens=*" %%A in ('dir /AD /OD /B') do set NewDir=%%A
REM for /D %%A in (*) do (if %%A GTR !NewDir! set NewDir=%%A)
pushd "%BaseDir%\%NewDir%"
for %%A in ("??CF0364" "??DMCIF" "??LMCIF" "??NMCIF") do (
  if not exist "%DestDir%\%%~nA" copy "%%~nA" "%DestDir%"
)
popd
popd

Open in new window

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I had left the old approach of just using a sorted DIR by date in the code when I added the find the largest approach, just in case you found one way worked better than the other.  Sounds like the date sorted approach is best in this case, so go with that.

~bp

Author

Commented:
Yep..working good.  Here's your points!
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Guys this is so simple you'll laugh, in this case it's a perfect place to be using RoBoCopy whether run from a batch file or command prompt this will do the trick:
On Windows Versions Vista or greater:
FOR %%A IN (CF0364, DMCIF,  LMCIF, NMCIF) DO Robocopy "C:\CSIRP\Storage" "\\Servername\ShareName" *%%A  /MAXAGE:1 /ZB /S /DCOPY:T /LOG+:"C:\CSIRP\CopyLogs\CopyLog.Log"  /R:2 /W:2 /XX /XJD

On Windows Versions Prior to Windows Vista:
FOR %%A IN (CF0364, DMCIF, LMCIF, NMCIF) DO Robocopy "C:\CSIRP\Storage"  "\\Servername\ShareName" *%%A   /MAXAGE:1 /ZB /S  /DCOPY:T /LOG+:"C:\CSIRP\CopyLogs\CopyLog.Log" /R:2  /W:2 /XX

I have it attached as a full batch file (which yes is not just as simple as this command) with logging and variables.  Enjoy.

PS  No need to check if the files exist, Robocopy does this for you.  Additionally it will be checking if they have changed allowing you to ensure your copy did not occur while the file was being formed and therefore woudl be incomplete.

::------------------------------------------------------------------------------------------------::
:: Script: Robocopy_Backup_Set-of-Files_From-Today.Bat                                            ::
:: Version: 1.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-05-13                                                                               ::
::                                                                                                ::
::                                                                                                ::
:: Desc: Copies Local System settings, and couments to a backup location on the HD where run from ::
::                                                                                                ::
::------------------------------------------------------------------------------------------------::
:Begin
	ECHO off

::------------------------------------------------------------------------------------------------::
:: This section Defines default values for most variables whcih may need to be changed.           ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
	SET ScriptName=RoboCopy_BKP_New-Files
	SET Preamble=%ScriptName% --
	ECHO %Preamble% Start Prep
	SET RDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
	SET RTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
	SET OSVer=NOMATCH
	SET RoBoVista=NO
::------------------------------------------------------------------------------------------------::
:: Directories used and the age of the file to look for.							              ::
::------------------------------------------------------------------------------------------------::
	SET FileAge=1
	SET SrcDrv=C:
	SET SrcShare=CSIRP\Storage
	SET DstShare=ShareName
	SET DstDrv=\\Servername
	
	:Start-Prep-LDIR
	SET LFName=%ScriptName%_%RDate%
	SET LFile=%LFName%.log
	SET LDir=%SrcDrv%\RobocopyLogs
	SET LExist=%LDir%\%LFile%
	SET LLoop=0
	:End-Prep-LDir
	
	:Start-Create-LDIR
		ECHO %Preamble% Checking For %LDir%
		IF EXIST %LDir% GOTO End-Create-LDir
		ECHO %Preamble% Creating %LDir%
		mkdir %LDir%
	:End-Create-LDir
	
	:Start-Prep-Log
		ECHO %Preamble% Checking For %LExist%
		IF EXIST %LExist% GOTO Start-LExist-Add
		Goto :End-Prep-Log
		:Start-LExist-Add
			SET /a LLoop=%LLoop%+1
			echo %LLoop%
			SET LFile=%LFName%_%LLoop%.log
			SET LExist=%LDir%\%LFile%
			GOTO Start-Prep-Log
		:End-LExist-Add
	:End-Prep-Log
	
:End-Prep

:Start-Begin-Log
	ECHO %Preamble% BEGIN Script on %Date% --
	ECHO %Preamble% >> %LDir%\%LFile%
	ECHO %Preamble% ------------------------------------ >> %LDir%\%LFile%
	ECHO %Preamble% BEGIN %ScriptName% Script on %Date%  >> %LDir%\%LFile%
	ECHO %Preamble% ------------------------------------ >> %LDir%\%LFile%
	ECHO %Preamble% >> %LDir%\%LFile%
:End-Begin-Log


::------------------------------------------------------------------------------------------------::
:: This section Determines if Robocopy is Already installed.						              ::
::------------------------------------------------------------------------------------------------::	
:Start-RoboInst
	SET RoboInst=NO
	ECHO %Preamble% Is RoboCopy Installed?
	ECHO %Preamble% Is RoboCopy Installed? >> %LDir%\%LFile%
	RoboCopy > "%IDir%\RoboCopy.txt"
	ECHO %Preamble% Loop
	ECHO %Preamble% Loop >> %LDir%\%LFile%
	FOR /F "tokens=1 delims==" %%R IN ('TYPE "%IDir%\RoboCopy.txt" ^| FIND /I "Robust File"') DO SET RoboInst=GO
	del "%IDir%\RoboCopy.txt"
	Echo %Preamble% RoboInst == %RoboInst%
	Echo %Preamble% RoboInst == %RoboInst% >> %LDir%\%LFile%
	IF RoboInst==NO Echo %Preamble% Aborting, Please install RoboCopy!
	IF RoboInst==NO Echo %Preamble% Aborting, Please install RoboCopy! >> %LDir%\%LFile%
	IF RoboInst==NO GOTO :EOF
:End-RoboInst


::------------------------------------------------------------------------------------------------::
:: This section finds the version of the operating system for use later in the script.            ::
::------------------------------------------------------------------------------------------------::
:Start-FIND-OS

	:Start-FIND-Ver
		
		FOR %%V IN (3.1, 3.5, 3.51, 4.0, 5.0, 5.1, 5.2, 6.0, 6.1) DO call :Start-Ver-Loop %%V
		GOTO End-Ver-Loop
		:Start-Ver-Loop
			If %OSVer% NEQ NOMATCH GOTO :EOF
			SET TVer=%1
			ECHO TVer = %TVer% >> %LDir%\%LFile%
			VER | FIND "%TVer%"
			IF ERRORLEVEL 1 GOTO :EOF
			IF %TVer% == 3.1 SET OSVer=NT%TVer%
			IF %TVer% == 3.5 SET OSVer=NT%TVer%
			IF %TVer% == 3.51 SET OSVer=NT%TVer%
			IF %TVer% == 4.0 SET OSVer=NT4
			IF %TVer% == 5.0 SET OSVer=2K
			IF %TVer% == 5.1 SET OSVer=XP
			IF %TVer% == 5.2 SET OSVer=2K3
			IF %TVer% == 6.0 SET OSVer=Vista
			IF %TVer% == 6.1 SET OSVer=7
			GOTO :EOF
			
		:End-Ver-Loop
		ECHO End-Ver-Loop >> %LDir%\%LFile%
		IF %OSVer% == Vista (SET RoBoVista=GO)  ELSE IF %OSVer% == 7 (SET RoBoVista=GO) ELSE (SET RoBoVista=NO)

	:End-Find-Ver
	
:End-FIND-OS


:Start-DoCopy
	REM Here we setup our Copy Loop
	ECHO %Preamble% Start-DoCopy
	ECHO %Preamble% Start-DoCopy >> %LDir%\%LFile%
	REM Below is the For Loop, %%A is the term pulled from the set to pass to the command loop
	REM  The terms are located within the open and close parenthesis, and quotes allow spaces to be ignored
	REM  If Quotes were nto used, the spaces would be seen as a delimiter just as commas are.
	FOR %%A IN (CF0364, DMCIF, LMCIF, NMCIF) DO call :Start-DoCopy-Loop %%A
	GOTO End-DoCopy
	:Start-DoCopy-Loop
		REM This is the command loop, everythign inside this loop will be preocessed as a separate batch file
		REM we use %~1 for the variable the Tilde says to strip quotes off the passed text XD
		ECHO %Preamble% Start-%~1
		ECHO %Preamble% Start-%~1 >> %LDir%\%LFile%
		IF %RoBoVista%==GO (Robocopy "%SrcDrv%\%SrcShare%" "%DstDrv%\%DstShare%" *%~1  /MAXAGE:%FileAge% /ZB /S /DCOPY:T /LOG+:%LDir%\%LFile% /TEE /NP /R:2 /W:2 /XX /XJD)
		IF %RoBoVista%==NO (Robocopy "%SrcDrv%\%SrcShare%" "%DstDrv%\%DstShare%" *%~1  /MAXAGE:%FileAge% /ZB /S /LOG+:%LDir%\%LFile% /TEE /NP /R:2 /W:2 /XX)
	:End-DoCopy-Loop
	GOTO :EOF
:End-DoCopy
ECHO %Preamble% End-DoCopy
ECHO %Preamble% End-DoCopy >> %LDir%\%LFile%

:End
	ECHO %Preamble% !!--!! Exiting %ScriptName% !!--!!
	ECHO %Preamble% !!--!! Exiting %ScriptName% !!--!! >> %LDir%\%LFile%

Open in new window

Author

Commented:
QCubed -  Thanks for the script.  This would work except you left out the part where it only copies files from today's folder.  This folder is a number inside of the basepath (c:\CSIRP\Storage\72 today for example.)  Eitherway, I have a working solution, but do appreciate your input as well.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
You're welcome, however, actually, I didn't forget your Newest Day requirement;

The /MAXAGE:1 switch in Robocopy says only to copy the files created within the last 24 hours.**

The /XX switch says to ignore all other directories.

=)


**You can make this ONLY files with TODAY's Date with a slight  modification to the script if that is more correct, it seemed like 24  hours was acceptable.  (see End)

 Also Note Because my script uses DCopy:T you preserve File and directory  times on the remote server (This may be unimportant to you)

Another nice feature of Robocopy is that if you want you could ignore that functionality and it just won't bother copying the files to the destination unless they are new in the first place..



It's funny because when I started writing the script for you  at  length the solution still hadn't been accepted however by the time I   posted my script it had already been closed. Ahh Well..



Here s how to change the Variable I set in the Batchfile for the max age to be only the file created TODAY instead of the file created in the last 24 hours if this more correct for what you're after:

Change: SET FileAge=1
TO: SET FileAge=%date:~10,4%-%date:~4,2%-%date:~7,2%

In the Single Line Batch Examples as I gave before you would simply do:

On Windows Versions Vista or greater:
FOR %%A IN  (CF0364, DMCIF,  LMCIF, NMCIF) DO Robocopy "C:\CSIRP\Storage"  "\\Servername\ShareName" *%%A  /MAXAGE:%date:~10,4%-%date:~4,2%-%date:~7,2% /ZB  /S /DCOPY:T /LOG+:"C:\CSIRP\CopyLogs\CopyLog.Log"   /R:2 /W:2 /XX /XJD
On Windows Versions Prior to Windows Vista:
FOR %%A IN (CF0364, DMCIF, LMCIF, NMCIF) DO  Robocopy  "C:\CSIRP\Storage"  "\\Servername\ShareName" *%%A   /MAXAGE:%date:~10,4%-%date:~4,2%-%date:~7,2% /ZB /S   /DCOPY:T /LOG+:"C:\CSIRP\CopyLogs\CopyLog.Log" /R:2  /W:2 /XX




::------------------------------------------------------------------------------------------------::
:: Script: Robocopy_Backup_Set-of-Files_From-Today.Bat                                            ::
:: Version: 1.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-05-13                                                                               ::
::                                                                                                ::
::                                                                                                ::
:: Desc: Copies Local System settings, and couments to a backup location on the HD where run from ::
::                                                                                                ::
::------------------------------------------------------------------------------------------------::
:Begin
	ECHO off

::------------------------------------------------------------------------------------------------::
:: This section Defines default values for most variables whcih may need to be changed.           ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
	SET ScriptName=RoboCopy_BKP_New-Files
	SET Preamble=%ScriptName% --
	ECHO %Preamble% Start Prep
	SET RDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
	SET RTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
	SET OSVer=NOMATCH
	SET RoBoVista=NO
::------------------------------------------------------------------------------------------------::
:: Directories used and the age of the file to look for.							              ::
::------------------------------------------------------------------------------------------------::
	SET FileAge=%date:~10,4%-%date:~4,2%-%date:~7,2%
	SET SrcDrv=C:
	SET SrcShare=CSIRP\Storage
	SET DstShare=ShareName
	SET DstDrv=\\Servername
	
	:Start-Prep-LDIR
	SET LFName=%ScriptName%_%RDate%
	SET LFile=%LFName%.log
	SET LDir=%SrcDrv%\RobocopyLogs
	SET LExist=%LDir%\%LFile%
	SET LLoop=0
	:End-Prep-LDir
	
	:Start-Create-LDIR
		ECHO %Preamble% Checking For %LDir%
		IF EXIST %LDir% GOTO End-Create-LDir
		ECHO %Preamble% Creating %LDir%
		mkdir %LDir%
	:End-Create-LDir
	
	:Start-Prep-Log
		ECHO %Preamble% Checking For %LExist%
		IF EXIST %LExist% GOTO Start-LExist-Add
		Goto :End-Prep-Log
		:Start-LExist-Add
			SET /a LLoop=%LLoop%+1
			echo %LLoop%
			SET LFile=%LFName%_%LLoop%.log
			SET LExist=%LDir%\%LFile%
			GOTO Start-Prep-Log
		:End-LExist-Add
	:End-Prep-Log
	
:End-Prep

:Start-Begin-Log
	ECHO %Preamble% BEGIN Script on %Date% --
	ECHO %Preamble% >> %LDir%\%LFile%
	ECHO %Preamble% ------------------------------------ >> %LDir%\%LFile%
	ECHO %Preamble% BEGIN %ScriptName% Script on %Date%  >> %LDir%\%LFile%
	ECHO %Preamble% ------------------------------------ >> %LDir%\%LFile%
	ECHO %Preamble% >> %LDir%\%LFile%
:End-Begin-Log


::------------------------------------------------------------------------------------------------::
:: This section Determines if Robocopy is Already installed.						              ::
::------------------------------------------------------------------------------------------------::	
:Start-RoboInst
	SET RoboInst=NO
	ECHO %Preamble% Is RoboCopy Installed?
	ECHO %Preamble% Is RoboCopy Installed? >> %LDir%\%LFile%
	RoboCopy > "%IDir%\RoboCopy.txt"
	ECHO %Preamble% Loop
	ECHO %Preamble% Loop >> %LDir%\%LFile%
	FOR /F "tokens=1 delims==" %%R IN ('TYPE "%IDir%\RoboCopy.txt" ^| FIND /I "Robust File"') DO SET RoboInst=GO
	del "%IDir%\RoboCopy.txt"
	Echo %Preamble% RoboInst == %RoboInst%
	Echo %Preamble% RoboInst == %RoboInst% >> %LDir%\%LFile%
	IF RoboInst==NO Echo %Preamble% Aborting, Please install RoboCopy!
	IF RoboInst==NO Echo %Preamble% Aborting, Please install RoboCopy! >> %LDir%\%LFile%
	IF RoboInst==NO GOTO :EOF
:End-RoboInst


::------------------------------------------------------------------------------------------------::
:: This section finds the version of the operating system for use later in the script.            ::
::------------------------------------------------------------------------------------------------::
:Start-FIND-OS

	:Start-FIND-Ver
		
		FOR %%V IN (3.1, 3.5, 3.51, 4.0, 5.0, 5.1, 5.2, 6.0, 6.1) DO call :Start-Ver-Loop %%V
		GOTO End-Ver-Loop
		:Start-Ver-Loop
			If %OSVer% NEQ NOMATCH GOTO :EOF
			SET TVer=%1
			ECHO TVer = %TVer% >> %LDir%\%LFile%
			VER | FIND "%TVer%"
			IF ERRORLEVEL 1 GOTO :EOF
			IF %TVer% == 3.1 SET OSVer=NT%TVer%
			IF %TVer% == 3.5 SET OSVer=NT%TVer%
			IF %TVer% == 3.51 SET OSVer=NT%TVer%
			IF %TVer% == 4.0 SET OSVer=NT4
			IF %TVer% == 5.0 SET OSVer=2K
			IF %TVer% == 5.1 SET OSVer=XP
			IF %TVer% == 5.2 SET OSVer=2K3
			IF %TVer% == 6.0 SET OSVer=Vista
			IF %TVer% == 6.1 SET OSVer=7
			GOTO :EOF
			
		:End-Ver-Loop
		ECHO End-Ver-Loop >> %LDir%\%LFile%
		IF %OSVer% == Vista (SET RoBoVista=GO)  ELSE IF %OSVer% == 7 (SET RoBoVista=GO) ELSE (SET RoBoVista=NO)

	:End-Find-Ver
	
:End-FIND-OS


:Start-DoCopy
	REM Here we setup our Copy Loop
	ECHO %Preamble% Start-DoCopy
	ECHO %Preamble% Start-DoCopy >> %LDir%\%LFile%
	REM Below is the For Loop, %%A is the term pulled from the set to pass to the command loop
	REM  The terms are located within the open and close parenthesis, and quotes allow spaces to be ignored
	REM  If Quotes were nto used, the spaces would be seen as a delimiter just as commas are.
	FOR %%A IN (CF0364, DMCIF, LMCIF, NMCIF) DO call :Start-DoCopy-Loop %%A
	GOTO End-DoCopy
	:Start-DoCopy-Loop
		REM This is the command loop, everythign inside this loop will be preocessed as a separate batch file
		REM we use %~1 for the variable the Tilde says to strip quotes off the passed text XD
		ECHO %Preamble% Start-%~1
		ECHO %Preamble% Start-%~1 >> %LDir%\%LFile%
		IF %RoBoVista%==GO (Robocopy "%SrcDrv%\%SrcShare%" "%DstDrv%\%DstShare%" *%~1  /MAXAGE:%FileAge% /ZB /S /DCOPY:T /LOG+:%LDir%\%LFile% /TEE /NP /R:2 /W:2 /XX /XJD)
		IF %RoBoVista%==NO (Robocopy "%SrcDrv%\%SrcShare%" "%DstDrv%\%DstShare%" *%~1  /MAXAGE:%FileAge% /ZB /S /LOG+:%LDir%\%LFile% /TEE /NP /R:2 /W:2 /XX)
	:End-DoCopy-Loop
	GOTO :EOF
:End-DoCopy
ECHO %Preamble% End-DoCopy
ECHO %Preamble% End-DoCopy >> %LDir%\%LFile%

:End
	ECHO %Preamble% !!--!! Exiting %ScriptName% !!--!!
	ECHO %Preamble% !!--!! Exiting %ScriptName% !!--!! >> %LDir%\%LFile%

Open in new window