我的JS代码构造很差,需要使用do-while或for (i=0;i<10;i++)
这样的增量来缩短。该脚本可以正常运行,但外观不佳。
我正在尝试将VMware中的文件夹树结构导出到具有两个属性(“文件夹名称”和“文件夹路径”)的对象数组中。我使用vCenter插件查询数据。这是库存图片-
这是代码-
function getFolderPath(dc)
{
var row = [];
var VcFolders = VcPlugin.getAllVmFolders()
for each(VcFolder in VcFolders)
{
if (VcFolder.sdkConnection.name.match(dc.sdkConnection.name))
{
if (VcFolder.name == "vm")
{
if (VcFolder.parent instanceof VcDatacenter)
{
if (VcFolder.parent.name == dc.name)
{
var parentFolder = VcFolder;
for each (child in parentFolder.childEntity)
{
if (child.vimType == "Folder")
{
var current = child.parent
var path = child.name
do {
var parent = current
if (parent.name != "vm")
{
path = parent.name + "\\" + path
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child.name,
Path: path
})
for each (child1 in child.childEntity)
{
if (child1.vimType == "Folder")
{
var current = child1.parent
var path1 = child1.name
do {
var parent = current
if (parent.name != "vm")
{
path1 = parent.name + "\\" + path1
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child1.name,
Path: path1
})
for each (child2 in child1.childEntity)
{
if (child2.vimType == "Folder")
{
var current = child2.parent
var path2 = child2.name
do {
var parent = current
if (parent.name != "vm")
{
path2 = parent.name + "\\" + path2
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child2.name,
Path: path2
})
for each (child3 in child2.childEntity)
{
if (child3.vimType == "Folder")
{
var current = child3.parent
var path3 = child3.name
do {
var parent = current
if (parent.name != "vm")
{
path3 = parent.name + "\\" + path3
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child3.name,
Path: path3
})
for each (child4 in child3.childEntity)
{
if (child4.vimType == "Folder")
{
var current = child4.parent
var path4 = child4.name
do {
var parent = current
if (parent.name != "vm")
{
path4 = parent.name + "\\" + path4
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child4.name,
Path: path4
})
for each (child5 in child4.childEntity)
{
if (child5.vimType == "Folder")
{
var current = child5.parent
var path5 = child5.name
do {
var parent = current
if (parent.name != "vm")
{
path5 = parent.name + "\\" + path5
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child5.name,
Path: path5
})
for each (child6 in child5.childEntity)
{
if (child6.vimType == "Folder")
{
var current = child6.parent
var path6 = child6.name
do {
var parent = current
if (parent.name != "vm")
{
path6 = parent.name + "\\" + path6
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child6.name,
Path: path6
})
for each (child7 in child6.childEntity)
{
if (child7.vimType == "Folder")
{
var current = child7.parent
var path6 = child7.name
do {
var parent = current
if (parent.name != "vm")
{
path7 = parent.name + "\\" + path7
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child7.name,
Path: path7
})
for each (child8 in child7.childEntity)
{
if (child8.vimType == "Folder")
{
var current = child8.parent
var path6 = child8.name
do {
var parent = current
if (parent.name != "vm")
{
path8 = parent.name + "\\" + path8
}
current = current.parent
} while (current.parent != null)
row.push({
Name: child8.name,
Path: path8
})
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return row
}
vcs = VcPlugin.allSdkConnections
for each (vc in vcs)
{
var dcs = vc.getAllDatacenters()
for each (dc in dcs)
{
System.log("Getting folder path in '"+dc.name+"'...")
var paths = getFolderPath(dc)
for (i = 0; i < paths.length; i++)
{
System.log("Name:- " + paths[i].Name + " Path:- " + paths[i].Path);
}
}
}
如您所见,我已经将子代码嵌套了八次。这是一个坏习惯,因为根据环境的不同,任何时候嵌套文件夹的数量都可能超过8。我还试图将每个孩子的父母添加到“行” array
中,以至于无法思考如何使用do-while
。
这是预期的输出-
[2019-12-05 12:37:31.276] [I] Getting folder path in 'CHA-NX2'...
[2019-12-05 12:37:50.790] [I] Name:- Apps Path:- CHA-NX2\Apps
[2019-12-05 12:37:50.792] [I] Name:- Test Path:- CHA-NX2\Apps\Test
[2019-12-05 12:37:50.794] [I] Name:- HSD Path:- CHA-NX2\Apps\Test\HSD
[2019-12-05 12:37:50.796] [I] Name:- CHD02 Path:- CHA-NX2\Apps\CHD02
[2019-12-05 12:37:50.798] [I] Name:- BlueZone Servers Path:- CHA-NX2\Apps\CHD02\BlueZone Servers
[2019-12-05 12:37:50.799] [I] Name:- Cactus Servers Path:- CHA-NX2\Apps\CHD02\Cactus Servers
[2019-12-05 12:37:50.801] [I] Name:- CognosTM1 Servers Path:- CHA-NX2\Apps\CHD02\CognosTM1 Servers
[2019-12-05 12:37:50.802] [I] Name:- Facets Beta Servers Path:- CHA-NX2\Apps\CHD02\Facets Beta Servers
[2019-12-05 12:37:50.804] [I] Name:- SAS Servers Path:- CHA-NX2\Apps\CHD02\SAS Servers
[2019-12-05 12:37:50.805] [I] Name:- SLX Servers Path:- CHA-NX2\Apps\CHD02\SLX Servers
[2019-12-05 12:37:50.807] [I] Name:- Facets5 Servers Path:- CHA-NX2\Apps\CHD02\Facets5 Servers
[2019-12-05 12:37:50.808] [I] Name:- DB2Apps Servers Path:- CHA-NX2\Apps\CHD02\DB2Apps Servers
[2019-12-05 12:37:50.810] [I] Name:- FHIA Test Servers Path:- CHA-NX2\Apps\CHD02\FHIA Test Servers
[2019-12-05 12:37:50.811] [I] Name:- ISTools Servers Path:- CHA-NX2\Apps\CHD02\ISTools Servers
[2019-12-05 12:37:50.813] [I] Name:- Infoserver Servers Path:- CHA-NX2\Apps\CHD02\Infoserver Servers
[2019-12-05 12:37:50.815] [I] Name:- Hedis 2018 Servers Path:- CHA-NX2\Apps\CHD02\Hedis 2018 Servers
[2019-12-05 12:37:50.816] [I] Name:- FireFox-Chrome Servers Path:- CHA-NX2\Apps\CHD02\FireFox-Chrome Servers
[2019-12-05 12:37:50.818] [I] Name:- Facets Test Servers Path:- CHA-NX2\Apps\CHD02\Facets Test Servers
[2019-12-05 12:37:50.819] [I] Name:- Facets Release2 Servers Path:- CHA-NX2\Apps\CHD02\Facets Release2 Servers
[2019-12-05 12:37:50.823] [I] Name:- PSF Servers Path:- CHA-NX2\Apps\CHD02\PSF Servers
[2019-12-05 12:37:50.825] [I] Name:- Prism Servers Path:- CHA-NX2\Apps\CHD02\Prism Servers
[2019-12-05 12:37:50.827] [I] Name:- CCPulse Servers Path:- CHA-NX2\Apps\CHD02\CCPulse Servers
[2019-12-05 12:37:50.829] [I] Name:- Facets TSIT Servers Path:- CHA-NX2\Apps\CHD02\Facets TSIT Servers
[2019-12-05 12:37:50.831] [I] Name:- Facets Release Servers Path:- CHA-NX2\Apps\CHD02\Facets Release Servers
[2019-12-05 12:37:50.833] [I] Name:- Hedis 2019 Servers Path:- CHA-NX2\Apps\CHD02\Hedis 2019 Servers
[2019-12-05 12:37:50.834] [I] Name:- Facets SUBS Path:- CHA-NX2\Apps\CHD02\Facets SUBS
[2019-12-05 12:37:50.836] [I] Name:- TeamMate Servers Path:- CHA-NX2\Apps\CHD02\TeamMate Servers
[2019-12-05 12:37:50.837] [I] Name:- Standard Servers Path:- CHA-NX2\Apps\CHD02\Standard Servers
[2019-12-05 12:37:50.839] [I] Name:- Facets 2016 Servers Path:- CHA-NX2\Apps\CHD02\Facets 2016 Servers
[2019-12-05 12:37:50.841] [I] Name:- Facets Prod2 Servers Path:- CHA-NX2\Apps\CHD02\Facets Prod2 Servers
[2019-12-05 12:37:50.842] [I] Name:- FHIA DEV Servers Path:- CHA-NX2\Apps\CHD02\FHIA DEV Servers
[2019-12-05 12:37:50.844] [I] Name:- Facets Development Servers Path:- CHA-NX2\Apps\CHD02\Facets Development Servers
[2019-12-05 12:37:50.846] [I] Name:- Templates Path:- CHA-NX2\Apps\Templates
[2019-12-05 12:37:50.847] [I] Name:- Discovered virtual machine Path:- CHA-NX2\Discovered virtual machine
[2019-12-05 12:37:50.849] [I] Name:- Desktops Path:- CHA-NX2\Desktops
[2019-12-05 12:37:50.850] [I] Name:- Persistent Path:- CHA-NX2\Desktops\Persistent
[2019-12-05 12:37:50.852] [I] Name:- Win10 1809 x64 Developer Path:- CHA-NX2\Desktops\Persistent\Win10 1809 x64 Developer
[2019-12-05 12:37:50.853] [I] Name:- Win10 1809 x64 Java Path:- CHA-NX2\Desktops\Persistent\Win10 1809 x64 Java
[2019-12-05 12:37:50.855] [I] Name:- Win10 1809 x64 Standard Path:- CHA-NX2\Desktops\Persistent\Win10 1809 x64 Standard
[2019-12-05 12:37:50.857] [I] Name:- Win10 1709 x64 Java Path:- CHA-NX2\Desktops\Persistent\Win10 1709 x64 Java
[2019-12-05 12:37:50.859] [I] Name:- Imaging EWS Path:- CHA-NX2\Desktops\Persistent\Imaging EWS
[2019-12-05 12:37:50.861] [I] Name:- Allan H Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Allan H
[2019-12-05 12:37:50.863] [I] Name:- David F Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\David F
[2019-12-05 12:37:50.865] [I] Name:- Jack JC Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Jack JC
[2019-12-05 12:37:50.867] [I] Name:- Melville P Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Melville P
[2019-12-05 12:37:50.869] [I] Name:- Swetha B Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Swetha B
[2019-12-05 12:37:50.871] [I] Name:- Raja T Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Raja T
[2019-12-05 12:37:50.873] [I] Name:- Tim C Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Tim C
[2019-12-05 12:37:50.875] [I] Name:- Peter A Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Peter A
[2019-12-05 12:37:50.876] [I] Name:- Lee G Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Lee G
[2019-12-05 12:37:50.878] [I] Name:- Sai KV Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Sai KV
[2019-12-05 12:37:50.880] [I] Name:- Shane M Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Shane M
[2019-12-05 12:37:50.882] [I] Name:- Vjay E Path:- CHA-NX2\Desktops\Persistent\Imaging EWS\Vjay E
[2019-12-05 12:37:50.884] [I] Name:- Win10 1709 x64 Developer Path:- CHA-NX2\Desktops\Persistent\Win10 1709 x64 Developer
[2019-12-05 12:37:50.886] [I] Name:- Templates Path:- CHA-NX2\Desktops\Persistent\Templates
[2019-12-05 12:37:50.888] [I] Name:- Archive Path:- CHA-NX2\Desktops\Persistent\Templates\Archive
[2019-12-05 12:37:50.890] [I] Name:- Maintenance Devices Path:- CHA-NX2\Desktops\Persistent\Templates\Maintenance Devices
[2019-12-05 12:37:50.892] [I] Name:- Win10 x64 Standard Path:- CHA-NX2\Desktops\Persistent\Win10 x64 Standard
[2019-12-05 12:37:50.894] [I] Name:- Connect2Dev.com Path:- CHA-NX2\Desktops\Persistent\Connect2Dev.com
[2019-12-05 12:37:50.896] [I] Name:- Win7 x86 Developer Path:- CHA-NX2\Desktops\Persistent\Win7 x86 Developer
[2019-12-05 12:37:50.897] [I] Name:- Win7 x64 Developer Path:- CHA-NX2\Desktops\Persistent\Win7 x64 Developer
[2019-12-05 12:37:50.899] [I] Name:- Win10 x64 Java Path:- CHA-NX2\Desktops\Persistent\Win10 x64 Java
[2019-12-05 12:37:50.900] [I] Name:- Win7 x64 IIB Path:- CHA-NX2\Desktops\Persistent\Win7 x64 IIB
[2019-12-05 12:37:50.902] [I] Name:- Win10 Path:- CHA-NX2\Desktops\Persistent\Win10
[2019-12-05 12:37:50.903] [I] Name:- 7.12 Path:- CHA-NX2\Desktops\Persistent\7.12
[2019-12-05 12:37:50.905] [I] Name:- Win7 x64 RAD Path:- CHA-NX2\Desktops\Persistent\Win7 x64 RAD
[2019-12-05 12:37:50.906] [I] Name:- Win7 x86 Standard Path:- CHA-NX2\Desktops\Persistent\Win7 x86 Standard
[2019-12-05 12:37:50.908] [I] Name:- Win10 1703 x64 Standard Path:- CHA-NX2\Desktops\Persistent\Win10 1703 x64 Standard
[2019-12-05 12:37:50.909] [I] Name:- Win10 x64 Developer Path:- CHA-NX2\Desktops\Persistent\Win10 x64 Developer
[2019-12-05 12:37:50.911] [I] Name:- Win7 x64 Standard Path:- CHA-NX2\Desktops\Persistent\Win7 x64 Standard
[2019-12-05 12:37:50.912] [I] Name:- Provisioned Path:- CHA-NX2\Desktops\Provisioned
[2019-12-05 12:37:50.914] [I] Name:- Windows 10 Deep Security Path:- CHA-NX2\Desktops\Provisioned\Windows 10 Deep Security
[2019-12-05 12:37:50.916] [I] Name:- PackagingVMs Path:- CHA-NX2\Desktops\Provisioned\PackagingVMs
[2019-12-05 12:37:50.917] [I] Name:- Windows 10 Standard Path:- CHA-NX2\Desktops\Provisioned\Windows 10 Standard
[2019-12-05 12:37:50.919] [I] Name:- Windows 7 Dakota Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Dakota
[2019-12-05 12:37:50.920] [I] Name:- New Folder Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Dakota\New Folder
[2019-12-05 12:37:50.922] [I] Name:- Windows 7 eCopy Path:- CHA-NX2\Desktops\Provisioned\Windows 7 eCopy
[2019-12-05 12:37:50.924] [I] Name:- Windows 7 Pilot Desktops Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Pilot Desktops
[2019-12-05 12:37:50.926] [I] Name:- Windows 7 Standard - Appsense Test Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Standard - Appsense Test
[2019-12-05 12:37:50.928] [I] Name:- Windows 7 Hedis Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Hedis
[2019-12-05 12:37:50.929] [I] Name:- Windows 7 Standard DSA Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Standard DSA
[2019-12-05 12:37:50.931] [I] Name:- Windows 7 Standard - Verint Dev Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Standard - Verint Dev
[2019-12-05 12:37:50.933] [I] Name:- Windows 7 Standard OfficeScan Path:- CHA-NX2\Desktops\Provisioned\Windows 7 Standard OfficeScan
[2019-12-05 12:37:50.935] [I] Name:- Windows 10 SQA Path:- CHA-NX2\Desktops\Provisioned\Windows 10 SQA
[2019-12-05 12:37:50.937] [I] Name:- Temp Path:- CHA-NX2\Desktops\Temp
[2019-12-05 12:37:50.939] [I] Name:- BSOD Path:- CHA-NX2\Desktops\Temp\BSOD
[2019-12-05 12:37:50.941] [I] Name:- Templates Path:- CHA-NX2\Desktops\Templates
[2019-12-05 12:37:50.943] [I] Getting folder path in 'CHA-N02'...
[2019-12-05 12:38:02.140] [I] Name:- ALL BCBST Path:- CHA-N02\ALL BCBST
[2019-12-05 12:38:02.142] [I] Name:- CareAdvance Path:- CHA-N02\ALL BCBST\CareAdvance
[2019-12-05 12:38:02.143] [I] Name:- Citrix Path:- CHA-N02\ALL BCBST\Citrix
[2019-12-05 12:38:02.145] [I] Name:- Health Mason Path:- CHA-N02\ALL BCBST\Citrix\Health Mason
[2019-12-05 12:38:02.147] [I] Name:- Infrastructure Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure
[2019-12-05 12:38:02.148] [I] Name:- Controllers Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\Controllers
[2019-12-05 12:38:02.150] [I] Name:- Admin Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\Admin
[2019-12-05 12:38:02.151] [I] Name:- Director Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\Director
[2019-12-05 12:38:02.153] [I] Name:- License Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\License
[2019-12-05 12:38:02.154] [I] Name:- Provisioning Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\Provisioning
[2019-12-05 12:38:02.156] [I] Name:- LoginVSI Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\LoginVSI
[2019-12-05 12:38:02.158] [I] Name:- ShareFile Controllers Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\ShareFile Controllers
[2019-12-05 12:38:02.159] [I] Name:- FAS Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\FAS
[2019-12-05 12:38:02.161] [I] Name:- StoreFront Path:- CHA-N02\ALL BCBST\Citrix\Infrastructure\StoreFront
[2019-12-05 12:38:02.162] [I] Name:- Disaster Recovery Servers Path:- CHA-N02\ALL BCBST\Disaster Recovery Servers
[2019-12-05 12:38:02.164] [I] Name:- Dynamics Servers Path:- CHA-N02\ALL BCBST\Dynamics Servers
[2019-12-05 12:38:02.165] [I] Name:- Enterprise Monitoring Servers Path:- CHA-N02\ALL BCBST\Enterprise Monitoring Servers
[2019-12-05 12:38:02.167] [I] Name:- Fax Path:- CHA-N02\ALL BCBST\Fax
[2019-12-05 12:38:02.169] [I] Name:- Fax Servers Path:- CHA-N02\ALL BCBST\Fax Servers
[2019-12-05 12:38:02.170] [I] Name:- File Servers Path:- CHA-N02\ALL BCBST\File Servers
[2019-12-05 12:38:02.172] [I] Name:- ICD10 Servers Path:- CHA-N02\ALL BCBST\ICD10 Servers
[2019-12-05 12:38:02.173] [I] Name:- Imaging Servers Path:- CHA-N02\ALL BCBST\Imaging Servers
[2019-12-05 12:38:02.175] [I] Name:- Active Directory Path:- CHA-N02\ALL BCBST\Active Directory
[2019-12-05 12:38:02.177] [I] Name:- bcbstbeta.com Path:- CHA-N02\ALL BCBST\Active Directory\bcbstbeta.com
[2019-12-05 12:38:02.178] [I] Name:- bcbstdev.com Path:- CHA-N02\ALL BCBST\Active Directory\bcbstdev.com
[2019-12-05 12:38:02.180] [I] Name:- bcbst.com Path:- CHA-N02\ALL BCBST\Active Directory\bcbst.com
[2019-12-05 12:38:02.181] [I] Name:- bcbstsit.com Path:- CHA-N02\ALL BCBST\Active Directory\bcbstsit.com
[2019-12-05 12:38:02.183] [I] Name:- bcbsttest.com Path:- CHA-N02\ALL BCBST\Active Directory\bcbsttest.com
[2019-12-05 12:38:02.185] [I] Name:- dmz.bcbst.com Path:- CHA-N02\ALL BCBST\Active Directory\dmz.bcbst.com
[2019-12-05 12:38:02.186] [I] Name:- nonprod.com Path:- CHA-N02\ALL BCBST\Active Directory\nonprod.com
[2019-12-05 12:38:02.188] [I] Name:- sdmz.bcbst.com Path:- CHA-N02\ALL BCBST\Active Directory\sdmz.bcbst.com
[2019-12-05 12:38:02.190] [I] Name:- Facets Servers Path:- CHA-N02\ALL BCBST\Facets Servers
答案 0 :(得分:1)
只是一种抽象方法(我仍然怀疑代码是Javascript而不是Java)。
这是一种yopur代码模式,您可以在其中迭代一些数据,检查类型并迭代路径。最后,您将一个新对象放入row
。
for (child in parentFolder.childEntity) { // *in* maybe wrong, just to mention ...
if (child.vimType == "Folder") {
var current = child.parent
var path = child.name
do {
var parent = current
if (parent.name != "vm") path = parent.name + "\\" + path;
current = current.parent
} while (current.parent != null)
row.push({ Name: child.name, Path: path });
// next same pattern with child.childEntity
}
}
您可以将此模式转换为函数,并交出用于迭代的数据和目标数组row
(或者如果不是嵌套函数,也可以不这样做)。
然后进行否定检查,并在没有嵌套块的情况下继续循环。
然后获取路径值并推送对象。
最后退出递归调用或使用新数据再次调用该函数。在您的代码中,您阻止了嵌套方法,在这里,您可以检查child.childEntity
是否虚假。
function iter(data, row) {
for (child in data) {
if (child.vimType !== "Folder") continue;
var current = child.parent
var path = child.name
do {
var parent = current;
if (parent.name !== "vm") path = parent.name + "\\" + path;
current = current.parent
} while (current.parent != null)
row.push({ Name: child.name, Path: path });
if (!child.childEntity) continue;
iter(child.childEntity, row);
}
}
// call
iter(parentFolder.childEntity, row);