Create Directory in Window Batch script

April 12, 2012 at 07:51:56
Specs: Windows Vista
I am in process of creating a basic backup script to backup our key files at the end of day or at set time:
1. Create director = BKDataBase with YYYYMMDD'-'HHMM
2. xcopy files from a to new created director
3. If Directory exist then delete and re-create

Any help would be appreciated please.....

MP


See More: Create Directory in Window Batch script

Report •

#1
April 12, 2012 at 08:24:28
UNtesteD, please check before use
I would imagine that schedualed tasks would allow you to run the batch file daily fairly easily for you.

There shouldn't ever be a reoccurence of the directory if it is date and time?
however you could use if exist and then del

md BKDataBase%date:~10,4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%

then XCOPY A:\whatever.xxx C:\BKDataBase%date:~10,4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%

mike


Report •

#2
April 12, 2012 at 08:43:10
Hi Mike,

Many thanks....

got error as:

C:\BKDataBase\BKSourceData>cd c:\BKDataBase

c:\BKDataBase>md BKDataBase4/011641
The syntax of the command is incorrect.

Please help

MP


Report •

#3
April 12, 2012 at 10:10:28
Try using

mkdir BKDataBase4/011641

Does that work?

hth

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

Related Solutions

#4
April 12, 2012 at 10:58:51
hmm, that worked in xp, perhaps that iss the difference, or perhaps a different date format?

theoretically the first should have made a directory

BKDataBase201204120157

which was in the date and time format requested.

mike


Report •

#5
April 12, 2012 at 12:36:00
Hi Mike,

Thanks...

My requirements is to create directory automatically whenever backup scrip is run (via schedule job or manually):

1. Set Backup directory path (cd C:\DataBaseBackup\.....)

2. Create directory if it does not exist ie: BKDabataBaseYYYYMMDD (YYYY=Current Year, MM=Month Number, DD=Day Number)

3. Then copy data to new directory

4. Finally backup offline directory

sorry if I did not explain

MP


Report •

#6
April 12, 2012 at 13:14:09
if you would at a command prompt please type

echo %date%
and then past/copy the results to here and I will modify

this script was for the following format Thu 04/12/2012, i will happily correct for you

mike


Report •

#7
April 12, 2012 at 21:23:51
Well
The syntax of the command is incorrect.

Could either be because of the / instead of \ or because of the md.
md should work on Windows Vista AFAIK.

Elinor Hurst
http://elinorhurst.blogspot.com


Report •

#8
April 19, 2012 at 09:01:29
Hi,

I am still in position to create a batch script for our database backup:

1. I need to create a variable 'BKData' & Current date in format of 'YYYYMMDD'
2. This will be used to create backup directory IE:BKData20120419
3. Copy key data files from database folder to newly created directory

any help would be much appreciated.

MP



Report •

#9
April 19, 2012 at 09:45:09
Why are people trying to use Unix slashes in Windows?


@echo off & pushd C:\DataBaseBackup
for /f "tokens=2-4 delims=/ " %%i in (
"%date%"
) do (
set d=%%i
set m=%%j
set y=%%k
)
if not exist BKDataBase%y%%m%%d% (
md BKDataBase%y%%m%%d%
)
...
Then copy data to new directory
Finally backup offline directory

Tony


Report •

#10
April 19, 2012 at 12:02:03
Somehow in the original code, i was off with the date variable which i think is where the odd slash came from.(my bad). When I did the code in xp it pulled what I wanted, however when done with MP3672's system, it pulled the wrong info? My guess was that the %date% variable / system was using a different format, however, lost track of the post to make the corrections earlier. my appologies MP3672 for leaving things hanging.

mike


Report •

#11
April 20, 2012 at 05:58:04
Hi Tony,

Many thanks for your input....

My backup script is some how not correct, its creating backup directory without current date IE: BKDataBase%y%%m%%d%.

Part of my script is as:

C:\Temp\BKDataBaseBackup

for /f "tokens=2-4 delims=/ " %%i in (
"%date /t%"
) do (
set d=%%i
set m=%%j
set y=%%k
)
if not exist BKDataBase%y%%m%%d% (
md BKDataBase%y%%m%%d%
)

xcopy "c:\Users\user\AppData\Local\VirtualStore\Program Files\lotus\notes\data\Kumbh Reference.nsf" C:\Temp\BKDataBaseBackup\BKDataBase%y%%m%%d%\ /Y /V


xcopy "c:\Users\user\AppData\Local\VirtualStore\Program Files\lotus\notes\data\Kumbh Contacts.nsf" C:\Temp\BKDataBaseBackup\BKDataBase%y%%m%%d%\ /Y /V


Any input is much appreciated,

Kind regards,
MP


Report •

#12
April 20, 2012 at 06:40:34
Hi
Please let me know whats wrong with the following:

for /f "tokens=2-4 delims=/ " %%i in (
"%date%"
) do (
set d=%%i
set m=%%j
set y=%%k
)

Output is as follows:
d=04
m=2012
y=

Any input please....


Report •

#13
April 20, 2012 at 07:14:10
Post the output of echo %date%. I shouldn't have assumed your date format was the same as mine.

Tony


Report •

#14
April 20, 2012 at 07:53:53
Many thanks my backup script is now working and going live next week...once again many thanks..

Report •

#15
December 30, 2012 at 21:01:53
In the format of the for command use, you need to state more than 2-4.
2-4 is considered one token.
i.e.
for /f "tokens=2,3,4" %%i in ('object') do (
command %%i
command %%j
command %%k
)

Report •

#16
December 30, 2012 at 21:51:48
i don't think so... at least with my experience (win-xp) tokens went to discreet vars regardless of syntax. (Ha ha!! now i know i'm up too late!)

Report •

Ask Question