Creating a batch file to append date to file names


B

bladeraptor

Hi

I am trying to create a batch file which will append the date to a
series of file that are moved from another folder. So there will need
to be the use of wildcards for the files - as the filenames will vary.

The batch sequence below is what I have so far - the problem is I did
not write this myself - I am very new to batch and as you can see the
second section of the batch - the bit that actualy does the renaming
was designed for the user to input the desired date when calling the
batch from the run or command line.

What I am trying to do is get this second portion to accept the output
of the first section of the batch - the first section creates the time
/ date reference as the input. The porblem seems to be all the
associated viariables - any help would be appreciated.

This needs to run on both 2000 and NT4

Regards

Alex Tanner

The batch is as follows:

if "%1"=="GoTo" goto %2
echo e180 BF 07 01 B4 04 CD 1A E8 02 00 89 CA E8 00 00> %temp%.\t1.src
echo e18F 86 D6 88 D0 D4 10 0D 30 30 86 C4 AB 47 C3>> %temp%.\t1.src
for %%? in (rip 180 g w q) do echo %%?>> %temp%.\t1.src
echo SET %%1=XX XX XX XX> %temp%.\t2.bat
DEBUG %temp%.\t2.bat < %temp%.\t1.src > nul
call %temp%.\t2.bat _
%0 GoTo parse %_% MM DD YC YY
:parse
set %7=%3
shift
if not "%7"=="" goto parse
for %%? in (t1.src t2.bat) do del %temp%.\%%?
set _=

IF !==!%1 ECHO USAGE: %0 date
IF !==!%1 ECHO Prepends 'date' to every filename in the current
directory
IF !==!%1 GOTO OUT
SET _=%TEMP%.\$.BAT
LFNFOR ON>%_%
FOR %%_ IN (*.*) DO ECHO REN "%%_" "%1%%_">>%_%
LFNFOR OFF
FOR %%_ IN (CALL DEL) DO %%_ %_%
SET _=
:OUT
 
Ad

Advertisements

M

Matthias Tacke

bladeraptor said:
Hi Alex again,
this ng is more appropriate then amb, but you shouldn't multipost, if
necessary you might crosspost.

The example looks like written for w9x/ME and it is unclear,
- if you want to append or prepend the date
- what date format is desired. (I strongly suggest YYYYMMDD for sorting)

My following batch includes both versions, you might rearrange the line
set mydate=
to your needs.

::AppDate.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions DisableDelayedExpansion
Call :GetDate yy mm dd
set mydate=%yy%%mm%%dd%
for %%A in (*.*) do (
echo/ren "%%~fA" "%mydate%%%~nxA"
echo/ren "%%~fA" "%%~nA%mydate%%%~xA"
)
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
::EOF::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
B

bladeraptor

Hi Matthias,

Thanks very much for your posting. I am afraid being new to batch
work, I am not sure how to implement the code you have kindly provided
- I cannot seem to get it to work as desired. What does the section of
code you have here do? Does it achieve both aims of querying the
relevant process to ascertain the date and then prepend that date to
any files it finds in the folder.

I created a batch file with your code and ran it and nothing seemed to
happen. The message returned is "The system cannot find the batch
label specified - GetDate"

I am sorry to be thick about this but could you take the original two
sections of the batch I posted and show me where your code replaces or
adds to the existing code. Does your code require any manual entry by
the user - I need this to run as a completely automated process.

Essentially a different batch file moves a selection of files into a
folder called library from a user accessible folder called temp. These
files then need to have the date prepended to their name in the
library folder. The idea is if a file with the same name is written by
the user to the temp folder, when the file is moved by the batch
process to the library folder there is no danger of the new file
overwriting the original as the batch will have renamed the original
with the date of entry into the folder.

your help is much appreciated

Regards

bladeraptor
 
B

bladeraptor

Hi Matthias

Thank you for your help and suggestions - once I had figured out how
you had kindly echoed the results rather than implementing them - I
got it to work and work well

Kind regards

Alex Tanner
 
M

Matthias Tacke

bladeraptor said:
Hi Matthias

Thank you for your help and suggestions - once I had figured out how
you had kindly echoed the results rather than implementing them - I
got it to work and work well

Hello Alex,

blame on me,I should have mentioned to remove the undesired line and the
echo on the desired one.
Glad that you found out by yourself :)
 
B

bladeraptor

Hi Matthias,

Run into another problem - one I should have thought about earlier. I
need this process to rename folders and subdirectories as well.

Literally if a user moves a folder structure into the first folder
called temp and then this folder structure and files are moved by the
batch process to the folder called library - all those folders and
files in the structure will need to be renamed to avoid overwrite. Is
there any way that this sequence of commands could be adapted to parse
subdirectories and rename all files and sub folders

Many thanks

Alex Tanner
 
Ad

Advertisements

M

Matthias Tacke

bladeraptor said:
Hi Matthias,

Run into another problem - one I should have thought about earlier. I
need this process to rename folders and subdirectories as well.

Literally if a user moves a folder structure into the first folder
called temp and then this folder structure and files are moved by the
batch process to the folder called library - all those folders and
files in the structure will need to be renamed to avoid overwrite. Is
there any way that this sequence of commands could be adapted to parse
subdirectories and rename all files and sub folders

Many thanks

Alex Tanner
No problem either, but we need a bit different approach then,
still assuming this batch is called from inside the folder with the
files/folders to be changed.
To be shure we don't change folder names during the run we need two runs
first with folders, the second with maybe then already changed folder
names.
So you've to remove lines and echo two times.

::AppDateFileNFolder.cmd:::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions DisableDelayedExpansion
Call :GetDate yy mm dd
set mydate=%yy%%mm%%dd%
for /F "tokens=*" %%A in ('dir /AD /S /B .') do (
echo/ren "%%~fA" "%mydate%%%~nxA"
echo/ren "%%~fA" "%%~nA%mydate%%%~xA"
)
for /F "tokens=*" %%A in ('dir /A-D /S /B .') do (
echo/ren "%%~fA" "%mydate%%%~nxA"
echo/ren "%%~fA" "%%~nA%mydate%%%~xA"
)
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
::EOF::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


HTH
 
B

bladeraptor

Hi Matthias,

Many thanks for the code - very nice using the dir function.
Unfortunately for reasons I have not been able to understand. The
batch only renames the top level folder and all the foldes below it
remain with their original names.

I have tried a combination of the dir switches but have been unable to
get it to change the subfolders

Regards

Alex Tanner
 
M

Matthias Tacke

bladeraptor said:
Hi Matthias,

Many thanks for the code - very nice using the dir function.
Unfortunately for reasons I have not been able to understand. The
batch only renames the top level folder and all the foldes below it
remain with their original names.

I have tried a combination of the dir switches but have been unable to
get it to change the subfolders

I did think zhis only halfway, that argument applies also to the folders
themselves. To avoid this we have do sort the folders reverse, so
subfolders are renamed before the folders.
Include here a /O-N:
for /F "tokens=*" %%A in ('dir /AD /O-N /S /B .') do (

HTH
 
B

bladeraptor

Hi Matthias,

I made the changes to the code you suggested so it now looks like this
- however it still fails to rename the subfolders. I am not sure if I
am implementing it properly

Regards

Bladeraptor

::AppDateFileNFolder.cmd:::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions DisableDelayedExpansion
Call :GetDate yy mm dd
set mydate=%yy%%mm%%dd%
for /F "tokens=*" %%A in ('dir /AD /O-N /S /B .') do (
ren "%%~fA" "%mydate%%%~nxA"
ren "%%~fA" "%%~nA%mydate%%%~xA"
)
for /F "tokens=*" %%A in ('dir /A-D /S /B .') do (
ren "%%~fA" "%mydate%%%~nxA"
ren "%%~fA" "%%~nA%mydate%%%~xA"
)
goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
: GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
::EOF::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

bladeraptor said:
Hi Matthias,

I made the changes to the code you suggested so it now looks like this
- however it still fails to rename the subfolders. I am not sure if I
am implementing it properly


No Alex, it was me shooting to fast again. The dir/O-N works only inner
folder bounds. That couldn't be right. It took me some time to test a
new version (based on Timo's Faq 049) which reverses all the lines of a
directory listing in a file and working with the reversed file
afterwards. That grew a bit complex now :)
Since you didn't state if you want pre- or appended date and also didn't
remove the wrong one, I choose now to use a leading date.
All the lines with two colons :: are comments and could be removed.

::AppDateFileNFolder.cmd:::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions EnableDelayedExpansion
Call :GetDate yy mm dd
set mydate=%yy%%mm%%dd%
set DirList=%temp%\Dirlist.$$$
set DirTemp=%temp%\DirTemp.$$$
set DirRev=%temp%\DirRev.$$$
for %%A in (%DirList% %DirTemp% %DirRev%) do if exist %%A del %%A
dir /AD /S /B . >%DirList%
:: reverse DirList into DirRev
for /F %%A in ('find /v /c "" ^<%DirList%') do set /A lineCount=%%A -1
for /l %%A in (%lineCount%,-1,0) do (
more /e +%%A %DirList%>%DirTemp%
set /p DirName=<%DirTemp%
echo/!DirName!>>%DirRev%)
:: do folder rename
for /F "tokens=*" %%A in (%DirRev%) do (
if exist "%%~fA" ren "%%~fA" "%mydate%%%~nxA")
:: do filerename
for /F "tokens=*" %%A in ('dir /A-D /S /B .') do (
if exist "%%~fA" ren "%%~fA" "%mydate%%%~nxA")
for %%A in (%DirList% %DirTemp% %DirRev%) do if exist %%A del %%A
goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
::EOF::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


HTH
 
Ad

Advertisements

B

bladeraptor

Hi Matthias,

wow - fantastic job - that works really well. Ha you are not going to
like this - but as one final problem - the batch file needs to be
outside the directory that is being impacted. Otherwise each time the
batch is called it renames itself and could not then be referenced in
another batch.

Is it a major hassle or fairly easy to be able to specify the start of
the folder tree? In all cases it would probably be root\user
name\archive\temp type of structure.

Regards and thanks

bladeraptor
 
M

Matthias Tacke

bladeraptor said:
Hi Matthias,

wow - fantastic job - that works really well. Ha you are not going to
like this - but as one final problem - the batch file needs to be
outside the directory that is being impacted. Otherwise each time the
batch is called it renames itself and could not then be referenced in
another batch.

Is it a major hassle or fairly easy to be able to specify the start of
the folder tree? In all cases it would probably be root\user
name\archive\temp type of structure.
After having done all the more difficult work, what about a challenge
for you learning to batch.
In testing I issued the batch residing in the folder one level above
with ..\AppDateFileNFolder.cmd
Another way, when you place it in a folder contained in your path you
may invoke it from everywhere; it references just the current folder
with the dot in the two dir commands. You may replace this dot with %1
this way being able to supply the folder to work on via an argument
from the command line.
 
Ad

Advertisements

B

bladeraptor

Hi Matthias

Yes I was just being an idiot - once I replaced the dot in the dir
section with the path it all worked fine.

Many thanks for your help.

Your final solution was very elegant and works well

Regards

bladeraptor
 

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

Similar Threads


Top