Logging of the success and failure status via Windows bat file - windows

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

Related

Batch file cannot find the extension file specified in the script

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.

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.

BATCH- How to put a variable into the move command?

Okay, this is my first question on Stackoverflow, so I'll try to make it a good one.
I've searched all over the web and I couldn't find any information to this. I have created a little batch file that prompts you to put in the name of a file that you would like to move. After that, it asks your your usersname. The problem I'm having is cmd tells me I have incorrect syntax.
Can anyone see what I did wrong?
Below is the code I am using. I just pasted in the part that is having trouble.
Thanks guys!
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename%
echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move C:\Users\%USERNAME%\Desktop\%filename%.bat
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\StartMenu\Programs\Startup\%filename%goto COMPLETED
goto FAILED
I editted a bit your script:
Surrounded filepaths with double quotes
Added /y parameter to move command to override automatically the file in new location (if exists)
Spotted some issues:
goto MOVE
:MOVE has no sense as it will continue anyway that path.
goto FAILED GOTO COMPLETED - there're no such labels in your script.
Please shout if you have other problems.
#echo off
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename% echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move /Y "C:\Users\%USERNAME%\Desktop\%filename%.bat" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%filename%" goto COMPLETED
goto FAILED
:FAILED

Suppress error messages in Windows commandline

Let's say I already have a folder created on the next path file: "C:\users\charqus\desktop\MyFolder", and I run the next command on CMD:
mkdir "C:\users\charqus\desktop\MyFolder"
I get a message like this: "A subdirectory or file C:\users\charqus\desktop\MyFolder already exists".
Therefore, is there any command in the commandline to get rid of this returned messages?
I tried echo off but this is not what I looking for.
Redirect the output to nul
mkdir "C:\users\charqus\desktop\MyFolder" > nul
Depending on the command, you may also need to redirect errors too:
mkdir "C:\users\charqus\desktop\MyFolder" > nul 2> nul
Microsoft describes the options here, which is useful reading.
A previous answer shows how to squelch all the output from the command. This removes the helpful error text that is displayed if the command fails. A better way is shown in the following example:
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 4E99-B781
Directory of C:\test
20/08/2015 20:18 <DIR> .
20/08/2015 20:18 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 214,655,188,992 bytes free
C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir
C:\test>dir new_dir >nul 2>nul || mkdir new_dir >nul 2>nul || mkdir new_dir
As is demonstrated above this command successfully suppress the original warning. However, if the directory can not be created, as in the following example:
C:\test>icacls c:\test /deny "Authenticated Users":(GA)
processed file: c:\test
Successfully processed 1 files; Failed processing 0 files
C:\test>dir new_dir2 >nul 2>nul || mkdir new_dir2 >nul 2>nul || mkdir new_dir2
Access is denied.
Then as can be seen, an error message is displayed describing the problem.

echoing (creating) an error log file

I have the following lines commands:
if %errorlevel% equ 1 (
set/a error=1
if not exist "error.log" echo. > "error.log"
echo the procedure has got an error >> "error.log"
echo. >> "error.log
)
but like this I obtain the message that the file is being processed by another process.
There is maybe another way to create the file if not exists instead of using Echo.
You can create the file with
copy NUL error.log
However, I doubt that echo is your problem. More likely is that the file already exists and you have it opened in a text editor (or viewer) that locks the file.

Resources