Solved

Batch Script

Posted on 2013-12-10
10
300 Views
Last Modified: 2014-10-27
1) The first script should copy data from \\sportographystorage\sportography\SPEEDJOBS\XXXXXX\SCANS\ORDERS2 to E:\Sportography\archive which is older than 6 months. While copying we need to make sure directory structure post job number(XXXXX) should remain same.

2) The second script should scan E:\Sportography\archive and delete files which are older than 18 months.



Note:-- SCANS\ORDERS2 subfolders are not present in each and every XXXXXXXX, so we need to first check if these exists or not. If they are there then we need to copy the files.
0
Comment
Question by:CHAUDHARYAKSHAY
10 Comments
 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39718956
@ECHO OFF


REM Set locations

SETLOCAL



set "location=\\sportographystorage\sportography\SPEEDJOBS\"
for /r "%location%" /d %%d in (*) do (
  xcopy "%%d\SCANS\ORDERS2" "C:\Users\achau\Desktop\Transfer\%%d\SCANS\ORDERS2\" /s /c /d /e /h /i /r /y /d:06-01-2013
)
0
 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39718958
So far I am able to make this script but how to use date in xcopy to copy the files which are 6 months old
0
 
LVL 28

Assisted Solution

by:Bill Bach
Bill Bach earned 250 total points
ID: 39722594
I would recommend using ROBOCOPY for this, as it has options for handling files older than a specific date, and XCOPY does not.  RoboCopy is built into the newer operating systems, and is downloadable from Microsoft for older OS releases.

Using a /MINAGE:180 should copy files that are approximately older than 6 months.  If you need to be more accurate, then you may have to calculate the MINAGE value based on the number of days in each of the last 6 months.  However, this likely has little additional value, and 180 should be OK.

If you use the /MOV option, then you can do both steps (copy and delete) in one fell swoop.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39722625
+1 BillBach.  I just don't use (x)copy any more.  Robocopy is far more superior and reliable.
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 39722660
Thanks, tagit.  I actually use a shareware utility called Beyond Compare (www.beyondcompare.com).  It's freaky fast, and it supports both scripts from a command line (for automated backup use) AND GUI operation (for manual updates or resolving conflicts) equally well.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39739113
Note that
for /r "%location%" /d %%d in (*) do (

Open in new window

is an undocumented combination of switches. You should be able to use either /R or /D, but not both. But it works. Anyway, it is not what you want. You just want to parse a single level of folders, which is /D, while /R is recursive (going thru all level of subfolders).

I agree to use RoboCopy, if you want to stick with what is available with your OS. We can perform both tasks with RoboCopy:
@echo off
setlocal
set dest=E:\Sportography\archive

REM Task 1: Copy files older than 180 days:
pushd \\sportographystorage\sportography\SPEEDJOBS\
for /d %%D in (*) do if exist "%%~D\SCANS\ORDERS2\" ^
robocopy "%%~D\SCANS\ORDERS2" "%dest%\SCANS\ORDERS2\" /NJH /NJS /S /NDL /MinAge:180
popd

REM Task 2: Delete archive files older than 18 months:
for /F "tokens=*" %%F in ('robocopy %dest% %dest% /IS /s /minage:180 /NDL /NP /L /NS /NC /NJH /NJS') do @echo del "%%F"

Open in new window

The second part doesn't actually delete the files, it only shows a "what-if". Remove the "echo" do let it do its evil work.

However, it doesn't make much sense without further actions. After deleting the files older than 18 months, the next copy will reintroduce that files, unless they are removed from the original folder. Or the first RoboCopy gets an additional switch for ignoring files older than 18 months: /maxage:540.
On the other hand, it seems to be more appropriate to move the files to the archive folder instead of copying.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Know what services you can and cannot, should and should not combine on your server.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

759 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

21 Experts available now in Live!

Get 1:1 Help Now