0

I am learning to write batch files scripts and to do this I am trying to create a batch script that asks for a path and iterates over each file, getting its creation date and renaming the file.

Right now I am able to iterate and echo the file name like this:

@echo off
set /p path="Enter the folder path: "
for /f "usebackq delims=|" %%f in (`dir /b %path%`) do (
    echo %%f
)

pause

Now, the next step: How can I get the creation date from %%f? I am stuck at this and was not able to proceed from here.

JMW
  • 261
  • 2
  • 7
Ravers
  • 988
  • 2
  • 14
  • 45
  • 4
    Before you do anything else, you should absolutely change that variable name from `path` to something which isn't an important system variable name. Then you need to open a Command Prompt window, type `dir /?`, press the `[ENTER]` key, and read the usage information. Pay specific attention to the `/T` option, which gives you the option of choosing the `C`reation, last `A`ccessed, or last `W`ritten, timestamps. – Compo Jul 02 '20 at 21:34
  • 2
    Why the `|` for the delimiter option? – Squashman Jul 02 '20 at 21:36
  • 2
    Why the `UseBackQ` option? – Compo Jul 02 '20 at 21:38
  • Squashman and Compo I used this post https://stackoverflow.com/questions/180741/how-to-do-something-to-each-file-in-a-directory-with-a-batch-script to check on how to do an iteration. That's why I have that delimiter and UseBackQ option. – Ravers Jul 02 '20 at 21:46
  • 2
    You should when using your new found knowledge from the command help, also consider using the appropriate option to select only files, as opposed to both files and directories too! I would also recommend that you use the on site search facility, as renaming files according to their dates is not something which has never been asked or answered before. – Compo Jul 02 '20 at 21:49
  • @Compo I have been using your first comment and I managed to echo the date! Thanks a lot for your help! – Ravers Jul 02 '20 at 21:53
  • 4
    The post you linked is awful syntax, regardless of the number of votes or reputation of the author. It should really read `for /f "eol=| delims=" %%f in ('dir /b /a:-d "c:\program files"') do echo %%f` That answer is poor, it includes directories as well as files, ignores any which could begin with a `;` character, delimits with a character which will never occur, _(harmless)_, and uses back quotes for no reason too! – Compo Jul 02 '20 at 22:01
  • ```for /f %%f in ('dir /b %path%') do (echo %%~tf)```? – Nico Nekoru Jul 02 '20 at 22:57
  • Have you even looked at the previous comments @NekoMusume, it appears that you've missed some important points, actual or implied, from within them. – Compo Jul 02 '20 at 23:45

0 Answers0