批处理脚本移动和重命名文件 [英] Batch Scripting moving and renaming files

查看:298
本文介绍了批处理脚本移动和重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有一个问题,非常类似于 批处理脚本使用时间戳移动文件但我遇到了问题.

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

我有一个文件系统C:\ Test \ Baseline-在Baseline文件夹下,我有许多文件夹,范围从1到10+,这些都是图像文件.我想将仅以-not tasty.jpg文件结尾的那些文件夹内的所有图像复制到Baseline文件夹中,但要删除-not tasty.jpg部分.

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.

下面是一个例子:
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-notsty.jpg,orange-tasty.jpg,orange-not tasty.jpg

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

因此,最后,当我运行此批处理脚本时,应从07-14-14取出文件apple-not tasty.jpg和fruits-not tasty.jpg,将其重命名而无需-not tasty.jpg-> apple. jpg和fruits.jpg移动/复制到其父目录C:\Test\Baseline并在必要时覆盖-还可以将cherry-not tasty.jpg,orange-not tasty.jpg-> cherry.jpg,orange.jpg移动/复制到C:\Test\Baseline

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

所以我们留下了
C:\Test\Baseline: apple.jpg,orange.jpg,watermellow.jpg,strawberry.jpg,eggs.jpg,fruits.jpg,cherry.jpg

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!

推荐答案

希望这对您有用-

@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

干杯, G

这篇关于批处理脚本移动和重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆