filename datestamp issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

filename datestamp issue

#1 Post by mcranmer » 16 Mar 2012 10:34

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: filename datestamp issue

#2 Post by Ed Dyreen » 16 Mar 2012 10:45

'
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 ?

Code: Select all

@echo off
for /?
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: filename datestamp issue

#3 Post by foxidrive » 16 Mar 2012 10:54

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: filename datestamp issue

#4 Post by abc0502 » 16 Mar 2012 11:00

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

mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

Re: filename datestamp issue

#5 Post by mcranmer » 16 Mar 2012 11:06

thanks eveyone. That gives me plenty to work with. :D

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: filename datestamp issue

#6 Post by Ed Dyreen » 16 Mar 2012 11:18

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: filename datestamp issue

#7 Post by foxidrive » 16 Mar 2012 12:10

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: filename datestamp issue

#8 Post by Ed Dyreen » 16 Mar 2012 15:50


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: filename datestamp issue

#9 Post by foxidrive » 17 Mar 2012 06:10

Thanks for the link but it all about ! isn't it?

What's the simplest case that illustrates the bug?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: filename datestamp issue

#10 Post by dbenham » 17 Mar 2012 10:54

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.

jeb
Expert
Posts: 1059
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: filename datestamp issue

#11 Post by jeb » 17 Mar 2012 15:29

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: filename datestamp issue

#12 Post by Ed Dyreen » 17 Mar 2012 15:55

'
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?
)

Code: Select all

%~$PATH:I
:mrgreen: guess
Jeb, 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 :oops:

aGerman
Expert
Posts: 4715
Joined: 22 Jan 2010 18:01
Location: Germany

Re: filename datestamp issue

#13 Post by aGerman » 17 Mar 2012 16:11

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: filename datestamp issue

#14 Post by Ed Dyreen » 17 Mar 2012 16:19

'

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

aGerman
Expert
Posts: 4715
Joined: 22 Jan 2010 18:01
Location: Germany

Re: filename datestamp issue

#15 Post by aGerman » 17 Mar 2012 16:25

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

Post Reply