Page 1 of 2
filename datestamp issue
Posted: 16 Mar 2012 10:34
by mcranmer
Why does the following code change a filename from
UK3_NEWPOLICY.txt to
UK3_NEWPOLICY.txt_16032012.txt?
Code: Select all
Set FileDate=%date:/=%
for %%i in (*.txt) do ren %%i %%i_%FileDate%.txt
I'm obviously getting an additional dot and file extension. What is the reason?
thanks,
Mark
Re: filename datestamp issue
Posted: 16 Mar 2012 10:45
by Ed Dyreen
'
Hello, try
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$date=!date:/=!"
for %%? in (
"*.txt"
) do echo.ren "%%~?" "%%~n?_!$date!%%~x?"
pause
exit
Code: Select all
ren "Nieuw - Tekstdocument.txt" "Nieuw - Tekstdocument_vr 16032012.txt"
Druk op een toets om door te gaan. . .
But why ?
Re: filename datestamp issue
Posted: 16 Mar 2012 10:54
by foxidrive
Ed Dyreen wrote:'
Code: Select all
for %%? in (
*.txt
) do echo.ren "%%~?" "%%~n?_!$date!%%~x?"
I have to bitch about %%? as using wildcards in a for loop has negatives, but what positives does it have?
Negatives include confusing syntax and confusing the newbies.
Why not use %%a or %%A where there is a logical connection to %%b and %%c etc?
No offense Ed, it's something I've seen many people use and I really don't see positive aspects of using special characters in metavariables. It's a bone I chew often.
Re: filename datestamp issue
Posted: 16 Mar 2012 11:00
by abc0502
here is your code :
Set FileDate=%date:/=%
for %%i in (*.txt) do ren %%i
%%i_%FileDate%.txt
Replace the red with %%~ni
i figured it out after ed post his

forget to say:
put this like ed said:
@echo off &setlocal
at the begining
Re: filename datestamp issue
Posted: 16 Mar 2012 11:06
by mcranmer
thanks eveyone. That gives me plenty to work with.

Re: filename datestamp issue
Posted: 16 Mar 2012 11:18
by Ed Dyreen
foxidrive wrote:Why not use %%a or %%A where there is a logical connection to %%b and %%c etc?
'
Good question, can't remember the topic where we discussed all of this.
I try to avoid [a-Z] by default, something with delayed expansion and the attributes of the
for command but my memory is hazy
This is one occasion I know it fail
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$=will it work ?"
for %%! in ( "$" ) do for %%a in ( "$" ) do echo. "!%%~a!"
for %%! in ( "$" ) do for %%? in ( "$" ) do echo. "!%%~?!"
pause
exit
Code: Select all
""
"will it work ?"
Druk op een toets om door te gaan. . .
jeb or dBenham are the experts here...
I like to think it's just good style, but the problem only shows up in complex scripts so..
Re: filename datestamp issue
Posted: 16 Mar 2012 12:10
by foxidrive
Edit:
Is this what you are trying to achieve?
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$=will it work ?"
for %%a in ( "$" ) do for %%b in ( "$" ) do echo. "!%%~b!"
for %%! in ( "$" ) do for %%? in ( "$" ) do echo. "!%%~?!"
pause
exit
Re: filename datestamp issue
Posted: 16 Mar 2012 15:50
by Ed Dyreen
Re: filename datestamp issue
Posted: 17 Mar 2012 06:10
by foxidrive
Thanks for the link but it all about ! isn't it?
What's the simplest case that illustrates the bug?
Re: filename datestamp issue
Posted: 17 Mar 2012 10:54
by dbenham
I have to agree with foxidrive on this one. I don't like to see non-alpha characters used as FOR variables unless an extreme situation requires it. I find it confusing and unsettling. But it is largely a matter of style.
Re: filename datestamp issue
Posted: 17 Mar 2012 15:29
by jeb
I agreee with foxidrive and Dave, I would avoid also non-alpha-numericals as FOR-variables, as it's too easy to create none obvious problems.
Like this one (reading last week)
Code: Select all
FOR /F %%$ in ("hello") do (
echo %%$ works
echo %%~$ fails, but why?
)
Re: filename datestamp issue
Posted: 17 Mar 2012 15:55
by Ed Dyreen
'
With me it's the other way around, I get confused with alpha-numericals, for some reason I always expect c to follow b to follow a. When I use '?' I know I didn't delimit.
Jou won't hate me for doing it like that will jou, I like it like that
jeb wrote:I agreee with foxidrive and Dave, I would avoid also non-alpha-numericals as FOR-variables, as it's too easy to create none obvious problems.
Like this one (reading last week)
Code: Select all
FOR /F %%$ in ("hello") do (
echo %%$ works
echo %%~$ fails, but why?
)
guessJeb, and ben are right, maybe I just do it because I want to keep those tokens free.
jeb wrote:it's too easy to create none obvious problems.
I didn't encounter any none obvious problems in my 11.000 lines batch file.
you lost me on this one jeb

Re: filename datestamp issue
Posted: 17 Mar 2012 16:11
by aGerman
Alpha characters have also side effects. For that reason it's really nothing but a question of the style one would prefer.
Code: Select all
@echo off &setlocal
del "dummy.txt" 2>nul
for %%a in ("dummy.txt") do echo %%~aa
for %%A in ("dummy.txt") do echo %%~Aa
for %%A in ("dummy.txt") do echo %%~AA
>>"dummy.txt" type nul
for %%a in ("dummy.txt") do echo %%~aa
for %%A in ("dummy.txt") do echo %%~Aa
for %%A in ("dummy.txt") do echo %%~AA
pause
Result:
Code: Select all
ECHO ist ausgeschaltet (OFF).
dummy.txta
ECHO ist ausgeschaltet (OFF).
--a------
dummy.txta
--a------
Drücken Sie eine beliebige Taste . . .
All the characters with special meaning for arguments or FOR variables have those effects.
Regards
aGerman
Re: filename datestamp issue
Posted: 17 Mar 2012 16:19
by Ed Dyreen
'
Code: Select all
@echo off &setlocal
del "dummy.txt" 2>nul
echo.test0
for %%a in ("dummy.txt") do echo %%~aa
echo.test1
for %%A in ("dummy.txt") do echo %%~Aa
>>"dummy.txt" type nul
echo.test2
for %%a in ("dummy.txt") do echo %%~aa
echo.test3
for %%A in ("dummy.txt") do echo %%~Aa
pause
Code: Select all
test0
ECHO is off (uit).
test1
dummy.txta
test2
--a--c---
test3
dummy.txta
Druk op een toets om door te gaan. . .
That's
weird, I couldn't explain it

Re: filename datestamp issue
Posted: 17 Mar 2012 16:25
by aGerman
Ed Dyreen wrote:That's
weird, I couldn't explain it

Why not?
for /?
Code: Select all
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
You see the file attributes if the file exists.
Regards
aGerman