Who doesn't like to automate things? Well not sure about you, but I definitely do. So here is 1 for all my friends who got stuck into a use case like mine and after googling different pages, were not able to find a complete script which works like a charm.
Use Case:
I have plethora of folders in a SOURCE directory which contains folders from different years but I don't know which folder belong to which year. Date modified is of no use here since they were part of a zip earlier and when I extracted the zip, date modified was set to current date.
Anyway, I have the list of folder names from a excel, year wise, to refer to, but again searching for each name manually and copying folder 1 by 1 is a tedious task so I thought of creating a batch script to make this easier for me & I did it after lots of research & RND. The script also deletes the files which are copied from source folder so be careful with that and remove that part if not needed.
The comments are added within script for reference to guide on different actions that script will perform. You can remove the parts that you don't need or modify the logging details as per your need.
Solution:
Below is the script which takes care of the above requirement. But you'll have to provide some inputs
SOURCE_FOLDER: The folder from where folders/files would be copied
TARGET_FOLDER: The folder to which folders/files would be copied (including content inside them)
LIST_FILE: List of names of folders/files
For Example:
TextFile1:
==========
folder1
folder2
folder3
And here is the script.
Note: Please copy the content to some notepad and save it with .BAT extension to run it.
@echo off setlocal EnableDelayedExpansion :: Define source and target folders set "SOURCE_FOLDER=C:\Users\parvrath\SOURCE" set "TARGET_FOLDER=C:\Users\parvrath\TARGET" set "LIST_FILE=C:\Users\parvrath\List.txt" :: Create a log file set "LOG_FILE=copy_log.txt" if exist "%LOG_FILE%" del "%LOG_FILE%" :: Loop through the folder names in the list file for /f "tokens=*" %%a in (%LIST_FILE%) do ( set "FOLDER_NAME=%%a" set "SOURCE_PATH=!SOURCE_FOLDER!\!FOLDER_NAME!" set "TARGET_PATH=!TARGET_FOLDER!\!FOLDER_NAME!" :: Check if source folder exists if exist "!SOURCE_PATH!" ( :: Copy the folder and its contents xcopy "!SOURCE_PATH!" "!TARGET_PATH!" /s /i /y :: Append copied folder name to log file echo !FOLDER_NAME! copied >> "%LOG_FILE%" :: Delete the source folder rd /s /q "!SOURCE_PATH!" echo !FOLDER_NAME! deleted >> "%LOG_FILE%" ) else ( echo "Source folder '!SOURCE_PATH!' not found." >> "%LOG_FILE%" ) ) echo. echo "Copying complete. See %LOG_FILE% for details." pause endlocal
I hope this helps !!
No comments:
Post a Comment