Computing.Net > Forums > Programming > Batch or windows script

Batch or windows script

Reply to Message Icon

Original Message
Name: bangkok rat
Date: July 31, 2006 at 07:02:17 Pacific
Subject: Batch or windows script
OS: xp pro
CPU/Ram: 3.0 GHz / 2 GB
Comment:

Automated folder creation

This is my goal. I want to build a batch file that will create a new folder, and have the name assigned automatically. I would like for the name to be labeled in the following format: yyyy.mm.dd, which would correspond to the day the batch file is ran.

Does anyone know the syntax on how to achieve this? I think it is something like %d for date or something like that. I am not going to even try to act like I know what I am talking about.

Is a batch file the way to go, or should i use a script?

Thank you.


Report Offensive Message For Removal


Response Number 1
Name: uli_glueck
Date: July 31, 2006 at 07:40:22 Pacific
Reply: (edit)

This put the date in a variable and creates a directory.
(This is NT like, but should work. I don't know the XP specials....)
You have to tweak the set command. (Depends at the output format of your date. I use a german windows version.)

@echo off
FOR /f "tokens=*" %%a in ('date /t') do set date=%%a
set date=%date:~9,4%.%date:~6,2%.%date:~3,2%
md %date%
set date=
:EOF

hope it helps
uli


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: July 31, 2006 at 08:17:58 Pacific
Reply: (edit)

Well...

Not quite.

:: bangkok.bat
:: get sys YMD into vars

@echo off
call :YMD6
md %YYYY%%MM%%DD%
goto :eof

:YMD6

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof
:: DONE


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

M2



Report Offensive Follow Up For Removal

Response Number 3
Name: uli_glueck
Date: July 31, 2006 at 08:29:58 Pacific
Reply: (edit)

?!?
:-&
Wow, M2 could you maybe enlighten me a little?

uli


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: July 31, 2006 at 09:28:23 Pacific
Reply: (edit)

Hi uli,

One or two chunks at a time.

This:

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

creates a script which uses int21 service 2a to get 'system' month into dh. After the call we move dh into cl so that when it writes, the files size is equivalent to the numeric month.

Similar idea with YYYY and DD.

Near the bottom, it uses the %~z1 filesize to set vars. [This %~z1 is why it will NOT work in DOS.] I offered to work on a DOS version but nobody was interested.



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

M2



Report Offensive Follow Up For Removal

Response Number 5
Name: FishMonger
Date: July 31, 2006 at 09:48:04 Pacific
Reply: (edit)

Here's an alternative solution using a Perl script.

use POSIX qw(strftime);
mkdir strftime("%Y.%m.%d", localtime);



Report Offensive Follow Up For Removal


Response Number 6
Name: tonysathre
Date: July 31, 2006 at 10:19:13 Pacific
Reply: (edit)

Fish I see you doing a lot of Batch alternatives in Perl that look a lot easier to write. I have a Perl interpreter installed but I haven't had a change to learn it yet cause I've been trying to master Batch scripting. I think I should forget Batch scripts and take the time to learn Perl.

UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus.


Report Offensive Follow Up For Removal

Response Number 7
Name: FishMonger
Date: July 31, 2006 at 10:32:36 Pacific
Reply: (edit)

>> I think I should forget Batch scripts and take the time to learn Perl.

There are some things where a batch script is better suited but for most that I've seen Perl would be the better/cleaner option. It would be best to be experienced in both, but my batch scripting knowledge is limited.


Report Offensive Follow Up For Removal

Response Number 8
Name: bangkok rat
Date: July 31, 2006 at 11:34:19 Pacific
Reply: (edit)

Thank all of you for your help, but i am not a programer, so i am not sure what alot of the code you have provided does.

I am trying to build a simple batch file that will create a folder with date automaticlly. I will then schedule a tesk in the Task Schedule to run my batch file, which then will automaticlly move files i need to the newly creatd folder.

Is there a easy way to create a batch file for this task?


Report Offensive Follow Up For Removal

Response Number 9
Name: tonysathre
Date: July 31, 2006 at 13:40:36 Pacific
Reply: (edit)

I found this. It's called datedir.exe

It's a utility that names a folder as the date it is executed in the format of yyyymmdd. Hope that is useful, good luck.


UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus.


Report Offensive Follow Up For Removal

Response Number 10
Name: Shr0Om
Date: July 31, 2006 at 16:33:41 Pacific
Reply: (edit)

M2: That was a heavy mix of batch and assembly, hehe.

Well, in DOS this would be necesary. But if you use Win XP, (should work in win2k aswell i belive) there is an easier solution.

bangkok rat: First code is with comments. You can find the uncommented code at the bottom.

Copy everything after ::----------CODE---------
, paste it in notepad and save it as a .bat or a .cmd file.


@echo off
SetLocal EnableDelayedExpansion

::This will split up the default date format
::I have english XP so my defualt date
::format is dd.mm.yyyy.
::The delimiter is "."
::You can always change delim if you need

For /f "tokens=1-2* delims=." %%A in ('DATE/T') Do (
set ReArrDate=%%C.%%B.%%A

::I noticed i got a trailing space
::after yyyy, so next line removes that.
echo !ReArrDate: =!>>000tmp)

::%%C contains yyyy, %%B contains mm etc.

::Takes input from the file i created
::and reads into a new variable
set /p FolderName=<000tmp

::Creates folder
md %FolderName%

del 000tmp

::----------CODE---------
@echo off
SetLocal EnableDelayedExpansion

For /f "tokens=1-2* delims=." %%A in ('DATE/T') Do (
set ReArrDate=%%C.%%B.%%A
echo !ReArrDate: =!>>000tmp)

set /p FolderName=<000tmp

md %FolderName%

del 000tmp

::----------CODE---------


Report Offensive Follow Up For Removal

Response Number 11
Name: Shr0Om
Date: July 31, 2006 at 16:40:12 Pacific
Reply: (edit)

tonysathre:

I think that it depends on the task you want to do. For automation of tasks i would belive batch is superior since the programs/tools are already written. The batch script just executes them.

If it comes to txt file handling/manipulation and more heavy programming, Perl would be superior.

I just started to learn Perl myself for those more advanced tasks which requires more custom programming.

Still, knowing some batch is something i would recomend. It has made my world alot easier in several ocations.


Report Offensive Follow Up For Removal

Response Number 12
Name: ghostdog
Date: July 31, 2006 at 17:48:32 Pacific
Reply: (edit)

well, and so does Python. :-)

>>> import os,time
>>> os.mkdir( time.strftime("%Y.%m.%d") )


Report Offensive Follow Up For Removal

Response Number 13
Name: Mechanix2Go
Date: July 31, 2006 at 19:12:42 Pacific
Reply: (edit)

Shroom,

The script I posted will NOT work in DOS.

Using chunked up %DATE% in 2k/xp will NOT work unless the date layout happens to be a particular format.

Re-read #4.

bangkok, added MOVE:

::== bangkok2.bat
@echo off
call :YMD6
md %YYYY%%MM%%DD%
echo here's where you MOVE some files to %YYYY%%MM%%DD%
goto :eof

:YMD6

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof



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

M2



Report Offensive Follow Up For Removal

Response Number 14
Name: Shr0Om
Date: July 31, 2006 at 23:37:50 Pacific
Reply: (edit)

M2:

"The script I posted will NOT work in DOS."
Sorry, i just assumed so since you used a heavy aproach where also assembly is included.

"Using chunked up %DATE% in 2k/xp will NOT work unless the date layout happens to be a particular format."

Why is that? If the format is different, its easy to change the source. My script seems to work fine in English Windows.


Report Offensive Follow Up For Removal

Response Number 15
Name: Mechanix2Go
Date: August 1, 2006 at 00:26:39 Pacific
Reply: (edit)

Hi Shroom,

You're correct in saying that it's easy to change the %DATE% usage. Easy for someone who knows how. Most OPs don't know; that's why they're asking.

While we're at it, you don't need the for loop. Nor is there much point at trying to guess what the delims are for any particular setup.

In my caser the DATE is:

Tue 01-08-2006

So this bat gets the needed layout right:

::== d.bat
@echo off

echo %DATE%
set myDATE=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
echo myDATE is %myDATE%
:: DONE

Even more simply, for the original purpose:

::-- mover.bat
md %DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
move blabla %DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
:: DONE

2 lines, but they mau get busted here.



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

M2



Report Offensive Follow Up For Removal

Response Number 16
Name: uli_glueck
Date: August 1, 2006 at 01:01:36 Pacific
Reply: (edit)

::I noticed i got a trailing space
::after yyyy, so next line removes that.
echo !ReArrDate: =!>>000tmp)

::%%C contains yyyy, %%B contains mm etc.

::Takes input from the file i created
::and reads into a new variable
set /p FolderName=<000tmp

Hi Shr0Om

you don`t need to write in a tmp
and read it out to remove the trailing spaces.
Just set a new Variable:

set FolderName=!ReArrDate: =!
md !FolderName!

Uli


Report Offensive Follow Up For Removal

Response Number 17
Name: Shr0Om
Date: August 1, 2006 at 01:33:16 Pacific
Reply: (edit)

M2: If the script is to be executed on just his pc i dont see much problem in it.
The part of the code where changes can/should be made should be pretty easy to understand even tho your not a programmer. I also added comments in the code for easier understanding.
A drawback with this script is of course as you say that the delimiter can be different from what i specified. Thus, it wont be universially compatible with all systems. But i was just thinking it should be runned on his system.

Although, you could probably implement a
echo %DATE%|find "."
if errorlevel 1 (set delim=.) else set delim=-

When it comes to the FOR loop i agree it aint needed. It was just one solution. Parsing also works, but i think its a bigger chance it goes wrong thinking of different date formats. If you specify the delimiter that the date format uses, you should be pretty safe.

Anyway, there should be enough solutions here by now :]

uli_glueck:
I was pretty sure it wasn't needed but i tried "set FolderName=!ReArrDate: =! "
and it didnt work. Maybe coz i used md %FolderName% instead of !FolderName! ?.. I'll have a look at it later. Thnx for the tip.
Anyway, it was late and i just used the lazy aproach.. :]



Report Offensive Follow Up For Removal

Response Number 18
Name: bangkok rat
Date: August 1, 2006 at 05:32:48 Pacific
Reply: (edit)

Thank all of you for your help, but i did some searching and found that PC World had an article about what i wanted to do. Thanks again.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch or windows script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge