Computing.Net > Forums > Programming > Batch to copy and increment folders

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to copy and increment folders

Reply to Message Icon

Name: cwiz
Date: March 26, 2009 at 11:50:57 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Is there a way I can have a batch file take a set of files in a folder c:\123
file1.txt
file2.txt
file3.txt

and copy them to a folder c:\backup
having another set of folders increment for the day that they are backed up on? (only 5 days)

c:\backup\Monday\file1.txt
file2.txt
file3.txt
c:\backup\Tues\file1.txt
file2.txt
file3.txt

c:\backup\Wed\file1.txt
file2.txt
file3.txt
.....etc

this should then repeat the next week overwriting Mondays backup then on Tuesday it overwrites Tuesdays backup.

I've read on here how to make incremental directories, how to increment a single file name but nothing like this one?

Anybody have an idea?
Maybe its easier to say I need to copy a daily current set of files to the daily folder overwriting the previous weeks file of the current day. Get it?

I'm loosing it.... ahhhh



Sponsored Link
Ads by Google

Response Number 1
Name: Valerie (by Garibaldi)
Date: March 26, 2009 at 18:03:05 Pacific
Reply:

The first section of the code below is to establish the alpha name of the day-of-the-week from %date% and is dependent on the format of what is returned by echoing your %date%. The date format used is "day mm/dd/yyyy". The deletion of old file(s) and copying the new ones has not been tested.

:: Code begins...

@echo off
cls

:: Find alpha day of week...
  set day=%date:~7,2%
set month=%date:~4,2%
 set year=%date:~-4%

if   %day% lss 10 set day=%day:~-1%
if %month% lss 10 set month=%month:~-1%

set /a a=(14-%month%)/12
set /a y=%year%-%a%
set /a m=%month%+12*%a%-2
set /a daynumber=(%day% + %y% + %y%/4 - %y%/100 + %y%/400 + (31*%m%/12)) %% 7

set /a daynumber=%daynumber%+1

for /f "tokens=%daynumber%" %%1 in (
    "Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do (
    set dayalpha=%%1
)

:: Delete old backup file(s) and Copy new...
set pathname=C:\Backup\%dayalpha%\

if exist %pathname% del/q %pathname%\*.*

copy C:\123\*.* %pathname%
:: Code ends...

Good luck.


0

Response Number 2
Name: Mechanix2Go
Date: March 26, 2009 at 22:46:44 Pacific
Reply:

This will set a var to today's DAY, using BIOS:

==================================
@echo off & setLocal EnableDelayedExpansion

> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday
echo today is %day%


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: cwiz
Date: March 26, 2009 at 23:38:13 Pacific
Reply:

This is what I have. I decided to use a full date instead of a weekday. Now I am having the issue where I cannot remove the directory. The code states rd /s /q I even tried to pass it as a var _rdflag. It tells me that the directory isnt empty. This code will work if there isnt anything in the directories. Any ideas how to force this to delete?

@Echo Off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set day=%%a-%%b-%%c)

mkdir %DAY%
xcopy "C:\stuff\*.*" c:\backups\%DAY% /q

:: Set this to the number of folders you want to keep
Set _NumtoKeep=2

:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\backups

If Exist "%temp%\tempjunk" Del "%temp%\tempjunk"
PushD %_Path%

Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single

For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tempjunk" (
Echo %%I:%%~fI >>"%temp%\tempjunk"
) Else (
Echo.>"%temp%\tempjunk"
Echo.>>"%temp%\tempjunk"
Echo.>>"%temp%\tempjunk"
Echo %%I:%%~fI >>"%temp%\tempjunk"
))

:_Removeold
Set _rdflag= /q
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tempjunk"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%J")

:_Done
If Exist "%temp%\tempjunk" Del "%temp%\tempjunk"


0

Response Number 4
Name: Mechanix2Go
Date: March 27, 2009 at 00:11:08 Pacific
Reply:

What happens when you:

rd /s /q


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: cwiz
Date: April 22, 2009 at 06:16:14 Pacific
Reply:

I took a little break from this project, but I have since gotten it to work. Seems as I had a typo... go figure but here is the code if someone else could benefit.
Thank to all that helped :)

@Echo Off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set day=%%a-%%b-%%c)

mkdir %DAY%
xcopy "C:\stuff to backup\*.*" c:\backups\%DAY% /q /e /h /i /c /g /y

:: Set this to the number of folders you want to keep
Set _NumtoKeep=10

:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\backups

If Exist "%temp%\tempjunk" Del "%temp%\tempjunk"
PushD %_Path%

Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single

For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tempjunk" (
Echo %%I:%%~fI >>"%temp%\tempjunk"
) Else (
Echo.>"%temp%\tempjunk"
Echo.>>"%temp%\tempjunk"
Echo.>>"%temp%\tempjunk"
Echo %%I:%%~fI >>"%temp%\tempjunk"
))

:_Removeold
Set _rdflag= /q
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tempjunk"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%I")

:_Done
If Exist "%temp%\tempjunk" Del "%temp%\tempjunk


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Txt renamer from content? Batch scripting



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch to copy and increment folders

BATCH to copy and rename files www.computing.net/answers/programming/batch-to-copy-and-rename-files/15221.html

Batch file copy and Rename www.computing.net/answers/programming/batch-file-copy-and-rename/18689.html

Batch to create an incremented fold www.computing.net/answers/programming/batch-to-create-an-incremented-fold/16805.html