基于批处理文件中的用户输入对文件夹创建进行故障诊断

时间:2017-08-18 21:11:37

标签: batch-file cmd

我正在尝试创建一个基于逗号分隔输入的简单批处理文件,它将创建一系列嵌套文件夹。在以下代码中, GSD 是主文件夹,其中包含所有内容。用户将从那里输入以逗号分隔的不同块。这可以是A,B,C或Z,G,I等。

根据该输入,批处理文件应创建特定于块的文件夹并适当地嵌套所有内容。当用户只输入一个块(即A)时,代码I完全正常运行,但是当我尝试用多个块的输入(即A,B,C)运行时,它会崩溃并且我得到Access Denied错误。任何帮助都会非常感激,因为我的编码知识几乎没有。

我试图在其他文件夹中嵌套块文件夹,然后在GSD下嵌套所有文件夹。

@echo off

rem Get User Input
set /p gsd= Enter the GSD of the project: 
set /p blocks= Enter the blocks you are prepping a drive for separated by commas (i.e. A,B,C,etc.): 
set ItemCount=0

mkdir %gsd%

mkdir 01-Workspace_Setup
mkdir 02-Tiles
mkdir 03-Orthos
mkdir 04-Saved_Workspaces
mkdir 05-Backup
mkdir 06-Raw
mkdir 07-Excluded

mkdir ECW
mkdir FixPoints
mkdir ImageFrames
mkdir PCR
mkdir ProjectInfo
mkdir Seamlines
mkdir TrackingGrid
mkdir FixTo_Boundary

:NextItem
if "%blocks%" == "" pause & goto :EOF

set /A ItemCount+=1
for /F "tokens=1* delims=," %%a in ("%blocks%") do (
    mkdir Block_%%a
    move /y Block_%%a .\02-Tiles
    mkdir Block_%%a
    move /y Block_%%a .\03-Orthos
    mkdir Block_%%a
    move /y Block_%%a .\06-Raw
    mkdir Block_%%a
    move /y Block_%%a .\07-Excluded

    mkdir Block_%%a
    move /y Block_%%a .\ECW
    mkdir Block_%%a
    move /y Block_%%a .\FixPoints
    mkdir Block_%%a
    move /y Block_%%a .\ImageFrames
    mkdir Block_%%a
    move /y Block_%%a .\PCR
    mkdir Block_%%a
    move /y Block_%%a .\ProjectInfo
    mkdir Block_%%a
    move /y Block_%%a .\Seamlines
    mkdir Block_%%a
    move /y Block_%%a .\TrackingGrid
    mkdir Block_%%a
    move /y Block_%%a .\FixTo_Boundary

    set "blocks=%%b"

    move /y ECW .\01-Workspace_Setup
    move /y FixPoints .\01-Workspace_Setup
    move /y ImageFrames .\01-Workspace_Setup
    move /y PCR .\01-Workspace_Setup
    move /y ProjectInfo .\01-Workspace_Setup
    move /y Seamlines .\01-Workspace_Setup
    move /y TrackingGrid .\01-Workspace_Setup
    move /y FixTo_Boundary .\01-Workspace_Setup

    move /y 01-Workspace_Setup .\%gsd%
    move /y 02-Tiles .\%gsd%
    move /y 03-Orthos .\%gsd%
    move /y 04-Saved_Workspaces .\%gsd%
    move /y 05-Backup .\%gsd%
    move /y 06-Raw .\%gsd%
    move /y 07-Excluded .\%gsd%
)
goto NextItem



echo Folder creation complete. Have a pleasant day!
pause

根据评论,我将代码更改为:

@echo off

rem Get User Input
set /p gsd= Enter the GSD of the project: 
set /p blocks= Enter the blocks you are prepping a drive for separated by commas (i.e. A,B,C,etc.): 

mkdir %gsd%

mkdir 01-Workspace_Setup
mkdir 02-Tiles
mkdir 03-Orthos
mkdir 04-Saved_Workspaces
mkdir 05-Backup
mkdir 06-Raw
mkdir 07-Excluded

mkdir ECW
mkdir FixPoints
mkdir ImageFrames
mkdir PCR
mkdir ProjectInfo
mkdir Seamlines
mkdir TrackingGrid
mkdir FixTo_Boundary

if %blocks% == "" pause & goto :EOF

for %%a in (%blocks%) do (
    mkdir Block_%%a
    move /y Block_%%a .\02-Tiles
    mkdir Block_%%a
    move /y Block_%%a .\03-Orthos
    mkdir Block_%%a
    move /y Block_%%a .\06-Raw
    mkdir Block_%%a
    move /y Block_%%a .\07-Excluded

    mkdir Block_%%a
    move /y Block_%%a .\ECW
    mkdir Block_%%a
    move /y Block_%%a .\FixPoints
    mkdir Block_%%a
    move /y Block_%%a .\ImageFrames
    mkdir Block_%%a
    move /y Block_%%a .\PCR
    mkdir Block_%%a
    move /y Block_%%a .\ProjectInfo
    mkdir Block_%%a
    move /y Block_%%a .\Seamlines
    mkdir Block_%%a
    move /y Block_%%a .\TrackingGrid
    mkdir Block_%%a
    move /y Block_%%a .\FixTo_Boundary

)

move /y ECW .\01-Workspace_Setup
move /y FixPoints .\01-Workspace_Setup
move /y ImageFrames .\01-Workspace_Setup
move /y PCR .\01-Workspace_Setup
move /y ProjectInfo .\01-Workspace_Setup
move /y Seamlines .\01-Workspace_Setup
move /y TrackingGrid .\01-Workspace_Setup
move /y FixTo_Boundary .\01-Workspace_Setup

move /y 01-Workspace_Setup .\%gsd%
move /y 02-Tiles .\%gsd%
move /y 03-Orthos .\%gsd%
move /y 04-Saved_Workspaces .\%gsd%
move /y 05-Backup .\%gsd%
move /y 06-Raw .\%gsd%
move /y 07-Excluded .\%gsd%

echo Folder creation complete. Have a pleasant day!
pause

当使用一个块运行时,一切正常。 Block_%%a个文件夹嵌套在应有的位置,结构看起来很好。当没有变量或多个变量(A,B,C)运行时,事物会爆炸并且不起作用。它将创建除Block_%%a文件夹之外的所有文件夹而不嵌套任何内容。我试图创建它,所以它只是构建已经嵌套的文件夹,但我无法找到正确的语法来做到这一点。

2 个答案:

答案 0 :(得分:0)

也许我错了,但是访问解释器CMD将使用上面的脚本创建文件,读取行,记住位置,使用只读属性关闭文件/文件夹以及需要做什么更改这由Interpreter属性从只读到档案

答案 1 :(得分:0)

引号使列表显示为一个项目。

for /F "tokens=1* delims=," %%a in ("%blocks%") do (

应该是

for /F "tokens=1* delims=," %%a in (%blocks%) do (

如上所述,您不需要A / F循环。你可以:

for %%a in (%blocks%) do (