0
votes

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
2
if not exist "D:\KUMAR_398519667_9262017" is not valid. You need a command after ifphuclv

2 Answers

0
votes

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

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