Rename document filenames by appending last saved date to filename

E

EZ

Can anyone help me with suggestions for this task?

I have to rename many hundreds of different MS-Word and ADOBE pdf
documents in several different Windows 2000 network folders by
appending the "last saved" date to the original file name. For
example, for all the documents found under a particular folder \
\SERVERname\SHAREname\xyzFOLDER, rename each ABCDE.doc to ABCDE.
20041230.doc, (i.e. this document was last saved on December 30,
2004). Part of the purpose of this task is to create and maintain an
online archive of different versions of the same document in one
directory, as they have been updated over time.

Is there a set of batch file commands I can use to simplify this
task? I am new to writing batch file commands.

Thank you kindly,

Ez
 
B

billious

EZ said:
Can anyone help me with suggestions for this task?

I have to rename many hundreds of different MS-Word and ADOBE pdf
documents in several different Windows 2000 network folders by
appending the "last saved" date to the original file name. For
example, for all the documents found under a particular folder \
\SERVERname\SHAREname\xyzFOLDER, rename each ABCDE.doc to ABCDE.
20041230.doc, (i.e. this document was last saved on December 30,
2004). Part of the purpose of this task is to create and maintain an
online archive of different versions of the same document in one
directory, as they have been updated over time.

Is there a set of batch file commands I can use to simplify this
task? I am new to writing batch file commands.

Thank you kindly,

Ez

Relatively simple thing to do.

A few things you need to clarify first though.

First - have you considered what will happen when a document is
re-processed? For instance, today it's called ABCDE.doc, tomorrow it's
ABCDE20070809.doc and the next day ABCDE2007080920070810.doc and so on. Is
the rename operation going to cause problems for your users - they've saved
the file under one name, and its name will be different tomorrow (and hence
their MRU lists will cease to function.)

COPYing the file to ABCDEyymmdd.doc would probably be preferable from a user
viewpoint - but it's still no protection against a user modifying a historic
version (if that's your purpose) - and even making the copy read-only and/or
hidden is no protection against a sufficiently-aware user - whether that
user's actions defeat your purpose by accident, ignorance or design.

Do your filenames contain non-alphameric characters? Certain characters can
cause problems - spaces need to be noted, but [%!&] and others can cause
problems.

What is your date format? Is it kosher to assume that a filename
ABCDEyyyymmdd.doc has already been processed if yymmdd is the file's date?

Could you process ABCDE.doc to ABCDEyyyymmdd.COD (and .PDFs to .FDP) - that
is, change the extent so that the copy is nominally invisible to the naive
user (you could reinforce this with hidden/read-only?) This would have the
advantage of obviating the re-processing problem I first referred to.
Naturally, you could then construct a "recover" facility to re-copy to
ABCDE.DOC if required - but may be too complex for your users, or what you
have in mind.

Meanwhile, try reading alt.msdos.batch.nt - especially Timo's "Hints and
Tips" (or is that "Hints and Tricks"?)
 
E

EZ

Can anyone help me with suggestions for this task?
I have to rename many hundreds of different MS-Word and ADOBE pdf
documents in several different Windows 2000 network folders by
appending the "last saved" date to the original file name. For
example, for all the documents found under a particular folder \
\SERVERname\SHAREname\xyzFOLDER, rename each ABCDE.doc to ABCDE.
20041230.doc, (i.e. this document was last saved on December 30,
2004). Part of the purpose of this task is to create and maintain an
online archive of different versions of the same document in one
directory, as they have been updated over time.
Is there a set of batch file commands I can use to simplify this
task? I am new to writing batch file commands.
Thank you kindly,

Relatively simple thing to do.

A few things you need to clarify first though.

First - have you considered what will happen when a document is
re-processed? For instance, today it's called ABCDE.doc, tomorrow it's
ABCDE20070809.doc and the next day ABCDE2007080920070810.doc and so on. Is
the rename operation going to cause problems for your users - they've saved
the file under one name, and its name will be different tomorrow (and hence
their MRU lists will cease to function.)

COPYing the file to ABCDEyymmdd.doc would probably be preferable from a user
viewpoint - but it's still no protection against a user modifying a historic
version (if that's your purpose) - and even making the copy read-only and/or
hidden is no protection against a sufficiently-aware user - whether that
user's actions defeat your purpose by accident, ignorance or design.

Do your filenames contain non-alphameric characters? Certain characters can
cause problems - spaces need to be noted, but [%!&] and others can cause
problems.

What is your date format? Is it kosher to assume that a filename
ABCDEyyyymmdd.doc has already been processed if yymmdd is the file's date?

Could you process ABCDE.doc to ABCDEyyyymmdd.COD (and .PDFs to .FDP) - that
is, change the extent so that the copy is nominally invisible to the naive
user (you could reinforce this with hidden/read-only?) This would have the
advantage of obviating the re-processing problem I first referred to.
Naturally, you could then construct a "recover" facility to re-copy to
ABCDE.DOC if required - but may be too complex for your users, or what you
have in mind.

Meanwhile, try reading alt.msdos.batch.nt - especially Timo's "Hints and
Tips" (or is that "Hints and Tricks"?)- Hide quoted text -

- Show quoted text -

Thanks for your response.

There is currently no intent to reprocess any of the target files; the
renamed files are part of an archive cleanup process. The archived
files will live in a network folder with very limited access; the file
rename process is more for easy identification and much less of a
security measure. None of the file names currently contain non-
alphanumeric characters. The date format of choice for the filename
append is yyyymmdd, and this date/time append is currently not present
in any of the filenames. But once any of the filenames bear this
append, it will serve as cue that it has already been processed.

I like the idea of the additional file extension processing for
protective measure.

So based on the feedback above, do you have any specific batch script
ideas that will perform this specific process as outlined?

Thanks again.
 
B

billious

EZ said:
[snippedy-poo]

There is currently no intent to reprocess any of the target files; the
renamed files are part of an archive cleanup process. The archived
files will live in a network folder with very limited access; the file
rename process is more for easy identification and much less of a
security measure. None of the file names currently contain non-
alphanumeric characters. The date format of choice for the filename
append is yyyymmdd, and this date/time append is currently not present
in any of the filenames. But once any of the filenames bear this
append, it will serve as cue that it has already been processed.

I like the idea of the additional file extension processing for
protective measure.

So based on the feedback above, do you have any specific batch script
ideas that will perform this specific process as outlined?

Thanks again.


----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]:: starting at Your Data Source, replicate tree to Your Data Destination
[4]set yds=c:\datasource
[5]set ydd=c:\datadest
[6]echo d|xcopy /t %yds% %ydd% >nul
[7]:: start processing at datasource
[8]pushd %yds%
[9]:: for extensions of interest, scan
[10]:: construct Your Destination Filename from filenames found
[11]::
[12]for %%e in (pdf txt) do for /r %%i in (*.%%e) do set ydf=%%~dpni&set
ydf=!ydf:%yds%=%ydd%!&call :process %%e "%%~ti" "%%~ni" "%%~fi"
[13]:: back to original dir
[14]popd
[15]goto :eof
[16]
[17]:: parameters are
[18]:: extent (1)
[19]:: quoted date-time(2)
[20]:: quoted-filename (3)
[21]:: quoted-full-sourcefilename(4)
[22]:process
[23]:: grab the last 8 characters of the filename as a "File Date"
[24]set yfd=%~3&set yfd=!yfd:~-8!
[25]:: if these are <20791232 then we have a date-suffixed name, so skip
[26]if %yfd% lss 20791232 echo %4 already suffixed
[27]if %yfd% lss 20791232 goto :eof
[28]:: so we need to copy this file. Examine the date part
[29]:: my date format is dd/mm/yy.
[30]set yfd=%~2
[31]set yfd=20%yfd:~6,2%%yfd:~3,2%%yfd:~0,2%
[32]:: if reformatted-date is >20791232, is 20th-century - subtract 1000000
[33]if %yfd% gtr 20791232 set /a yfd=%yfd% - 1000000
[34]ECHO copy %4 "%ydf%%yfd%.%1"
[35]
[36]goto :eof
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO keyword needs to be removed to activate the copy It is there as a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
to be evaluated as the CURRENT value of VARNAME - that is, as modified by
the operation of the FOR

ye notes:
I wrote this using XP. It's possible that a few items may need to be
adjusted
lines starting with a double-colon are comments.
[4] It's important that the directory name inserted here is a case-match for
the actual name, otherwise the substitute in [12] may not take place
properly
[8] Not sure whether 2000 supports pushd with an argument. Might need
[8] pushd&cd %yds%
[12] I used txt and pdf for testing. The extensions you use are up to you.
Note that there may be some confusion with a file's short filename. For
instance, the file "something.HTML" will be detected by the for/r command as
matching "*.htm" because the files SHORT filename will be ...HTM
[24] Not sure whether the "-n" syntax works in 2K
[31] Since you've not specified the date format that you use, I've used the
format that I use which is dd/mm/yy. To adjust this, the formula can be
extracted from the documentation for set

SET /?

from the prompt (or anycommand /? from the prompt for (sometimes-cryptic)
help on anycommand)
- this may also aid in understanding the substitution used in [12]

FOR /? and alt.msdos.batch.nt may also be of aid in decoding some of the
more obscure aspects of this kind of processing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top