2014-12-11 138 views
-1

一直googling如何使用文件名删除文件,我还没有发现任何将有我的相同的场景。我对这个批处理文件脚本非常新颖。批处理文件脚本删除文件夹中重复的文件

问题: 我有1个文件夹文件有很多文件,大多数文件有4到5个其他文件的副本。这些文件之间唯一的区别是包含在文件名中的时间戳。

sssss_1020102_201412101123 
sssss_1020102_201412101124 
sssss_1020102_201412101125 
sssss_1020102_201412101126 

我要转移的所有复制到其他文件夹,并有sssss_1020102_201412101123保持我的文件夹中的第一个文件。

for /r %%f in (*) do (
set filename=%%~nxf 
set rootname=!filename:~0,13! 
IF "!rootname!"=="" (
    move "%source_folder%%filename%" %otherfolder% 
) ELSE (%temp%=!filename:~0,13! 
     ) 

我想获得的文件名的前几个字符,并将其分配给一个变量,但仍然无法正常工作。

感谢您的帮助。

回答

0
@echo off 
setlocal 

for /F "tokens=1-3 delims=_" %%a in ('dir /B /A-D') do (
    if not defined file[%%a_%%b] (
     rem Is the first file: define its variable 
     set "file[%%a_%%b]=1" 
    ) else (
     rem Not the first file: move it 
     move "%%a_%%b_%%c" otherFolder 
    ) 
) 

编辑实例会议添加

C:\ dir files 
El volumen de la unidad C no tiene etiqueta. 
El número de serie del volumen es: 0895-160E 

Directorio de C:\Users\Antonio\Documents\test\files 

11/12/2014 11:03 a. m. <DIR>   . 
11/12/2014 11:03 a. m. <DIR>   .. 
11/12/2014 11:01 a. m.    112 sssss_1020102_201412101123 
11/12/2014 11:01 a. m.    112 sssss_1020102_201412101124 
11/12/2014 11:01 a. m.    112 sssss_1020102_201412101125 
11/12/2014 11:01 a. m.    112 sssss_1020102_201412101126 
       4 archivos   448 bytes 
       2 dirs 423,203,606,528 bytes libres 

C:\ type test.bat 
@echo off 
setlocal 

CD FILES 

for /F "tokens=1-3 delims=_" %%a in ('dir /B /A-D') do (
    if not defined file[%%a_%%b] (
     rem Is the first file: define its variable 
     set "file[%%a_%%b]=1" 
    ) else (
     rem Not the first file: move it 
     ECHO move "%%a_%%b_%%c" otherFolder 
    ) 
) 

C:\ test 
move "sssss_1020102_201412101124" otherFolder 
move "sssss_1020102_201412101125" otherFolder 
move "sssss_1020102_201412101126" otherFolder 
+0

你有降级我的职务问题,但你甚至不能回答我的询问。 – chenz101 2014-12-11 09:14:51

+0

没有办法知道哪个用户下了问题或答案,但我没有降低它的评分。你测试了我的解决方案吗?我使用您的示例名称对其进行了测试,并将结果发布在答案中! – Aacini 2014-12-11 17:27:08

+0

聚苯乙烯 - 我upvoted你的问题只是为了弥补... – Aacini 2014-12-11 17:35:03

相关问题