Computing.Net > Forums > Programming > Batch file to append created date

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 file to append created date

Reply to Message Icon

Name: CHR15
Date: January 10, 2007 at 00:59:16 Pacific
OS: NT4
CPU/Ram: old and slow
Product: compaq
Comment:

Hi chaps,

I need to write a batch file to append the created date to the beginning of a bunch of files.
There are hundreds of files, mostly office based so *.doc, *.xls, *.ppt etc.

An example is a file called
Equipment Spares.xls

I need this renaming to: 20060729_1030_Equipment_Spares.xls
where the date is the CREATED date and not the system date at time of conversion.

The date format can have no spaces or separation characters.

Spaces to be replaced with underscores.

All files within the directory are to be renamed and saved in the same directory.

If it is a factor, the dates are currently in UK format so are separated by a / and the hours minute by a :

OS is NT4 and it's bolted down so I can't install any third party software, simple Batch file only.

Ideally, I could get users to simply copy the batch file into their respective folders and run it from there themselves.


Hope someone can help me out. Thanks in advance for any replies :)



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: January 10, 2007 at 01:31:55 Pacific
Reply:

I need to know what these put out:

echo %date%
echo %time%


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

M2



0

Response Number 2
Name: Mechanix2Go
Date: January 10, 2007 at 02:18:29 Pacific
Reply:

and this:

dir /tc/od


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

M2



0

Response Number 3
Name: CHR15
Date: January 10, 2007 at 04:28:33 Pacific
Reply:

I dont get what you mean! Are you asking me to try those and see what happens??

I am a newbie at all of this. In fact I was quite chuffed with myself when I wrote a small batch file to create an index file for the directory:

DIR /S /B >Listing.txt


0

Response Number 4
Name: Mechanix2Go
Date: January 10, 2007 at 04:33:26 Pacific
Reply:

"I dont get what you mean! Are you asking me to try those and see what happens??"

yes


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

M2



0

Response Number 5
Name: CHR15
Date: January 10, 2007 at 04:46:33 Pacific
Reply:

okay:

I probably need more help with the first one. Nothing happened so I stuck a >time.txt at the end. The file just said Echo is on


The Second one (again I stuck a >list.txt at the end)

Gave me DD/MM/YY HH:SS Filesize filename

Hope this goes some way to answer your questions. Thanks for the help :)


0

Related Posts

See More



Response Number 6
Name: CHR15
Date: January 10, 2007 at 04:48:31 Pacific
Reply:

Oh sorry. I had to modify the second one to:

dir /tc /o-d

I hope that is what you meant (wouldn't do anything if I didn't change it)


0

Response Number 7
Name: Mechanix2Go
Date: January 10, 2007 at 05:24:33 Pacific
Reply:

Hi,

We're making some progress. I'm a bit challenged because I haven't run NT4 for a long time. I don't know if NT4 has 'CMD extensions' similar to 2k/xp.

Do this and see if it mentions extensions:

cmd /?


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

M2



0

Response Number 8
Name: CHR15
Date: January 10, 2007 at 05:38:36 Pacific
Reply:

Yes it does. Something along the lines of....

The command extensions involve changes and/or aditions to the following commands:

Del or ERASE
COLOR
CD or CHDIR

and it goes on.

Hope it helps :)


0

Response Number 9
Name: Mechanix2Go
Date: January 10, 2007 at 05:46:34 Pacific
Reply:

Not sure.

Try thses:

echo %PATH%


echo %PATH:~,7%



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

M2



0

Response Number 10
Name: CHR15
Date: January 10, 2007 at 05:49:34 Pacific
Reply:

It says C:\WINN

I have no idea where that comes from. Its not even the directory I am playing in.


0

Response Number 11
Name: Mechanix2Go
Date: January 10, 2007 at 05:55:19 Pacific
Reply:

It comes from the built-in variable %PATH%.

The important thing is that we now know that your NT4 can 'do' substrings.

"Gave me DD/MM/YY HH:SS Filesize filename"

Please double check.



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

M2



0

Response Number 12
Name: IVO
Date: January 10, 2007 at 06:03:59 Pacific
Reply:

Hi M2,

Win NT/4.0 Cmd.exe can process the same scripts you code for the Win 2K/XP environments with the only (noticeably) difference it doesn't have the "delayed expansion" feature. So keep that in evidence while structuring your code.


0

Response Number 13
Name: CHR15
Date: January 10, 2007 at 06:06:24 Pacific
Reply:

Okay.

the batch file entirely consists of

dir /tc /o-d >List.txt

When run, it gives me the following (I put the >list.txt as its the only way of me knowing what it does)


10/01/07 12:41 22 New Text Document.bat


There is a fair amount of spacing which I removed to fit in the reply. The 22 is the filesize.


0

Response Number 14
Name: Mechanix2Go
Date: January 10, 2007 at 06:19:19 Pacific
Reply:

Hi IVO,

Thanks for that. Does that mean that setlocal works but EnableDelayedExpansion does not?

HI CHR,

We're getting there.

I don't know why the /o-d worked and the /od did not, but no matter. The key is that the /tc [time created] works.

I need to hammer this a little. Mine puts out dd/mm/yyyy so it's not much to filter.

==============================
C:\temp\-\created>dir /tc|find /v ">"|find "2007"
10-01-2007 19:19 129 A.BAT
10-01-2007 19:29 255 G.BAT
10-01-2007 19:29 247 G2.BAT
==================================

Your's does 2 digit year so it takes a bit more doing to clean up a 'plain' DIR.



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

M2



0

Response Number 15
Name: CHR15
Date: January 10, 2007 at 07:04:19 Pacific
Reply:

I notice in my example at the top I could have made a better effort with the time.

This is supposed to be in 24hr format.

Dont know if it makes a significant difference to the code!


0

Response Number 16
Name: IVO
Date: January 10, 2007 at 07:17:10 Pacific
Reply:

Hi M2,

just as you said: SetLocal works under Win NT/4.0 but it doesn't recognize the EnableDelayedExpansion operand (as the /V switch for Cmd.exe was a big Win 2000 enhancement).

Good luck with your in progress training session...


0

Response Number 17
Name: CHR15
Date: January 10, 2007 at 07:20:26 Pacific
Reply:

ECHO Good luck with your in progress training session

lol.


0

Response Number 18
Name: CHR15
Date: January 11, 2007 at 02:51:50 Pacific
Reply:

I am almost there.
So far I have a batch file which does what I want but will only produce a two digit year.

Here is what I have:

md DII_Ready
for /f "tokens=1,2,3,4,5,6,* delims=:/ " %%A in ('dir /t /c *.* ^|
find "/"') do copy "%%G" "DII_Ready\%%C%%B%%A_%%D%%E_U_%%G"


So this makes a folder called DII_Ready
Copies all files into it renaming them with YYMMDD_HHMM_U_filename and it works a treat.

The extra _U_ is a feature I decided to add.

Just get to find a way of getting the four digit year (perhaps this is due to a setting on my machine (short date format).


0

Response Number 19
Name: IVO
Date: January 11, 2007 at 03:07:11 Pacific
Reply:

Try to add the switch /4 to the dir command, i.e.

('dir /4 /t /c *.* ^|...


0

Response Number 20
Name: CHR15
Date: January 11, 2007 at 03:42:10 Pacific
Reply:

Thanks IVO but didn't work.

In fact it stopped it doing anything (other than make the new folder)


0

Response Number 21
Name: IVO
Date: January 11, 2007 at 06:04:04 Pacific
Reply:

Sorry, the /4 switch works in 2K/XP only, the following workaround should solve the problem while a quite cumbersome

MD DII_Ready
For /f "tokens=1,2,3,4,5,6,* delims=:/ " %%A in ('Dir /t /c *.* ^|Find "/"') Do (
If %%C gtr 80 then (
Copy "%%G" "DII_Ready\19%%C%%B%%A_%%D%%E_U_%%G"
) else (
Copy "%%G" "DII_Ready\20%%C%%B%%A_%%D%%E_U_%%G"))



0

Response Number 22
Name: Mechanix2Go
Date: January 11, 2007 at 07:04:12 Pacific
Reply:

Hi guys,

Yeah, that's what I would do.

If you have dates 1999 and older you could fix that too.

if %YY% GTR 80 set YYYY=19%YY%



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

M2



0

Response Number 23
Name: IVO
Date: January 11, 2007 at 07:26:16 Pacific
Reply:

Hi M2,

I too planned what you posted, but I prefer to avoid the setting of an environment variable due to the lack of delayed expansion in NT/4.0.

And about /4, it is another one of that annoying issue affecting MS script dialetcs (i.e. 2K version is unlike XP, unlike NT and so on...).


0

Response Number 24
Name: Mechanix2Go
Date: January 11, 2007 at 07:33:42 Pacific
Reply:

Hi IVO,

Yeah, I figured I would avoid the delayed expansion by using a sub. This is not complete and not tailored to his DATE format, but you get the ides.

============================
:: hard lesson: delims=* just gaggles up but delims= " is OK
setlocal

@echo off

for /f "tokens=1-4 delims= " %%A in ('dir /t /c ^| find "2007"') do (
call :sub1 %%A %%B %%D
)
goto :eof

:sub1

set rawDATE=%1
set rawTIME=%2
set rawFN=%3
::echo %rawDATE%
::echo %rawTIME%
echo %rawFN%
set appDATE=%rawDATE:~6,4%%rawDATE:~3,2%%rawDATE:~0,2%
echo %appDATE%
set appTIME=%rawTIME:~0,2%%rawTIME:~3,2%
echo %appTIME%
goto :eof



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

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






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 file to append created date

Batch file to delete files by date. www.computing.net/answers/programming/batch-file-to-delete-files-by-date/13144.html

Batch file to change some text. www.computing.net/answers/programming/batch-file-to-change-some-text/18769.html

Batch File to Append Date to file n www.computing.net/answers/programming/batch-file-to-append-date-to-file-n/17574.html