问题
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.jpgC:\Test\Baseline\07-14-14:
apples-tasty.jpg, apples-not tasty.jpg, fruits-tasty.jpg, fruits-not tasty.jpgC:\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 withC:\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