我试图找到一种方法,基于XML文件输入在Windows中创建完整文件夹/子文件夹/文件(快捷方式)树状图。Windows:基于XML文件创建文件夹结构
我的XML看起来是这样的:
<folder name="Folder1">
<shortcut url="http://A.com" name="A" />
<shortcut url="http://B.com" name="B" />
<folder name="1.1">
<shortcut url="http://C.com" name="C" />
</folder>
<folder name="Folder1.2">
<shortcut url="http://D.com" name="D" />
</folder>
</folder>
<folder name="Folder2">
...
</folder>
并将得到的文件夹为:
- Folder1中
- A.url
- B.url
- Folder1中。 1
- C.url
- Folder1.2
- D.url
- 文件夹2
- ...
- >综上所述,递归文件夹/子文件夹的创建,再加上快捷方式(的.url文件)的创建
如何做到这一点任何想法? 通过cmd,powershell?
(如果在的.url无法创建文件,我将手动让他们(超过300 ...))
非常感谢!
编辑:解
感谢@rojo,良好的方向发展。 我修改了html文件创建“快捷方式”的需求。 (内容截断,仅供示例) 我添加了一个目标路径,几个错误处理(创建文件夹和文件,创建错误txt文件,易于搜索以手动修复),以及在根目录下创建文件。 (不是在子文件夹)
可能不是很优化,但也...
<# : Batch Portion
@echo off & setlocal
powershell -noprofile -noexit -noninteractive "iex (gc \"%~f0\" | out-string)"
goto :EOF
: End Batch/begin PowerShell hybrid chimera #>
[xml]$DOM = gc clv.xml
$destPath="D:\Test\Folders"
function CreateShortcut([string]$target, [string]$saveLoc, [string]$fileName) {
$aspxText= @"
<html>
<body>
<a href="$target">Target URL</a>
</form>
</body>
</html>
"@
try{
New-Item ($saveLoc+'/'+$fileName+'.aspx') -type file -value $aspxText -ea Stop
}
catch{
[email protected]"
Name:
$fileName
Url:
$target
Error:
$_.Exception.GetType().FUllname
"@
New-Item ($saveLoc+'/###Error.txt') -type file -value $txtDoc
}
write-host "$($saveLoc)\$($fileName)" -f cyan
}
cd $destPath
function launchCreation($root){
$rootshortcuts = @($root.shortcut)
if($rootshortcuts -ne $null){
foreach ($shortcut in $rootshortcuts) {
$fixedShortcutName=$shortcut.name -replace '[<>:"\/\\?\*\|]', '-'
$urlfile = (pwd).Path
CreateShortcut $shortcut.url $urlfile $fixedShortcutName $shortcut.isDoc $shortcut.isTaxo
}
}
Walk($root)
}
function Walk($root) {
$folders = @($root.folder)
if($folders -ne $null){
foreach ($folder in $folders) {
$folderName=$folder.name -replace '[<>:"\/\\?\*\|]', '-'
if (-not (test-path $folderName)) { md $folderName }
cd $folderName
write-host (Join-Path $destPath $folderName) -f magenta
$shortcuts = @($folder.shortcut)
if($shortcuts -ne $null){
foreach ($shortcut in $shortcuts) {
$fixedShortcutName=$shortcut.name -replace '[<>:"\/\\?\*\|]', '-'
$urlfile = (pwd).Path
CreateShortcut $shortcut.url $urlfile $fixedShortcutName $shortcut.isDoc $shortcut.isTaxo
}
}
Walk $folder
cd..
}
}
}
[void](launchCreation $DOM.documentElement)
使用PowerShell(PowerGUI的为典型),如果你知道C#或使用Excel VBA(就像VB6 IDE运行和调试.NET类)如果你现在用vb6 – SalientBrain
用脚本很简单。我会使用powershell,但这更多的是个人喜好。使用你知道的或你想学的东西。我会建议搜索“powershell xml”和cmdlet“new-item”。如果您遇到问题,请在此处询问,我们会提供帮助,但在这一点上,问题太广泛了。更多的请求/订单,而不是QA问题。 –
用基于rojo的有用答案的代码编辑我的问题。 – okp