windows windows批处理文件脚本将每十个文件从一个文件夹复制到另一个文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5560371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
windows batch file script to copy every tenth file from a folder to another folder
提问by techdaemon
I have numerous text files in a directory and I want to select every tenth file (i.e. 10th, 20th, 30th, 40th, etc. file) according to name/alphabetical arrangement of the files and copy them to another folder.
我在一个目录中有许多文本文件,我想根据文件的名称/字母顺序选择每十个文件(即第 10、20、30、40 等文件)并将它们复制到另一个文件夹。
For example:
例如:
I have a folder C:\documents\source\ that contains the files:
我有一个文件夹 C:\documents\source\ 包含文件:
12.txt
16.txt
2007.txt
2008.txt
200865.txt
2008616263.txt
a.txt
across.txt
addition.txt
album.txt
American.txt
an.txt
and.txt
April.txt
article.txt
Artist.txt
at.txt
Award.txt
Awards.txt
Awards64.txt
Bad.txt
Best.txt
breakout.txt
by.txt
Canada.txt
categories.txt
Collaboration59.txt
Dance.txt
Dark.txt
December.txt
Diva.txt
Duo.txt
earned.txt
embarked.txt
Entertainment.txt
entitled.txt
Europe60.txt
Favorite.txt
Female.txt
Fiasco.txt
first.txt
five.txt
for.txt
four.txt
Girl.txt
Glow.txt
Gone.txt
Good.txt
Grammy.txt
Group.txt
he.txt
headlining.txt
her.txt
in.txt
including.txt
Kanye.txt
kicked.txt
Lupe.txt
Margeaux.txt
Monster.txt
MTV.txt
Music.txt
NERD.txt
nominated.txt
nominations.txt
of.txt
off.txt
on.txt
or.txt
other.txt
Performance.txt
PopRock.txt
R&B.txt
RapSung.txt
receiving.txt
Record.txt
Recording.txt
referred.txt
Rihanna.txt
second.txt
September.txt
several.txt
she.txt
shows.txt
Single.txt
Song.txt
SoulR&B.txt
States.txt
success.txt
support.txt
the.txt
then.txt
to.txt
tour.txt
United.txt
Video.txt
was.txt
Watson.txt
Weekly.txt
West.txt
which.txt
winning.txt
with.txt
won.txt
wrote.txt
Year.txt
Year58.txt
I want the files album.txt, Awards64.txt, December.txt, Fiasco.txt, Group.txt, Monster.txt, other.txt, second.txt, support.txt and West.txt copied to the folder C:\documents\output\
我要将文件专辑.txt、Awards64.txt、December.txt、Fiasco.txt、Group.txt、Monster.txt、other.txt、second.txt、support.txt 和 West.txt 复制到文件夹 C:\文件\输出\
How do I write a batch file that can achieve this goal?
如何编写可以实现此目标的批处理文件?
回答by Joey
Just count while iterating through the list of files:
只需在遍历文件列表时进行计数:
@echo off
set Counter=0
for %%f in (*.txt) do call :p "%%f"
goto :eof
:p
set /a Counter+=1
set /a X=Counter %% 10
if %X%==0 copy %1 C:\Documents\Output
goto :eof
Should work, but cannot test since I (rightfully) have no permissions to write into the root directory ;-)
应该可以工作,但无法测试,因为我(理所当然地)没有写入根目录的权限;-)
To adapt to every eighth file or so, just change the 10
in set /a X=Counter %% 10
.
为了适应每八个文件左右,只是改变10
在set /a X=Counter %% 10
。
Note though, that while NTFS outputs file lists sorted this is not the same sorting you'd get from Explorer (or anything else that respects the sorting method set in the regional and language options).
但请注意,虽然 NTFS 输出文件列表已排序,但这与您从资源管理器(或其他任何尊重区域和语言选项中设置的排序方法)获得的排序不同。
回答by Ray Kinsella
Can you change the file names? Easy solution would be to mark each 10th file depending on what your output does to create them. Then just copy them with wildcards and your marker.
可以改文件名吗?简单的解决方案是根据您的输出创建它们的目的标记每个第 10 个文件。然后只需使用通配符和标记复制它们。
回答by Aleks
added some modifications for make couple of disks from one folder: Dirty, but works:). For every disk "offs" var and target folder must be changed. Other as in original script
添加了一些修改以从一个文件夹制作几个磁盘:脏,但有效:)。对于每个磁盘“关闭”变量和目标文件夹都必须更改。其他如原始脚本
@echo off
set Counter=0
set offs=0
for %%f in (*.mp3) do call :p "%%f"
goto :eof
:p
set /a c2=Counter-offs
set /a X=c2 %% 8
set /a Counter+=1
if %X%==0 copy %1 d:\avtcd_tgt\cd1
goto :eof