2013-12-15 61 views
0

我需要創建大量需要從csv文件創建的目錄和子目錄。我認爲使用批次是最簡單的方法。然而,我不太熟悉批次,所以我正在尋找幫助。使用csv創建批處理目錄/文件夾

下面是CSV的某些部分(有超過800線):

FolderID,FolderName,FolderParentID,OwnerID,TopLevelFolderID 
1,My Folder,NULL,1,1 
2,My Folder,NULL,2,2 
5,Test,NULL,3,5 
195,My Folder,NULL,132,195 
174,My Folder,NULL,133,174 
133,My Folder,NULL,135,133 
154,My Folder,NULL,139,154 
134,My Folder,NULL,140,134 
137,My Folder,NULL,141,137 
136,My Folder,NULL,145,136 
191,My Folder,NULL,148,191 
160,My Folder,NULL,150,160 
147,My Folder,NULL,152,147 
165,My Folder,NULL,153,165 
157,My Folder,NULL,154,157 
158,My Folder,NULL,155,158 
142,My Folder,NULL,157,142 
192,My Folder,NULL,158,192 
153,My Folder,NULL,159,153 
186,My Folder,NULL,160,186 
177,My Folder,NULL,162,177 

我通過它認爲,這是基本的結構應該如何:

Create Folder 1 per OwnerID with OwnerID as folder name 
"/OwnerID/" 


if FolderParentID = NULL and/or is same as TopLevelFolderID, then create folder with  FolderID in OwnerID folder. 
"/OwnerID/FolderID/" 



if FolderParentID = notNULL, and FolderParentID is same as TopLevelFolderID, then create folder with FolderID in FolderParentID 
"/OwnerID/FolderParentID/FolderID/" 


if FolderParentID = notNULL, adn FolderParentID is not same as TopLevelFolderID, then create folder with FolderID in FolderID form level above 
"/OwnerID/FolderParentID/FolderID/FolderID/" 

任何幫助將不勝感激,並提前致謝!

格式編輯.. google.com

回答

0
@ECHO OFF 
SETLOCAL 
SET "destdir=c:\destdir" 
FOR /f "skip=1tokens=1-5delims=," %%a IN (q20592270.txt) DO (
    ECHO MD "%destdir%\%%d" 
) 
FOR /f "skip=1tokens=1-5delims=," %%a IN (q20592270.txt) DO (
    IF "%%c"=="NULL" ECHO MD "%destdir%\%%d\%%a" 
    IF "%%c"=="%%e" ECHO MD "%destdir%\%%d\%%a" 
) 
FOR /f "skip=1tokens=1-5delims=," %%a IN (q20592270.txt) DO (
    IF NOT "%%c"=="NULL" IF "%%c"=="%%e" ECHO MD "%destdir%\%%d\%%c\%%a" 
) 
FOR /f "skip=1tokens=1-5delims=," %%a IN (q20592270.txt) DO (
    IF NOT "%%c"=="NULL" IF NOT "%%c"=="%%e" ECHO MD "%destdir%\%%d\%%c\%%a\%%a" 
) 
GOTO :EOF 

這將創建所需的目錄。顯然,你需要指定目的地。

爲了測試目的,所需的命令僅僅是ECHO。驗證命令是否正確後,將每個ECHO MD更改爲MD以實際創建目錄。

請注意,當試圖創建一個已經存在的目錄時,將顯示一條錯誤消息。這是無害但醜陋的。您可以通過將2>nul添加到每個MD ...行來導致錯誤消息被抑制。

q20592270.txt只是我用於測試數據的文件的名稱(它總是有第三個字段「NULL」)。您需要替換您的.csv文件的名稱。

我假設數據實際上包含單詞「NULL」,它不是簡單的空字段。

0
@echo off 
setlocal EnableDelayedExpansion 

rem Initialize the variable that store field names 
set "fieldNames=" 

rem Process the csv file 
for /F "tokens=1-5 delims=," %%a in (theFile.csv) do (

    rem Get field names from first line 
    if not defined fieldNames (
     set "fieldNames=%%a %%b %%c %%d %%e" 
    ) else (

     rem Copy field values to field names 
     for /F "tokens=1-5" %%i in ("!fieldNames!") do (
     set "%%i=%%a" & set "%%j=%%b" & set "%%k=%%c" & set "%%l=%%d" & set "%%m=%%e" 
    ) 

     rem Create Folder 1 per OwnerID with OwnerID as folder name 
     ECHO md "\!OwnerID!" 

     rem if FolderParentID = NULL and/or is same as TopLevelFolderID, then create folder with  FolderID in OwnerID folder. 
     if !FolderParentID! equ NULL ECHO md "\!OwnerID!\!FolderID!" 
     if !FolderParentID! equ !TopLevelFolderID! ECHO md "\!OwnerID!\!FolderID!" 

     rem if FolderParentID = notNULL, and FolderParentID is same as TopLevelFolderID, then create folder with FolderID in FolderParentID 
     if !FolderParentID! neq NULL if !FolderParentID! equ !TopLevelFolderID! ECHO md "\!OwnerID!\!FolderParentID!\!FolderID!" 

     rem if FolderParentID = notNULL, adn FolderParentID is not same as TopLevelFolderID, then create folder with FolderID in FolderID form level above 
     if !FolderParentID! neq NULL if !FolderParentID! neq !TopLevelFolderID! ECHO md "\!OwnerID!\!FolderParentID!\!FolderID!\!FolderID!" 

     echo ------------------------------------- 

    ) 

) 

輸出例如:

md "\1" 
md "\1\1" 
------------------------------------- 
md "\2" 
md "\2\2" 
------------------------------------- 
md "\3" 
md "\3\5" 
------------------------------------- 
md "\132" 
md "\132\195" 
------------------------------------- 
md "\133" 
md "\133\174" 
------------------------------------- 
md "\135" 
md "\135\133" 
相關問題