Search code examples
windowsbatch-fileoutlookbatch-rename

Renaming file and attaching to outlook


I have a batch file collecting some user machine data. At the end I want to rename the file test.txt to %username%%date%time%.txt. The code I found on other sites will not rename the file. What am I missing?

ren "c:\users\%username%\Desktop\test.txt" "%username%-%date:~-4,2%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:`-8,2%.txt"

This line below is to attach the renamed file above to a new Outlook message with a .zip folder. Am I off base here as well?

"C:\Program Files\Microsoft Office\Office12\Outlook.exe" /a "c:\users\%username%\Desktop\"%username%-%date:~-4,2%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:`-8,2%.txt"

Solution

  • The very last part of your rename is using the wrong character. Change

    %time:-8,2%.txt`
    

    to

    %time:~-8,2%.txt
    

    Also, in the second line, you have an extra " after Desktop\