Rename filename using another files name from the same folder

寵の児 提交于 2019-12-25 02:01:31

问题


I currently have a windows folder with only two files in it: vacation.jpg and summer.avi

What I want to to is rename summer.avi in vacation.avi by using the text that is before the .jpg extension.

So for example if I have another folder with christmas.jpg and summer.avi the .avi file would be renamed into christmas.avi.

Any help would be much appreciated!


回答1:


You can use the ~n parameter extension to grab just the filename without the extension. The script needs to be in the same directory as the jpg and avi, but if you want to run it on multiple directories, you can wrap the whole thing in a second for loop.

@echo off
setlocal enabledelayedexpansion

:: This assumes there is only one avi file in the folder
for /F %%A in ('dir /b *.jpg') do (
    set basename=%%~nA
    ren *.avi !basename!.avi
)



回答2:


This should worlk, though I haven't tested it:

@echo off
for %%i in (*.jpg) do set src=%%i
ren *.avi %src%


来源:https://stackoverflow.com/questions/28943950/rename-filename-using-another-files-name-from-the-same-folder

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