Batch file cannot find the extension file specified in the script - windows

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.

Related

See if file exists and rename using Windows command line

I have spent a lot of time trying to figure this out and am quite frustrated.
All I want to do is see if a file exists (which it does) and then rename it. I am working in the command line in Windows 10.
IF exist C:\content\info.txt (
ren "C:\content\info.txt" "C:\content\info_new.txt"
) ELSE (
echo "Couldn't find file."
)
I keep getting the error "Syntax of the command is incorrect". I know the file exists because I ran the following earlier.
if exist C:\content\info.txt echo "info.txt is in C:\content\info.txt."
I am entering all of this directly into the command prompt, and not calling it from a .bat file. Am I missing something here? Any help would be appreciated.
you don't have to give the full location while renaming the file. Just do this:
IF exist C:\content\info.txt (
ren "C:\content\info.txt" "info_new.txt"
) ELSE (
echo "Couldn't find file."
)
You need to go to the directory first, then change the filename. Like this:
IF exist C:\content\info.txt (
cd C:\content
ren info.txt info_new.txt
) ELSE (
echo "Couldn't find file."
)
Here is working one liner, if the file is there, no need of else
IF exist C:\content\info.txt ren C:\content\info.txt info_new.txt

Logging of the success and failure status via Windows bat file

I am creating a bat file to create folders automatically as per one of the tasks.I have written a simple for loop to check for the folder names in the text file and then create that folder automatically.
Here two cases arise:
Case 1: Creates a new folder if the folder doesn't exist.
Case 2: Logs the error into the log file that the folder already exists in case if the folder is an existing one.(I've used the >> folder_create_log.log 2>&1 to achieve the purpose)
But however,I wanted to get the log created for the fodler created succesfully too.For that i thought of using the %errorlevel% of the windows bat file.
Code bit as below:
set param="
for /F "delims=" %%a in (%FILE_NAME%.txt) do (
mkdir %param%S:\blitz2\Data\%%a\Audit%param% >> folder_create_log.log 2>&1
mkdir %param%S:\blitz2\Data\%%a\Working%param% >> folder_create_log.log 2>&1
mkdir %param%L:\ZaiUpload\Blitz\%%a%param% >> folder_create_log.log 2>&1
mkdir %param%L:\ZaiUpload\Blitz\%%a\Archive%param% >> folder_create_log.log 2>&1
echo %errorlevel%
pause
)
But as the >> folder_create_log.log 2>&1 works fine it is treated as a success and the %errorlevel% is shown as 0 instead of 1
Could anyone please assist me in getting the successful creation logged into the log file ?
md test && echo successful
&& works as "if previous command was successful, then".
The opposite is || , so a complete command could look like:
md test && echo successful || echo failed

Downloading file from server over FTP

I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.
Here's the script I am working on:
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I know that I can loop through directories using for like this:
for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)
But "for" doesn't work inside ftp>.
Any help is highly appreciated. Thanks.
I am assuming that for a given date there is only one abc_YYYYMMDD_nnnnnn directory.
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
set root=E:
echo open server1>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
for /D %%D in (%root%\%foldername%??????) do set target=%%D
echo lcd %target%>>file.tmp
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I set root to E:\ based on your original script - you can change it to whatever the actual parent directory is. I also changed
echo open server1>>file.tmp
to
echo open server1>file.tmp
so it creates a new temp file. Otherwise, it will keep appending to file.tmp each time the script runs. If that is what you want, or if you are generating a unique script file name each time and were just using file.tmp as a placeholder, ignore that change and use >> as before.
If there is more than one folder for a given day, I believe this script will use whichever one comes up last when the wildcards are expanded. I did not test this out, however.

using space in path for copying files using batch script

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"

Create folder with batch but only if it doesn't already exist

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

Resources