开发者

How can a create a directory that follows a numbered sequence?

I'd like to write a batch file that logs data. Each time it runs, it should log data in a new, sequentially numbered directory.

If I were doing this in BASH I would simply do:

~/$ 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

What would be the equivalent of this in Windows (XP or more recent) batch programming?

[EDIT]

This is what I implemented, and it doesn't do what I'd hoped:

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%"

Running on Windows 7 (cmd) gives:

c:\batch\log-dir.bat
Check Thermo-Cal
The filename, directory name, or volume label syntax is incorrect.
Check Thermo-Cal\00534
The filename, directo开发者_Go百科ry 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"

The first time the batch file runs, the log-1 is created. Running the command a second time produces the exact same results, I would hope it create log-2.

Turning off the @echo off shows that the loop never breaks out early and runs (in this case) 99 times.


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

Inserted pause so you can see each output before it moves onto the next sequential number. Remove PAUSE when you finalize your script.

EDIT: Adding an IF NOT EXIST condition

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
)

When using IF [NOT] EXIST statements on directories, you must specify .\NUL as a file, as Windows normally only passes the condition on files and not folders. And in Windows, the NUL file ALWAYS exists in an existing directory.

EDIT2: Making log-%%F accessible outside of the loop

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%

Plug that into a batch file and try it.


This worked on 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%"


You have to be careful where you use quotes, as in batch scripting, quoted content can be seen as literal strings instead of code.

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%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜