Batch File to archive files based on their name -
i have text files need archive daily , automate it. there 100 user folders , each user folder has 20 subfolders.
ex user folder structure: d:\logs\john hayse\01 d:\logs\john hayse\02 d:\logs\john hayse\03 etc... d:\logs\john hayse\20 ex filenames: john.hayes.t01.daily.log.txt john.hayes.t02.daily.log.txt john.hayes.t04.tasks.to.complete.txt billy.gavin.t02.daily.logs.txt
i started righting batch file hundreds of if exist statements this:
if exist d:\john.hayes.t01* move d:\john.hayes.t01* d:\logs\john hayse\01" if exist d:\john.hayes.t02* move d:\john.hayes.t02* d:\logs\john hayse\02"
if create separate text file containing users folders:
dir "d\logs" /b /a:d >d:\userfolderlist.txt
how use create if exist statement once , loop through of users files , place them in proper user folder , in corresponding subfolder ##?
ex. d:\john.hayes.t02.daily.log.txt archive d:\logs\john hayes\02
the users files start firstname.lastname.t## user's folder has space instead of '.' in between first , last name.
@echo off setlocal set "sourcedir=u:\sourcedir" set "destdir=u:\destdir" /f "tokens=1,2" %%a in ( 'dir /b /ad "%destdir%\*" ' ) %%n in (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20) ( if exist "%sourcedir%\%%a.%%b.t%%n.*" ( md "%destdir%\%%a %%b\%%n\" move "%sourcedir%\%%a.%%b.t%%n.*" "%destdir%\%%a %%b\%%n\" ) ) goto :eof
this should execute teh task describe - you'd need change settings of sourcedir
, destdir
suit.
the destination directory created if not exist. append 2>nul
md
line suppress already exists
message if desired.
note there obvious flaw in scheme. not apparently catering possibility destination file exists.
Comments
Post a Comment