Batch Scripting moving and renaming files

拟墨画扇 提交于 2019-12-12 04:09:54

问题


I have another question, very similar to Batch Scripting Moving files with timestamp But i'm having problems with it.

I have a file system C:\Test\Baseline - Under Baseline folder i have many folders ranging from 1 to 10+ these are all image files. I want to copy all the images inside those folders ending ONLY with -not tasty.jpg files into Baseline folder but removing the -not tasty.jpg part.

To put this into perspective here is an example:
C:\Test\Baseline: apple.jpg, orange.jpg, watermellow.jpg, strawberry.jpg, eggs.jpg
C:\Test\Baseline\07-14-14: apples-tasty.jpg, apples-not tasty.jpg, fruits-tasty.jpg, fruits-not tasty.jpg
C:\Test\Baseline\07-16-14: cherry-tasty.jpg, cherry-not tasty.jpg, orange-tasty.jpg, orange-not tasty.jpg

So in the end when i run this batch script it should take the files from 07-14-14 apple-not tasty.jpg and fruits-not tasty.jpg rename them without -not tasty.jpg --> apple.jpg and fruits.jpg move/copy to it's parent directory C:\Test\Baseline and overwrite if necessary - Also take cherry-not tasty.jpg, orange-not tasty.jpg --> cherry.jpg, orange.jpg move/copy to C:\Test\Baseline

So we are left with
C:\Test\Baseline: apple.jpg, orange.jpg, watermellow.jpg, strawberry.jpg, eggs.jpg, fruits.jpg, cherry.jpg

I hope you can understand this. Any help will be grateful. Thanks!


回答1:


Hope this will work for you -

@echo OFF

cd D:\test\baseline

for /f "delims=" %%i in ('dir /s /b "*-not tasty.*"') do copy /y "%%~fi" d:\test\baseline\* >nul 2>&1

for /f "delims=" %%i in ('dir /b "*-not tasty.*"') do CALL :rename "%%i" 
goto :EOF

:rename
set filename=%1
set newfilename=%filename:-not tasty=%
ren %filename% %newfilename% >nul 2>&1
if %errorlevel% EQU 1 del %newfilename% & ren %filename% %newfilename%

:EOF

Cheers, G



来源:https://stackoverflow.com/questions/24897725/batch-scripting-moving-and-renaming-files

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