目标:
文件夹test有以下文件:
i (1)_1.png
i (1)_2.png
i (1)_3.png
i (1)_4.png
i (2)_1.png
i (2)_2.png
i (2)_3.png
i (2)_4.png
i (3)_1.png
i (3)_2.png
i (3)_3.png
i (3)_4.png
he (1)_1.png
he (1)_2.png
he (1)_3.png
he (1)_4.png
he (2)_1.png
he (2)_2.png
he (2)_3.png
he (2)_4.png
he (3)_1.png
he (3)_2.png
he (3)_3.png
he (3)_4.png
she (1)_1.png
she (1)_2.png
she (1)_3.png
she (1)_4.png
she (2)_1.png
she (2)_2.png
she (2)_3.png
she (2)_4.png
she (3)_1.png
she (3)_2.png
she (3)_3.png
she (3)_4.png
想蒋文件按照关键字i, he, she 分类,以及按照“_”后的关键字分类,以达到以下预期结果:
文件夹 i
文件夹 1
i (1)_1.png
i (2)_1.png
i (3)_1.png文件夹 2
i (1)_2.png
i (2)_2.png
i (3)_2.png文件夹 3
i (1)_3.png
i (2)_3.png
i (3)_3.png文件夹 4
i (1)_4.png
i (2)_4.png
i (3)_4.png
文件夹 he
文件夹 1
he(1)_1.png
he (2)_1.png
he (3)_1.png文件夹 2
he (1)_2.png
he (2)_2.png
he (3)_2.png文件夹 3
he (1)_3.png
he (2)_3.png
he (3)_3.png文件夹 4
he (1)_4.png
he (2)_4.png
he (3)_4.png
文件夹 she
文件夹 1
she(1)_1.png
she (2)_1.png
she (3)_1.png文件夹 2
she (1)_2.png
she (2)_2.png
she (3)_2.png文件夹 3
she (1)_3.png
she (2)_3.png
she (3)_3.png文件夹 4
she (1)_4.png
she (2)_4.png
she (3)_4.png
代码部分:
java script 代码: classification.js
var iFSO = new ActiveXObject("Scripting.FileSystemObject");
var inputdir = ".\\test";
//WScript.Echo("inputdir : " + inputdir);
var d = iFSO.GetFolder(inputdir);
var fc = new Enumerator(d.Files);
var str = "Fin";
var output = inputdir;
//WScript.Echo("output : " + output);
for (; ! fc.atEnd() ; fc.moveNext())
{
var currentFile = inputdir + "\\"+fc.item().Name;
var file = iFSO.GetFile(currentFile);
var fileExt = currentFile.split('.').pop();
var fileSName = currentFile.split('_').pop();
fileExt = fileExt.toUpperCase();
//WScript.Echo("fileExt " + fileExt);
if( fileExt == "PNG")
{
var pathFolder = currentFile.split(' (')[0];
//WScript.Echo("create folder : " + pathFolder);
//WScript.Echo("exist folder : " + pathFolder + ":" + iFSO.FolderExists(pathFolder));
if(!iFSO.FolderExists(pathFolder)){
//WScript.Echo("to create the folder" + pathFolder);
iFSO.createFolder(pathFolder);
//WScript.Echo("create folder : " + pathFolder);
}
var pathFolder2 = pathFolder +"\\" + fileSName.split('_').pop().split('.')[0];
//WScript.Echo("exist folder : " + pathFolder2 + ":" + iFSO.FolderExists(pathFolder2));
if(!iFSO.FolderExists(pathFolder2)){
//WScript.Echo("to create the folder" + pathFolder2);
iFSO.createFolder(pathFolder2);
//WScript.Echo("create folder : " + pathFolder2);
}
file.Move(pathFolder2 + "\\");
}
}
WScript.Echo(str);
bat 批命令脚: runClassification.bat
@echo on
Set WscriptNnBits=""
if %PROCESSOR_ARCHITECTURE%==x86 (set WscriptNnBits=Wscript) ELSE (set WscriptNnBits=%SystemRoot%\SysWOW64\WScript.exe)
%WscriptNnBits% .\classfication.js