Solved

Batch Script

Posted on 2013-12-10
10
328 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
[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
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 43

Expert Comment

by:Rob
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 70

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

696 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