下面的脚本提示文件夹路径。然后脚本将循环通过文件夹来创建文件。
例如,输入路径C:\ aa。脚本应通过C:\ aa \ fol1循环到C:\ aa \ fol5以创建文件。
例如,当前文件夹C:\ aa为空。
Input C:\aa.
Expected output would be
C:\ -- aa -- fol1\aa1\bb1\cc1\dd1\00-1 with each subfolder has 20 files.
-- fol2\aa2\bb2\cc2\dd2\00-2 with each subfolder has 20 files.
-- fol3\aa3\bb3\cc3\dd3\00-3 with each subfolder has 20 files.
-- fol4\aa4\bb4\cc4\dd4\00-4 with each subfolder has 20 files.
-- fol5\aa5\bb5\cc5\dd5\00-5 with each subfolder has 20 files.
@echo off
setlocal enableDelayedExpansion
set /p folder="Enter path: (C:\aa) : "
set /p action="Choose an action (Modify for M , Add for A , Delete for D , Rename for R , F for create Folder and files ): "
pause
2>NUL CALL :CASE_%action% # jump to :CASE_M, :CASE_A, etc.
IF ERRORLEVEL 1 CALL :DEFAULT_CASE # if label doesn't exist
ECHO Done.
EXIT /B
:CASE_F
FOR /L %%A IN (1,1,5) DO (
mkdir "%folder%"\fol"%%A"\aa"%%A"\bb"%%A"\cc"%%A"\dd"%%A"\00-"%%A"
)
timeout 11 > NUL
FOR /L %%A IN (1,1,5) DO (
mkdir "%folder%"\fol"%%A"\uu"%%A"\bb"%%A"\cc"%%A"\dd"%%A"\00-"%%A"
)
timeout 11 > NUL
echo end of create folder
pause
FOR /L %%A IN (1,1,5) DO (
echo %folder%\fol%%A
set pfolderpath=%folder%\fol%%A
pause
for /D /r "%pfolderpath%" %%F in (*) do (
echo LALA
FOR /L %%B IN (1,1,20) DO (
@echo this is new line > "%%F"\bear"%%B".txt
)
)
)
GOTO END_CASE
:END_CASE
VER > NUL # reset ERRORLEVEL
GOTO :EOF # return from CALL
上面的脚本成功创建子文件夹fol1 \ aa1 \ bb1 \ cc1 \ dd1 \ 00-1到fol5。只是它似乎无法循环到foll到fol5来创建文件。如果我使用绝对路径C:\ aa \ fol1对pfolderpath进行硬编码,该脚本运行良好。我觉得for循环有些不对劲但无能为力。请指教。
答案 0 :(得分:0)
更改以下代码部分,使两个最内层循环运行,并在新创建的文件夹中创建文件:
for /D /r "%pfolderpath%" %%F in (*) do (
echo LALA
FOR /L %%B IN (1,1,20) DO (
@echo this is new line > "%%F"\bear"%%B".txt
)
)
为:
PUSHD !pfolderpath!
for /r %%F in (.) do (
echo LALA
FOR /L %%B IN (1,1,20) DO (
@echo this is new line > "%%F"\bear"%%B".txt
)
)
POPD
请注意,有5项更改:
.
作为枚举文件夹的集合。如果你想枚举文件,你可以使用*
,但还没有。{/ li>
醇>
另请注意,对于测试此脚本的任何人,您必须回复F
提示“选择操作”;其他的没有在这里实施。