How can we use the current directory path(%~dp0) w

2019-08-14 08:44发布

问题:

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'.

回答1:

adding quotes will work:

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

or

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

Thanks wOxxOm !



回答2:

If you want to use relative path also / is incorrect in windows, use \ instead.

If you want to search which relative path is correct, use the command dir

example:

dir .\
dir ..\..\
dir \
dir .
dir ..

Update

ren subdirectory\*.log* file2.txt

Your rename mask seems dangerous!

I don't know if you can rename many files with mask wildcard into the same name. I think it will override every files into one, or at least it will concatenate into single file but not sure. If you want to see some examples of renaming mask, see this: How does the Windows RENAME command interpret wildcards?

UPDATE

Small batch to see your mask in action:

@echo off

md Somefolder
echo some content into _file1.log>_file1.log
echo some content into _file2.log>_file2.log
echo some content into _file3.log>_file3.log
echo some content into _file999.log>_file999.log

dir Somefolder
pause>NUL

REM Correct
ren Somefolder\_file???.log _file???.txt
dir Somefolder
pause>NUL

REM Incorrect because it override _file???.txt into one _file1.log
ren Somefolder\_file???.txt _file1.log
dir Somefolder

pause