批处理脚本可复制txt文档中列出的文件并保留重复项 [英] batch script to copy files listed in a txt document and keep duplicates

查看:164
本文介绍了批处理脚本可复制txt文档中列出的文件并保留重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本文件中列出的数千个图像文件从多个文件夹复制到一个文件夹。如果所有文件名都不相同,我拥有的脚本将起作用。如何获得重命名重复项或重命名所有重复项的信息?两者皆可,因为名称并不重要。

I am trying to copy thousands of image files listed in a text file from multiple folders to one folder. The script I have works if all the file names were different. How do I get it to rename duplicates or rename all of them? Either will work as the name isn't important.

示例列表

G:\research_data\an\an01\DCIM\100MSDCF\DSC04450.JPG
G:\research_data\an\an01\DCIM\100MSDCF\DSC04076.JPG
G:\research_data\an\an01\DCIM\100MSDCF\DSC03141.JPG
G:\research_data\an\an01\DCIM\120MSDCF\DSC04840.JPG
G:\research_data\an\an02\DCIM\100MSDCF\DSC04450.JPG
G:\research_data\an\an02\DCIM\112MSDCF\DSC04076.JPG
G:\research_data\an\an03\DCIM\102MSDCF\DSC03141.JPG
G:\research_data\an\an03\DCIM\105MSDCF\DSC04450.JPG
G:\research_data\an\an03\DCIM\106MSDCF\DSC04076.JPG

代码:

@echo off
for /f "tokens=* delims=" %%a in ('type "L:\an_2017\image_list.txt"') do xcopy /hrkvy "%%a" "L:\an_2017"
pause


推荐答案

我看到了您遇到的问题。您似乎正在尝试从各种目录传输图像,其中某些包含相同的文件名。如果您希望保留两个文件,则可以使用 FOR 语句读取文本文件,并将手边的文件重命名为 DSC {Count} 来自 1 +

I see the issue you're running into. Looks like you're trying to transfer images from various directories with some containing the same file names. If you wish to keep both files, you could use an FOR statement to read your text file and rename the file at hand to DSC{Count} from 1+.

使用 enabledelayedexpansion 我们可以使用 set / a count + = 1 来计算一个数字,并将 %% a 重命名为一个号码。请记住,我正在使用 %%〜xa 从文本文件获取每个文件的扩展名。此处的更多信息:参数扩展

Using enabledelayedexpansion we can use set /a count += 1 to count up one number and rename %%a to a number. Please keep in mind I'm using %%~xa to get the extension of each file from the textfile. More info here: Parameter Extensions

下面的脚本将读取 image_list.txt 文件中的所有文件位置,并将基于新名称 DSC1-DSCinfinity 的每个项目复制到其目标目录中。 image_list.txt 中有多少个项目。这样可以避免文本文件中名称重复的任何问题。

The script bellow will read all file locations in the image_list.txt file and will copy each item to it's target directory with a new name of DSC1-DSCinfinity based on how many items are in image_list.txt. This avoids any issue's with duplicate names in your text file.

@ECHO OFF
@setlocal enableextensions enabledelayedexpansion

rem configure directories
set "source=L:\an_2017\image_list.txt"
set "target=L:\an_2017"

rem rename files to DSC{Count}
set /a count = 1
for /f "tokens=* delims=" %%a in ('type "%source%"') do (

copy "%%a" "%target%\DSC!count!%%~xa"
set /a count += 1

)

goto :EOF

这篇关于批处理脚本可复制txt文档中列出的文件并保留重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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