I am new to BAT writing.
I am trying to write a batch file which will check to see if a file is above or below a certain size and then send an email accordingly. I have written something that can do this with a static file name
#echo off
setlocal
set file="ssoff.bat"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is ^< %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize less than" -body testbody
) ELSE (
echo.File is ^>= %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize greater than" -body testbody
)
In this case, the filename being checked is ssoff.bat. However, I need to have the bat check a filename which changes daily according to the date. The mask for the filename uses the date string IE: deployEAR_restartWAS_03132013.log ; deployEAR_restartWAS_03142013.log ... and so on - with the last 8 chars reflecting the date generated. The bat needs to check the latest file in that directory. For today it would be deployEAR_restartWAS_03152013.log
This logfile would not be in the parent dir either.
I thought maybe of having the bat copying over the latest file from that dir to the parent dir and then checking its size? Or using the static part of the filename 'deployEAR_restartWAS_' and then passing the last part of it through a date variable?
But I'm not sure what approach would be best, and I'm sure there are others that I have not thought of.
The bat file run time would be the same day as the date variable at the end of the filename.
for /f %%i in ('dir /b /a-d /od deployEAR_restartWAS_*.log') do set name=%%i&set size=%%~zi
echo latest file is %name% size %size%
Can't work out in which directory this set of .log files resides from your description. You tell us it won't be in the parent directory and your only reference appears to be that directory.
If the .log files are not in the CURRENT directory, all you need do is add the directory name to the deploy... thus: Q:\wherever\it\maybe\deployEAR_restartWAS_*.log or, if the file's path contains spaces, quote the name thus: "Q:\where ever\it\may be\deployEAR_restartWAS_*.log"
Related
I am trying to save the output of forfiles to a variable in batch.
I've tried a few things to set a var to the output directly as well as saving to a file
Long story short, I am comparing the output text to another string variable, if it matches an event log is created and I get an alert.
Everything in my script works as intended other than saving the forfile output to the variable so I can compare it to my default text variable
forfiles /P %filename% /D -1 > variable
set TEST=
the variable just stays completely blank.
If I run the command just in command prompt it outputs like this
forfiles /P C:\users\justin.pcs\desktop\test\ /D -1 > variable
ERROR: No files found with the specified search criteria.
So I am getting the error I am watching for, but the variable stays blank. I'm assuming I am missing some sort of switch that is necessary for this.
to get the output of a command, either redirect STDERR to a file and read it back:
forfiles /P C:\users\justin.pcs\desktop\test\ /D -1 2>temp
<temp set /p variable=
echo %variable"%
or use a for construct (no temporary file needed):
for /f "delims=" %%a in ('forfiles /P C:\users\justin.pcs\desktop\test\ /D -1 2^>^&1 1^>nul') do set variable=%%a
echo %variable%
The redirections (2^>^&1 1^>nul)are needed, because STDOUT is parsed, but you need STDERR.
I'm in need of a batch command that will look into a directory's sub folders and find a partial file name. If that file exists, then move that directory otherwise leave it alone.
I have a folder full of movies:
D:\Movies\movie1\
\movie2\
\movie3\
Within each movie folder I would like to know if it has a trailer downloaded into the folder, if so then move that directory to x:\movies. I say partial file name as every trailer file will include the title of the movie with "-trailer" added to the end (f.e. The Interview (2014)-trailer).
Let's look for a solution from Windows CLI (Command Line Interpreter) with -> prompt using An A-Z Index of the Windows CMD command line and with next scenario:
->tree d:\test\movies
D:\TEST\MOVIES
├───movie1
├───movie2
├───movie3
└───movie4
->tree d:\test\xmovies
D:\TEST\XMOVIES
No subfolders exist
->dir /B /S /A-D "d:\test\movies"
d:\test\movies\movie1\The View (2014)-trailer missing.rtf
d:\test\movies\movie2\The Interview (2014)-trailer.bat
d:\test\movies\movie2\The Interview (2014)-trailer.bmp
d:\test\movies\movie2\The Interview (2014)-trailer.txt
d:\test\movies\movie3\An Interview (2014)-trailer.bmp
To find all those files under MOVIES subfolder with -trailer added to the filename end preceding . of file extension:
->dir /B /S /A-D "d:\test\movies\*-trailer.*"
d:\test\movies\movie2\The Interview (2014)-trailer.bat
d:\test\movies\movie2\The Interview (2014)-trailer.bmp
d:\test\movies\movie2\The Interview (2014)-trailer.txt
d:\test\movies\movie3\An Interview (2014)-trailer.bmp
To get folder names only:
->for /F "tokens=*" %G in ('dir /B /S /A-D "d:\test\movies\*-trailer.*"') do #echo %~dpG
d:\test\Movies\movie2\
d:\test\Movies\movie2\
d:\test\Movies\movie2\
d:\test\Movies\movie3\
But here the movie2 appears more than once! And, moreover, the move command does not allow source directory with trailing backslash:
->move "D:\test\Movies\movie4\" "D:\test\xMovies\"
The system cannot find the file specified.
->move "D:\test\Movies\movie4" "D:\test\xMovies\"
1 dir(s) moved.
Therefore we need switch from Windows shell (CLI) to batch scripting. Create file 28167824.bat (e.g with notepad and save it to D:\bat\StackOverflow folder):
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
for /F "tokens=*" %%G in (
'dir /B /S /A-D "d:\test\movies\*-trailer.*"'
) do (
set "rawfolder=%%~dpG"
if exist "%%~dpG" call :moveDir
)
:endlocal
#ENDLOCAL
goto :eof
:moveDir
set "srcfolder=%rawfolder:~0,-1%"
`enter code here`echo move "%srcfolder%" "d:\test\xMovies\"
rem move "%srcfolder%" "d:\test\xMovies\"
goto :eof
Run this batch script, the template of next move commands is the same as in that successful move command mentioned above:
->D:\bat\StackOverflow\28167824
move "d:\test\Movies\movie2" "d:\test\xMovies\"
move "d:\test\Movies\movie2" "d:\test\xMovies\"
move "d:\test\Movies\movie2" "d:\test\xMovies\"
move "d:\test\Movies\movie3" "d:\test\xMovies\"
Bingo! Now we could remove rem before move "%srcfolder%" "d:\test\xMovies\", save the script and run it:
->D:\bat\StackOverflow\28167824
move "d:\test\Movies\movie2" "d:\test\xMovies\"
1 dir(s) moved.
move "d:\test\Movies\movie3" "d:\test\xMovies\"
1 dir(s) moved.
And here is final configuration, cf. starting scenario:
->tree d:\test\movies
D:\TEST\MOVIES
└───movie1
->tree d:\test\xmovies
D:\TEST\XMOVIES
├───movie2
├───movie3
└───movie4
I am trying to save a batch variable into a text file. I currently have this code:
#echo off
Set var=6
#echo %var%>txt.txt
For /f "tokens*" %%i in (txt.txt) do #echo %%i
Pause
It's supposed to save the 6 into the variable var and then write the variable in a text file. I want to do this to save user input into a text file so that when the batch program is terminated it will hold the variables.
There is a little problem with redirection. You are redirecting a "stream"; they are numbered 0-9. 0 is for "Standard Input" (STDIN), 1 is for "Standard Output" (STDOUT), 2 is for "Error Output" (STDERR).
If you use the redirection symbol > without a stream number, it defaults to "1".
So echo text>txt.txt is just an abreviation for echo text 1>txt.txt
Now it's getting tricky: echo 6>txt.txt won't echo "6" to the file, but tries to redirect "Stream 6" (which is empty) to the file. The Standard Output echo is off goes to the screen, because "Stream1" is not redirected.
Solution:
If you try to redirect a number or a string which ends with a number, just use a different syntax:
>txt.txt echo 6
Use the set command to get the contents of a file:
set /p var=<filename
Use the echo command to put into a file:
#echo Contents Of File > "FileName"
To append another line to the end of the file, use:
#echo Contents Of File >> "FileName"
Also, put the commands on separate lines or use '&&' between them on the same line.
I use the below code in order to create a directory from the value of the first 4 chars of collector text file
#echo off & setlocal enabledelayedexpansion
:loop
set /P txt_file=<Collector.txt
Set collector_id=!txt_file:~0,4!
:: check for existence of [OutputFolder]
:: if [OutputFolder] doesn't exist, create it
if not exist %collector_id% (
echo folder %collector_id% not found
echo creating folder %collector_id%
md %collector_id%
)
xcopy *.txt %collector_id% /v
Del *.txt
goto loop
I want to execute the above loop continuously in order to check if current directory is empty or not. If not I want to make a dir, if does not exist, with name the first 4 chars of collector.txt.
If the directory is not empty everything is ok. When the above is looping and I add collector.txt to the current directory the collector_id does not change.
Where am I wrong?
Is there any other way, expect infinite, loop to do this?
Put setlocal EnableDelayedExpansion at the start, and use !var! instead of %var%. Then it is evaluated every time.
I'm stuck on a dead end with this logparser dilemma. so there's no where else to go but here.
Batch file (disktrend.bat)
#echo off
pushd "Program Files (x86)\Log Parser 2.2\"
for /f "tokens=1,2 delims=," %%A in (d:logs\serverdrives.txt) do #logparser -i:tsv file:D:\logs\disktrend.sql?sd=%%A -o:chart -charttype:line > D:\logs\%%B.gif
SQL file (disktrend.sql)
SELECT date,pfree using to_int(sub(to_real(100),to_real(replace_chr(free,'%','')))) as pfree,replace_chr(strcat(server,drive),':','') AS serverdrive from d:\logs\compiled\*.log where serverdrive =%sd%
List file (serverdrives.txt)
'SERV06F',SERV06F
'SERV00F',SERV00F
Log file sample content (*.log)
Date Server Drive Free Free Space Total
20120914 SERV06 F: 79.55% 27619.02MB 34719.82MB
20120914 SERV00 F: 99.73% 34630.37MB 34723.81MB
When I run the batch script, I get below error:
Error
Invalid output filename ""
Invalid output filename ""
Does anyone know how to solve this?
while eating dinner an answer suddenly popped on me.
I edited the Batch and SQL files.
See below.
Batch file (disktrend.bat)
#echo off
pushd "Program Files (x86)\Log Parser 2.2\"
for /f "tokens=1,2 delims=," %%A in (d:logs\serverdrives.txt) do #logparser -i:tsv file:D:\logs\disktrend.sql?sd=%%A+filen=%%B -o:chart -charttype:smoothline -q:off -stats:off
SQL file (disktrend.sql)
SELECT to_string(date),pfree using to_int(sub(to_real(100),to_real(replace_chr(free,'%','')))) as pfree,replace_chr(strcat(server,drive),':','') AS serverdrive into d:\logs\%filen%.gif from d:\logs\compiled\*.log where serverdrive =%sd%