In a file . bat (Batch) how to rename a file using variables such as %userprofile%?

Asked

Viewed 5,532 times

5

I am wanting to rename a file that is found on the Desktop using batch script. The only line that’s going wrong is this:

rename "%userprofile%\Desktop\Lista.xlsb" "%userprofile%\Desktop\Lista - %usuario%.xlsb"

Right after this line, appears:

Command syntax is incorrect.

I researched a lot, both in Portuguese and in English for a solution and found nothing. I went after pages that explained about the Ren/Rename syntax. I used Ren instead of Rename, I removed the quotation marks, I exchanged quotation marks, I tested everything and nothing. Always giving syntax error.

When I enter the folder, I run without telling the command ren the way %userprofile%\Desktop\, it performs without error:

cd %userprofile%\Desktop\
rename "Lista.xlsb" "Lista - teste.xlsb"

Does this mean Rename does not work with variables? This is the only solution?

1 answer

2


Understand that the command REN and the RENAME, do not work as the command mv of linux (where you define the path complete to move or rename).

In the REN you need to enter the source file path in the first parameter, already in the second parameter, you only enter the new name.

As an example in your case:

REN "%userprofile%\Desktop\Lista.xlsb" "Lista - %username%.xlsb"

I made the exchange of %usuario% for %username%, because it is the correct name of the user at issue in the Windows 7.

Test there and see if it works.

  • 1

    Bulfaitelo, very grateful for the answer! I will test when I return to work next week and return to see if it worked. I only have Mac at home. I could install a VM software, use a Windows ISO, but I prefer to use the work computer. As for %user%, I used this variable because at the beginning of the code I use a set for user input in this variable, putting its name. But maybe not even needed with the %username%, which would be even better. I will test everything and come back to return. Thanks!

  • 1

    @Jonessantiago Vlw good, holiday! has other variables that can be useful in these bat after a look. LINK MS

  • 1

    It worked! The tip of using %username% was better than asking the user his name and using this variable in the name of the file, since it is less work for the system user to use the file: the less you type, the better. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.