Refer to file made by date / time variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Refer to file made by date / time variable

#1 Post by drgt » 17 Oct 2021 09:10

Suppose
Dir /a-D /b >%date%%time%.txt

How to refer to that file further down the code?
Time would have advanced so %date%%time%.txt won't be found.

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

Re: Refer to file made by date / time variable

#2 Post by aGerman » 17 Oct 2021 09:41

You know, there is a "Search this forum..." field directly on top of the list view of threads ...

Obviously you're asking questions before you even tried out anything. Not only that %time% advances, it also contains colons which are not allowed in file names. Use WMIC. The FOR /F loop in this thread creates a %LocalDateTime% variable that you can cut off after 14 characters in order to contain yyyyMMddHHmmSS. This order has also the adwantage of getting properly sorted in an explorer list view. And obviously a defined variable containing the date-time string can be re-used subsequently in the code, because its value doesn't change anymore.

Steffen

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Refer to file made by date / time variable

#3 Post by drgt » 17 Oct 2021 10:02

Thank you Steffen.
I would have never found it...
Regarding the colons you mentioned, that is why I wrote "suppose'.

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

Re: Refer to file made by date / time variable

#4 Post by aGerman » 17 Oct 2021 11:28

drgt wrote:
17 Oct 2021 10:02
I would have never found it...
Do you actually believe that I recall thousands of threads and posts? Guess how I find the topics I link in your threads :wink:

Steffen

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Refer to file made by date / time variable

#5 Post by drgt » 17 Oct 2021 12:21

Please tell me because all I get is this:
https://www.mediafire.com/view/rn4h4iordpcaqpd

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

Re: Refer to file made by date / time variable

#6 Post by aGerman » 17 Oct 2021 12:43

E.g. if you want to get a timestamp as name for your file:
timestamp.png
timestamp.png (74.62 KiB) Viewed 5918 times
(yellow emphasizing is mine)
And once you know that WMIC is worth doing further investigation, just do it.

Steffen

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Refer to file made by date / time variable

#7 Post by drgt » 17 Oct 2021 13:03

Thanks again. I will try again when I get to pc

Post Reply