如何创建一个遵循编号序列的目录?

时间:2011-09-01 15:03:18

标签: batch-file

我想写一个记录数据的批处理文件。每次运行时,它都应该将数据记录在一个新的,按顺序编号的目录中。

如果我在BASH中这样做,我会这样做:

~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-1
~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-2
~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-3

在Windows(XP或更近期)批处理编程中,这相当于什么?

[编辑]

这是我实施的,并没有做到我所希望的:

set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST "C:\Thermo-Cal\NUL" "md C:\Thermo-Cal"
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\NUL" "md C:\Thermo-Cal\%UNIT_ID%"
FOR /L %%F IN (1,1,99) DO (
    IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL"  (
        "md C:\Thermo-Cal\%UNIT_ID%\log-%%F"
        set "LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F"
        goto dir_set
    ) 
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo "Stuff will get saved in: %LOG_DIR%"

在Windows 7(cmd)上运行 会给出:

c:\batch\log-dir.bat
Check Thermo-Cal
The filename, directory name, or volume label syntax is incorrect.
Check Thermo-Cal\00534
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
"Stuff will get saved in: C:\Thermo-Cal\00534\log-1"

第一次运行批处理文件时,会创建log-1。 第二次运行命令产生完全相同的结果,我希望它创建log-2 关闭@echo off表示循环从未提前发生并且运行(在这种情况下)99次。

3 个答案:

答案 0 :(得分:1)

FOR /L %%F IN (1,1,25) DO (
 IF "condition" "md C:\some\folder\log-%%F"
 ECHO log-%%F
 PAUSE
)

插入暂停,以便在移动到下一个序列号之前可以看到每个输出。完成脚本后删除PAUSE。

编辑:添加IF NOT EXIST条件

FOR /L %%F IN (1,1,25) DO (
 IF NOT EXIST "C:\some\folder\log-%%F\NUL" "md C:\some\folder\log-%%F"
 ECHO log-%%F
 PAUSE
)

在目录上使用IF [NOT] EXIST语句时,必须将。\ NUL指定为文件,因为Windows通常只传递文件而不是文件夹的条件。在Windows中,NUL文件始终存在于现有目录中。

EDIT2:使log - %% F可以在循环之外访问

FOR /L %%F IN (1,1,25) DO (
 IF NOT EXIST "C:\some\folder\log-%%F\NUL" ("md C:\some\folder\log-%%F" && SET dir%%F=C:\some\folder\log-%%F)
)
ECHO %dir1%
ECHO %dir2%
ECHO %dir3%

将其插入批处理文件并尝试。

答案 1 :(得分:0)

这适用于Windows 7:

set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST C:\Thermo-Cal\NUL md C:\Thermo-Cal
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST C:\Thermo-Cal\%UNIT_ID%\NUL md C:\Thermo-Cal\%UNIT_ID%
FOR /L %%F IN (1,1,99) DO (
    IF NOT EXIST C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL  (
        md C:\Thermo-Cal\%UNIT_ID%\log-%%F
        set "LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F"
        goto dir_set
    ) 
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo "Stuff will get saved in: %LOG_DIR%"

答案 2 :(得分:0)

在使用引号时必须要小心,因为在批处理脚本中,引用的内容可以看作是文字字符串而不是代码。

set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST "C:\Thermo-Cal\NUL" (md C:\Thermo-Cal)
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\NUL" (md C:\Thermo-Cal\%UNIT_ID%)
FOR /L %%F IN (1,1,99) DO (
    IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL" (
        md C:\Thermo-Cal\%UNIT_ID%\log-%%F
        set LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F
        goto dir_set
    ) 
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo Stuff will get saved in: %LOG_DIR%