Search code examples
windowsbatch-filecmdrelative-pathbatch-rename

How can we use the current directory path(%~dp0) with 'ren' command in a batch file?


I want to rename a file which is present in the sub directory and the known part of the file name is '.log'. When I try something like below

ren subdirectory\*.log* file2.txt

or

ren .\subdirectory\*.log* file2.txt

I get error : The syntax of the command is incorrect. I have two questions here:

  1. Do I always need to give full path in ren command for file1?

  2. If yes, then how can I we give the full path if I do not want to hard code it?

I got to know that we can use '%~dp0' to get the current directory path but I am not sure how to use it with 'ren'.


Solution

  • adding quotes will work:

    ren  "subdirectory\*.log*"  file2.txt
    

    or

    ren  ".\subdirectory\*.log*"  file2.txt
    

    Thanks wOxxOm !