2016-01-20 107 views
-1

我試圖找到一種方法,基於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) 
+0

使用PowerShell(PowerGUI的爲典型),如果你知道C#或使用Excel VBA(就像VB6 IDE運行和調試.NET類)如果你現在用vb6 – SalientBrain

+5

用腳本很簡單。我會使用powershell,但這更多的是個人喜好。使用你知道的或你想學的東西。我會建議搜索「powershell xml」和cmdlet「new-item」。如果您遇到問題,請在此處詢問,我們會提供幫助,但在這一點上,問題太廣泛了。更多的請求/訂單,而不是QA問題。 –

+0

用基於rojo的有用答案的代碼編輯我的問題。 – okp

回答

1

接受了挑戰,但僅僅是爲了阻止他人試圖將XML解析爲扁平文本以進行標記和刮取。將來,請努力編寫自己的代碼,並在尋求幫助時發佈你寫的內容。

我掀起一個快速和骯髒的PowerShell腳本,做你想做的。它將XML作爲XML對象導入,然後遞歸地遍歷DOM,創建不存在的目錄並創建快捷方式。 XML 必須格式良好,才能正常工作。

這是我用來測試腳本中的XML:

<?xml version="1.0"?> 
<root> 
    <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"> 
     <shortcut url="http://E.com" name="E" /> 
    </folder> 
</root> 

而這裏的.PS1腳本:

[xml]$DOM = gc folders.xml 
$shell = New-Object -COM WScript.Shell 

function CreateShortcut([string]$target, [string]$saveLoc) { 
    $shortcut = $shell.CreateShortcut($saveLoc + ".url") 
    $shortcut.TargetPath = $target 
    $shortcut.Save() 
    write-host $shortcut.FullName -f cyan 
} 

function Walk($root) { 
    foreach ($folder in $root.folder) { 
     if (-not (test-path $folder.name)) { md $folder.name } 
     cd $folder.name 
     write-host (pwd).Path -f magenta 
     foreach ($shortcut in $folder.shortcut) { 
      $urlfile = ((pwd).Path, $shortcut.name) -join '\' 
      CreateShortcut $shortcut.url $urlfile 
     } 
     Walk $folder 
     cd .. 
    } 
} 

[void](Walk $DOM.documentElement) 

如果你寧願有一個蝙蝠腳本,只需插入這個註釋塊進入腳本的頂部,並給這個東西一個.bat擴展名:

<# : Batch Portion 
@echo off & setlocal 

powershell -noprofile -noninteractive "iex (gc \"%~f0\" | out-string)" 
goto :EOF 

: End Batch/begin PowerShell hybrid chimera #> 
+0

非常感謝!很高興有一個真正的XMl解析器,而不是依靠換行符!它指向我正確的方向,但我打破了這一切......請看看我編輯的問題與代碼 – okp

+0

循環無限期...看起來像循環在非現有的節點上... – okp

-1
@ECHO Off 
SETLOCAL 
SET "sourcedir=U:\sourcedir" 
SET "destdir=U:\destdir" 
SET "filename1=%sourcedir%\q3489447.txt" 
PUSHD "%destdir%" 
FOR /f "usebackqtokens=*" %%z IN ("%filename1%") DO (
FOR /f "tokens=1-6delims=<=>" %%a IN ('echo "%%z"') DO (
    IF "%%~b"=="shortcut url" CALL :mdshortcut %%d 
    IF "%%~b"=="folder name" MD "%%c"&CD "%%c" 
    IF "%%~b"=="/folder" CD .. 
) 
) 
POPD 

GOTO :EOF 

:mdshortcut 
MD "%~1.url" 
GOTO :EOF 

你需要改變的sourcedirdestdir設置以適合你的情況。

我用了一個名爲q3489447.txt的文件來存放我的測試數據。

  • 到目標目錄
  • 讀取文件;剝離前導空格使用合適的分隔符%%z
  • ,打破行成份
  • 如果第二部分folder name然後創建新的目錄,並改變它
  • 如果第二部分是/folder回來一個目錄級。
  • 如果第二部分是shortcut url,則調用通過第四部分的子例程,其第一部分是附加.url的所需新目錄名稱。
  • 回到原來的目錄成品

時,你可以追加2>nulmd命令來抑制一個目錄已經存在的投訴。

+0

那麼沒有捷徑? –

+0

沒有好行爲不受懲罰,是吧? – rojo

0

這種類型的問題很容易通過遞歸過程解決。以下解決方案只需顯示md & cd創建文件夾結構所需的命令;如果結果看起來正確,則從每個命令中刪除ECHO部分。我希望這種方法很簡單,所以不需要解釋。

@echo off 
setlocal EnableDelayedExpansion 

call :treeProcess < input.xml 
goto :EOF 


:treeProcess 
    set "line=:EOF" 
    set /P "line=" 
    for /F "tokens=1-5 delims=<=> " %%a in ("%line%") do (
     if "%%a" equ "folder" (
     ECHO md "%%~c" 
     ECHO cd "%%~c" 
     call :treeProcess 
    ) else if "%%a" equ "shortcut" (
     echo Create here "%%~e.url" 
    ) else if "%%a" equ "/folder" (
     ECHO cd .. 
     exit /B 
    ) 
    ) 
if "%line%" neq ":EOF" goto treeProcess 
exit /B 

這是輸出與輸入提供:

md "Folder1" 
cd "Folder1" 
Create here "A.url" 
Create here "B.url" 
md "1.1" 
cd "1.1" 
Create here "C.url" 
cd .. 
md "Folder1.2" 
cd "Folder1.2" 
Create here "D.url" 
cd .. 
cd .. 
md "Folder2" 
cd "Folder2" 
cd .. 
相關問題