I am new to windows batch script, I have a simple program which creates a folder if not exists and moves the file from another directory into the folder.
The Code works perfectly when executed the lines over command window, but does not work when I double click on the .bat file. Need your help regarding this.
.bat file script:
@echo off
if not exist "D:\KUMAR_398519667_9262017"
mkdir D:\KUMAR_398519667_9262017
move D:\Siebel\15.0.0.0.0\ses\siebsrvr\TEMP\10488_1540_0_4D44EED2-8EEE-11E7-B1F0-5056B24CF000.docx D:\KUMAR_398519667_9262017
The syntax for if is IF [NOT] EXIST filename command
where command
is a command or group of commands. The whole thing must be in one line
If you want to make the directory and move files to it if it wasn't exist then put the command into a block
@echo off
if not exist "D:\KUMAR_398519667_9262017" (
mkdir D:\KUMAR_398519667_9262017
move D:\Siebel\15.0.0.0.0\ses\siebsrvr\TEMP\10488_1540_0_4D44EED2-8EEE-11E7-B1F0-5056B24CF000.docx D:\KUMAR_398519667_9262017
)
If you just want to make the directory when it's not exist then the following is sufficient
if not exist "D:\KUMAR_398519667_9262017" mkdir D:\KUMAR_398519667_9262017
move D:\Siebel\15.0.0.0.0\ses\siebsrvr\TEMP\10488_1540_0_4D44EED2-8EEE-11E7-B1F0-5056B24CF000.docx D:\KUMAR_398519667_9262017
Solution 1: (general case)
The command that needs to be performed should be placed on the same line as the if
(since you left your code unformatted, we can only assume the format)
if not exist "D:\KUMAR_398519667_9262017" mkdir D:\KUMAR_398519667_9262017
Solution 2: (in this case)
The mkdir
(or md
) command will create a directory if that directory does not already exist, and produce an error message if it already exists.
@echo off
mkdir D:\KUMAR_398519667_9262017 2>nul
move D:\Siebel\15.0.0.0.0\ses\siebsrvr\TEMP\10488_1540_0_4D44EED2-8EEE-11E7-B1F0-5056B24CF000.docx D:\KUMAR_398519667_9262017
The 2>nul
suppresses the error message.
In general, it's a good idea to "quote full paths to filenames" - just in case they contain spaces - and terminate the destination of a move
with \\
if the destination is a directory (and enclose the destination name in "quotes" also). Note "good idea" - not "absolutely required".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.