我需要创建很多需要从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 :(得分: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”,它不仅仅是一个空字段。
答案 1 :(得分: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"