jeudi 31 janvier 2019

Batch Script: if folder exists, copy ONLY SUB-folders/files to archive network folders based on first two digits of folder name i.e. "02" for February

In my new role, at the beginning of each year, we move all client data files from from two years prior (this year they are 2016 data files) from one network and all of their associated analysis data files from another network, to our archive drive/network.

I am looking for a way to automate this task because previously users would just click-and-drag each of the data folders to the archive, which is time consuming and tedious.

I have been looking into creating a batch script to alleviate this, however, I just started learning batch scripting yesterday. So here goes:

I have two network folders: A:\ where we store our client data analysis files and templates broken down by client, then year, then month (A:\Client Names\2016\06 June 2016), and B:\ where we store the client source data files which is broken down by year, then month, then client (B:\All Client Data\2016\01 Client Data\"Client Name").

I need to move all 2016 analysis folders to their respective folders in the All Client Data network. Then move the entire All Client Data\2016\ directory to our archive network.

The main problem so far, is that there is not always a 2016 folder in the A:\ drive, and when there is, the month folders within can vary.

So the problem then:

I need to FOR LOOP through the subdirectories of the A:\Client Names\ drive

IF EXIST Client Names\2016\ then xcopy/robocopy ONLY the sub folder(s) and their files to the appropriate folder in the B:\ drive, matching those folders by the first 2 digits in their folder names.

I am new to batch scripting, actually just started yesterday, but I feel like I am on the right path.

My problem is that I do not know how to resolve the pathname to the sub folders if the 2016 folder exists (I havent gotten past this step). I also do not know how to copy those folders to the specific folder in the B:\ drive.

I have been writing my code on a smaller scale by creating a folder on my local drive and another netwrok drive to see if I cant get the basics down, but I've ran into a wall.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION


:: variables

SET scriptname=%~n0
SET parentpath=%~dp0
SET archyear=2016
SET "sourcepath=C:\Data_Files"
SET "destinpath=Z:\Batch_Test"

NET USE Z: \\C_WSDUSA_Groups\PRA\GBL

:: begin main script language

CD %sourcepath%

FOR /D /R "%sourcepath%" %%A IN (%archyear%) DO (
    IF EXIST "%%~fA\%%A\2016" (
        xcopy "%%~fA\%%A\2016\*" "%destinpath%" /e /v /g /h
    ) ELSE IF NOT EXIST (
        ECHO %%~pA does not contain %archyear% data
    )
)

PAUSE

:END
ENDLOCAL
ECHO ON
@EXIT /B 0

In the above code, in the %sourcepath% I have copied over two client folders, one with a 2016 folder and \02 February 2016\ and \08 August 2016 CAP\ sub folders, and the other client folder with just data files in it (no 2016 folder or sub-folders)

I know the problems and errors in the above code are in the IF EXIST line, Because I do not know how to dynamically reference the path to the 2016 folder.

-- After correcting for this, will I have to create a nested IF EXIST command to find what the beginning strings are: "02" and "08", to then be able to match them to the appropraite folder in the B:\ drive when copying?

-- Thank you all very much for your help. I am learning on the fly here and appreciate and help or guidance you can offer. I come to this site frequently as a lurker for SAS and SQL help, which is more my background, but this is my first time posting. I apologize for the long-winded post. Thanks again!

Aucun commentaire:

Enregistrer un commentaire