I have a batch file that detects csv files and moves them to another folder.
But since sometimes now the script does not work anymore and it was working totally fine before.
Is there any clue why these files are not detected anymore?
I have checked the script and it seems totally fine.
Script below:
#echo off
set LOGFILE="D:\M\logs\runquery.log"
cd D:\M\output
if exist *.csv
( move *.csv D:\M\output\archive >> %LOGFILE% cd D:\M\bin )
else
( echo "No file was generated" >> %LOGFILE% cd D:\M\bin )
thank you.
Regards,
I tried the code on my PC, it works. Did you try to launch it from cmd? Do you see any warning / error?
You pasted the code in the comments, so I have no idea of the indentation, but be careful with the parenthesis, they have to be put exactly like this:
#echo off
set LOGFILE="D:\M\logs\runquery.log"
cd D:\M\output
if exist *.csv (
move *.csv D:\M\output\archive >> %LOGFILE%
cd D:\M\bin
) else (
echo "No file was generated" >> %LOGFILE%
cd D:\M\bin
)
otherwise the script will give an error: "The syntax of the command is incorrect".
Also check your folder tree, if any of the folders (M, logs, output, archive, bin) doesn't exist, the script will fail.
I am creating a batch script for which I have a script that is giving me a frustrating error. The location the batch file is being excecuted from is
C:\Users####\Desktop\Sp2\Sp2.bat. I want the file to prompt the user if it wants to make a batch file. I got that down. I used the code
echo Would you like to create a directory to output the files to?
set /p mkdir=[Y/N]
if %mkdir%==Y (
goto :mkdir
) ELSE (
goto :numset
)
This part works fine. Now here's where the problem arises:
:mkdir
echo Enter a name for your folder.
set /p foldername=
MD %~d0\%foldername%
goto :numset
I this keeps giving me the error "The syntax of this command is incorrect."Can anyone give me a solution to this problem?
%~d0 gives you the drive only. To get drive and path, use %~dp0:
MD %~dp0%foldername%
The complete listing of %~?0 is well hidden. See: for /?
%~d0 expands to a drive of the batchfile. Users don't have permission to create folders in the root of a drive. So it will never work.
Plus the path seperator is \ not the switch character /.
So, I don't have much experience with batch scripting and use regular programming languages quite a lot. I'm trying to write something very simple and don't understand why half of my code is not executing as I expect. Perhaps I'm breaking some cardinal rule in Windows Batch scripting. Right now the code below just makes the new directory, and then the script appears to stop and not complete any of the code involving the time, copying, or clearing of a file. Anyone see what's wrong with this code?
#echo off
rem check for directory
if not exist fileDir (
mkdir fileDir
)
rem check for file...
if file.txt exists (
REM get computer name
set hostName = echo %computername%
rem get time format
SET HOUR=%time:~0,2%
SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)
type file.txt >> ./fileDir/%hostName%_%dtStamp%.LOG
REM clearing file contents of file.txt.
break > file.txt
)
The line:
if file.txt exists (
is not a valid batch file command. There is not exists option for if the option is exist singular and it must go before the file name. So the line should be:
if exist file.txt (
You can get more information on using if in a batch file by typing help if in a command prompt window.
I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"
Can anybody tell me how to do the following in in a Windows batch script? (*.bat):
Create a folder only if it doesn't already exist
In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed.
You just use this: if not exist "C:\VTS\" mkdir C:\VTS it wll create a directory only if the folder does not exist.
Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.
if exist C:\VTS\NUL echo "Folder already exists"
if not exist C:\VTS\NUL echo "Folder does not exist"
See also https://support.microsoft.com/en-us/kb/65994
(Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )
Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.
Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn't care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn't exist, and it doesn't overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk's answer.
mkdir C:\VTS 2> NUL
create a folder called VTS and output A subdirectory or file TEST already exists to NUL.
or
(C:&(mkdir "C:\VTS" 2> NUL))&
change the drive letter to C:, mkdir, output error to NUL and run the next command.
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.
#echo off
#break off
#title Create folder with batch but only if it doesn't already exist - D3F4ULT
#color 0a
#cls
setlocal EnableDelayedExpansion
if not exist "C:\VTS\" (
mkdir "C:\VTS\"
if "!errorlevel!" EQU "0" (
echo Folder created successfully
) else (
echo Error while creating folder
)
) else (
echo Folder already exists
)
pause
exit
You can use:
if not exist "C:\VTS\" mkdir "C:\VTS"
You can also expand the code to replace any missing expected files.
if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
This should work for you:
IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder
However, there is another method, but it may not be 100% useful:
md \path\to\your\folder >NUL 2>NUL
This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.
You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.
First, I check to see if the folder doesn't already exist by entering this code:
if not exist "FOLDERPATH" (
mkdir "FOLDERPATH"
)
So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:
if exist "FOLDERPATH" (
rmdir /s /q "FOLDERPATH"
mkdir "FOLDERPATH"
)
Now if I run the code, It will re-create the folder if it already exists. This is the example code:
#echo off
cls
if not exist "C:\ExamplePath\" (
echo Creating Folder...
mkdir "C:\ExamplePath\"
pause
)
if exist "C:\ExamplePath\" (
echo Re-Creating Folder...
rmdir /s /q "C:\ExamplePath"
pause
)
Now the if exist part is Optional. If the folder already exists, you can jump to an label instead like this:
if exist "FOLDERPATH" (
goto :ExampleLabel
:ExampleLabel
echo Hi.
pause
)
Hopefully, this could help with your problem.
Personally, I would do this:
if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)
Let's break that down:
if not exist "C:\YourFolderPathHere\": this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.
mkdir C:\YourFolderPathHere\: Creates the directory if the above statement is true.
echo Folder already exists!: prints to console that it already exists.
Here's the same code, but with comments:
::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)
One-line version:
if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\
Haven't tested it though, so don't quote me on it.
i created this for my script I use in my work for eyebeam.
:CREATES A CHECK VARIABLE
set lookup=0
:CHECKS IF THE FOLDER ALREADY EXIST"
IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)
:IF CHECK is still 0 which means does not exist. It creates the folder
IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"