Task I'm faced with today is this:
Using a bat file (by doubleclicking on it) it will copy only certain folders (with a date in the title) to the bat file's directory that are NOT listed in a txt file but then add to a txt file what folder names were added.
1. It will first get current date in desired format. - problem solved.
2. It will first see if there is a txt file (which would be a list of folder names, one folder name per line). This txt file was created by this bat file running previously.
3. if the txt file doesn't exist.. copy folders that match the folder name criteria and then add those names to a txt file so that next time the bat is run, it will know to skip these folders.
4. if the txt file does exist.. copy only folders that match the folder name criteria that are NOT in the txt file... and then append those folder names to the txt file so that next time the bat is run, it will know to skip these folders.
So.. the code below is what I've come up with so far. The txt file is not getting appended correctly.. because the loops are going over and over everytime it's run. .. it's a mess.. and so is my brain at this point. Any help would be appreciated. It seems like.. this should work fine..
Code: Select all
@echo off
echo .. harvesting..
REM date parsing loop below will give date in form yyyymmdd to match Podium MR client folder name
FOR /F "tokens=1-3 delims=/-" %%A IN ("%DATE%") DO (
SET DayMonth=%%A
SET MonthDay=%%B
SET Year=%%C
)
FOR /F "tokens=2 delims= " %%A in ("%DayMonth%") DO (SET Month=%%A)
setlocal enabledelayedexpansion
set /a counter=0
setlocal enabledelayedexpansion
for /f "usebackq delims=*" %%T in (`dir /ad /b "C:\Program Files\Company" ^| findstr /i "client-%Year%%Month%%MonthDay%*" `) do (
set foldercheck=%%T
if exist c:\collect.txt (
for /f "usebackq delims=*" %%R in (`findstr /v "!foldercheck!" "C:\collect.txt"`) do (
set folder=%~dp0%
set folder=!folder:~0,-1!
echo D | xcopy /s "c:\program files\Company\%%T" "!folder!\%%T" >nul
set /a counter+=1
)
) else (
set folder=%~dp0%
set folder=!folder:~0,-1!
echo D | xcopy /s "c:\program files\company\%%T" "!folder!\%%T" >nul
echo %%T >>C:\collect.txt
)
)
pause
this was what I originally came up with that worked until I realized that it would keep copying all the folder names every time but.. maybe it's a springboard for someone if the other one is indecipherable.
Code: Select all
@echo off
echo .. harvesting..
REM date parsing loop below will give date in form yyyymmdd to match Podium MR client folder name
FOR /F "tokens=1-3 delims=/-" %%A IN ("%DATE%") DO (
SET DayMonth=%%A
SET MonthDay=%%B
SET Year=%%C
)
FOR /F "tokens=2 delims= " %%A in ("%DayMonth%") DO (SET Month=%%A)
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%c in (`dir /ad /b "C:\Program Files\company" ^| findstr /i "client-%Year%%Month%%MonthDay%*" `) do (
set folder=%~dp0%
set folder=!folder:~0,-1!
echo D | xcopy /s "c:\program files\company\%%c" "!folder!\%%c" >nul
)
if you a test folder name for debugging an example would be "client-20140119*"
Thanks in advance for any help. Jules