Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.xlsI 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 :)

I need to know what these put out:
echo %date%
echo %time%
=====================================
If at first you don't succeed, you're about average.M2

and this:
dir /tc/od
=====================================
If at first you don't succeed, you're about average.M2

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

"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

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 :)

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)

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

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 CHDIRand it goes on.
Hope it helps :)

Not sure.
Try thses:
echo %PATH%
echo %PATH:~,7%
=====================================
If at first you don't succeed, you're about average.M2

It says C:\WINN
I have no idea where that comes from. Its not even the directory I am playing in.

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

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.

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.

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

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!

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...

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).

Thanks IVO but didn't work.
In fact it stopped it doing anything (other than make the new folder)

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"))

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

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...).

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

![]() |
![]() |
![]() |

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