Batch file to detect only part of a filename

99封情书 提交于 2019-12-12 05:37:10

问题


I need a snippet of a batch file that can detect part of a filename, then rename it. Note that the numbers after the filename change randomly but "file" is always the same.

Example:

filename: file143424

There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.


回答1:


Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?

In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:

for %%f in (file*) do ren %%f file

If this is not what you want, then give us more details (you showld always give full details from the very beginning).




回答2:


You can check for the first four characters that way:

if "%filename:~0,4%"=="file" ...



回答3:


@Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:

RENAME file* file


来源:https://stackoverflow.com/questions/10969981/batch-file-to-detect-only-part-of-a-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!