user2629452
user2629452

Reputation: 17

Make a bat file that creates a txt file in a defined format

I am trying to make a bat file that creates a txt file with this format:

"

[date]  
2013/29/07 
10:38:00 

"

The code doesn't work with bat file, only when I put it in the cmd manual with copy-paste:

echo [date]  > e:\TAG.txt
for /f "tokens=2-4 delims=/ " %i in ('date /t') do ( echo %k/%j/%i >> e:\TAG.txt goto :eof)
for /F "tokens=1-4 delims=: " %i in ('time /t') do echo %i:%j:00 >> e:\TAG.txt

What am I doing wrong?

Upvotes: 1

Views: 3338

Answers (4)

foxidrive
foxidrive

Reputation: 41307

This technique eliminates problems with padding and different locales/computer settings.

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

>e:\TAG.txt echo [date]
>>e:\TAG.txt echo %yyyy%/%dd%/%mm%
>>e:\TAG.txt echo %hh%:%min%:%sec%

Upvotes: 0

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11191

You need to replace % with %% within batch files.

You don't need goto :eof inside for. Even if you needed, you must uses & between two commands.

In time part, you don't need 4 arguments, just 2.

Consider also using %DATE% and %TIME% instead of calling TIME /T and DATE /T:

echo [date]  > e:\TAG.txt
for /f "tokens=2-4 delims=/ " %%i in ("%date%") do echo %%k/%%j/%%i >> e:\TAG.txt
for /F "tokens=1-2 delims=: " %%i in ("%time%") do echo %%i:%%j:00 >> e:\TAG.txt

EDIT: for single line:

echo [date]  > e:\TAG.txt
for /f "tokens=2-7 delims=/ " %%i in ("%date%/%time::=/%") do echo %%k/%%j/%%i %%l:%%m:%%n >> e:\TAG.txt

Upvotes: 1

Stephan
Stephan

Reputation: 56238

in Batchfiles, double your % in for-loops:

echo [date]  > e:\TAG.txt
for /f "tokens=2-4 delims=/ " %%i in ('date /t') do ( echo %%k/%%j/%%i >> e:\TAG.txt goto :eof)
for /F "tokens=1-4 delims=: " %%i in ('time /t') do echo %%i:%%j:00 >> e:\TAG.txt

(single % for commandline, double %% in batchfiles - but only in for-loops)

Upvotes: 1

user1847879
user1847879

Reputation: 29

Just add start to the beginning of the line.

The contents of your batch file should be like this:

start for /f "tokens=2-4 delims=/ " %i in ('date /t') do echo %k/%j/%i >> e:\TAG.txt

Upvotes: 0

Related Questions