Any way to make a batch script to rename the most recent file?

Page 1 of 2 12 LastLast

  1. Posts : 100
    WINDOWS 10
       #1

    Any way to make a batch script to rename the most recent file?


    found scripts to locate a the most recent file, and scripts to rename files, but no combination.

    basically id need a script to rename, the most recent .pdf file added to "W:\Declarations", to 'decs365.pdf'

    any help is greatly appreciated
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @ReallyFrustratd,

    ReallyFrustratd said:
    Found scripts to locate a the most recent file, and scripts to rename files, but no combination.

    Basically id need a script to rename, the most recent .pdf file added to "W:\Declarations", to 'decs365.pdf'
    Perhaps if you posted the scripts you have, it will give us a better idea of exactly what you are referring to!

    Anyway, this will hopefully get you STARTED.

    I am NOT able to test this at present, but I think you would want something along the lines of [ save the .bat file in the W:\Declarations directory ] . . .

    Code:
    @echo off
    for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
      set LastFileCreated=%%i
    )
    echo  The Last File Created is %LastFileCreated%
    pause
    :: ren %LastFileCreated% "decs365.pdf"
    pause

    If the output shows the correct file, then remove the :: from the code. This should then rename the file.

    IMPORTANT: This is just Pseudo code. Test at your OWN RISK.

    I hope this helps.
      My Computer


  3. Posts : 100
    WINDOWS 10
    Thread Starter
       #3

    Paul Black said:
    Hello @ReallyFrustratd,



    Perhaps if you posted the scripts you have, it will give us a better idea of exactly what you are referring to!

    Anyway, this will hopefully get you STARTED.

    I am NOT able to test this at present, but I think you would want something along the lines of [ save the .bat file in the W:\Declarations directory ] . . .

    Code:
    @echo off
    for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
      set LastFileCreated=%%i
    )
    echo  The Last File Created is %LastFileCreated%
    pause
    :: ren %LastFileCreated% "decs365.pdf"
    pause

    If the output shows the correct file, then remove the :: from the code. This should then rename the file.

    IMPORTANT: This is just Pseudo code. Test at your OWN RISK.

    I hope this helps.
    first of all, thank you
    unfortunately I didn't save the scripts I found because they didn't work
    I should have been more specific, well id like to have a batch file on the desktop, with a script that looks for the newest .pdf file in specified paths namely W:\Declarations\Alpha\; W:\Declarations\Beta\; W:\Declarations\Gama\ and so on, and renames it/them to descs365, the existing file names are never the same, so thats why id like it to go for the newest one in there, instead of a specific name. And I have to do this at the start of every month
      My Computer


  4. Posts : 7,607
    Windows 10 Home 20H2
       #4

    Paul Black said:
    Code:
    @echo off
    for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
      set LastFileCreated=%%i
    )
    echo  The Last File Created is %LastFileCreated%
    pause
    :: ren %LastFileCreated% "decs365.pdf"
    pause
    I ran your code and got the following output:

    Any way to make a batch script to rename the most recent file?-pauls-codes-output.jpg
      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #5

    Matthew Wai said:
    I ran your code and got the following output:

    Any way to make a batch script to rename the most recent file?-pauls-codes-output.jpg

    Did you have a .PDF file named A?

    As I said . . .

    Paul Black said:
    Anyway, this will hopefully get you STARTED.

    I am NOT able to test this at present, but I think you would want something along the lines of [ save the .bat file in the W:\Declarations directory ] . . .

    IMPORTANT: This is just Pseudo code. Test at your OWN RISK.
      My Computer


  6. Posts : 7,607
    Windows 10 Home 20H2
       #6

    Paul Black said:
    Did you have a .PDF file named A?
    The file name is A PDF file.pdf, as shown in my screenshot above.
    Your code does not support a name containing spaces.

    Paul Black said:
    IMPORTANT:This is just Pseudo code. Test at your OWN RISK.
    No risk at all. A PDF file.pdf is for testing only.
      My Computer


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #7

    Matthew Wai said:
    Your code does not support a name containing spaces.
    Look Matthew, it was to give the OP an idea and START of what he probably wanted in the code. I COULDN'T test it as I have already said. I was on a train on the underground so I quickly put that together for him!

    You could have just put something like . . . for files with spaces change the code to . . . for example to help the OP instead of criticising what I put!

    Perhaps you could post some code for him?
    Last edited by Paul Black; 15 Dec 2020 at 05:32.
      My Computer


  8. Posts : 3,274
    Win10
       #8

    Does this work ?
    Code:
    @echo off
    
    
    for /f "tokens=*" %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
      set LastFileCreated="%%i"
    )
    echo  The Last File Created is %LastFileCreated%
    
    pause
    
    echo  ren %LastFileCreated% "decs365.pdf"
    
    pause
      My Computers


  9. Posts : 7,607
    Windows 10 Home 20H2
       #9

    Paul Black said:
    I was on a train on the underground so I quickly put that together for him!
    I admire your efforts to help people for 24 hours a day!
      My Computer


  10. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #10

    Matthew Wai said:
    I admire your efforts to help people for 24 hours a day!
    Thank you, that's what this forum is all about, helping people to resolve their problems/issues!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:34.
Find Us




Windows 10 Forums