Run a bat file on specific date - windows

I want to know how I can run a .bat file on a specific date.
Sample code:
if "%date%" == " Wed 08/04/2021"
start "" "C:\Users\CIM\Downloads\deletefile.bat"

if %DATE:~10,4%%DATE:~4,2%%DATE:~7,2% EQU "20210408" (
start "" "C:\Users\CIM\Downloads\deletefile.bat"
)

Related

Both if and else block are executed in Batch script

I am new to Batch scripting.
I am trying to write a script which parses given command and check if argument with name 'folder 'is present in that command and if not , add that argument with default value.
I have written following script. This scripts executes correctly if argument is missing.
But if argument is present , both if and else blocks are executed.
Please help. Thanks in advance.
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
Output of code :
Folder is specified in command
Folder is NOT specified in command. User's current directory will be used as Folder.
folderArgName : -folder
folderArgValue : "C:\work"
folderArg: -folder "C:\work"
ARGS : -action generate -folder "Source" -folder "C:\work"
You have to have a goto in the else, that goes to after the setDeafultFolder, otherwise it just will execute the setDefaultFolder after it
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
goto endOfBatch
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
:endOfBatch

Check if a PostgresSQL DB Exists with Batch File and Output Custom Values to a Text File

Basically I am trying to check if a certain PostgreSQL DB exists with a batch file from the CMD in Windows. Then output custom results onto a text file within the same location, the text file will contain custom values. But the batch I created keeps giving me this (below), does not create the file and instead outputs this on the cmd:
SET _chk_DB=server
was unexpected at this time.
And I would like to know where I went wrong.
I am a beginner with batch so please do take that into consideration.
SET _chk_DB=server
FOR /F "usebackq" %%S IN (psql.exe -h %_svr% -d %_db% -U %_usr%
-P %_psw%
-Q "set nocount on; select count(*) from dbo.sysdatabases where
[name]='%_dtb%'") DO ( SET _chk_DB=%%S )
IF [%_chk_DB%]==[server]
"1" >> Info.text
else
echo "0" >> Info.txt
IF [%_chk_DB%]==[login]
"1" >> Info.text
else
echo "0" >> Info.txt
IF [%_chk_DB%]==[0]
"1" >> Info.text
else
echo "0" >> Info.txt

If variable can't have spaces?

I am making an experimental program in batch for a simple chatting interface. In this one, I made a function where if there is the word r placed in chat, it ignores it and just redisplays the text file again. It works fine if I put r and it just refreshes, and if I put one word it works fine, but if I put a word and a space and another word, it breaks and shows the following error:
Chat(Put r for refresh):hey hi
hi was unexpected at this time.
Does anyone know how to fix this? Thanks.
Code:
#echo off
cls
cd %USERPROFILE%\Desktop\Chat
for /f "delims=" %%A in (chat.txt) do (
set %%A
)
echo %chatt%
echo %chatp%
echo %chatn%
cd %USERPROFILE%\Desktop\Chat\Servers\%chatt%
:1
cls
type %chatn%.chat
set /p in=Chat(Put r for refresh):
if %in% == r goto 1
echo %chatp%: %in%>>%chatn%.chat
goto 1
The usual way to deal with spaces in a string variables contents is to wrap it in quotes. This is the case here. When you use the variables contents with %in% the contents are inserted verbatim, so the suspect line would look like this:
if hey hi == r goto 1
It starts off okay if hey but then instead of seeing a comparison operator like == it sees hi and chokes. So wrap it all in quotes:
if "%in%" == "r" goto 1
That way it will be interpreted like
if "hey hi" == "r" goto 1
and the bat engine will know that "hey hi" should be treated as one entity.

Doing operation for each file of a folder using windows command line

I have some document in a folder. these document may have same name with different extention, e.g: xyz.doc, xyz.pdf and xyz.log. I want implement following logic in windows command line script:
foreach *.doc {
if EXIST *.pdf{
do something_1
}else {
do something_2
}
if EXIST *.log {
do something_3
}
Here's my Minimal, Complete, and Verifiable example (Copy&Paste from cmd window). Used another extensions but principle is the same:
==> dir /b "sta *.*"
sta tus.csv
sta tus.txt
sta tus2.txt
==> for %G in ("sta *.txt") do #if exist "%~nG.csv" (echo + %~nG ) else (echo - %~nG)
+ sta tus
- sta tus2
==>
A batch script for your extensions:
#echo off
for %%G in ("*.doc") do (
if exist "%%~nG.pdf" (
echo + %%~nG
) else (
echo - %%~nG
)
)
Resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G, %~nG etc. special page) Command Line arguments (Parameters)

Batch script or CMD for daily task on directory content by date

I admit I am a noob when it comes to CMD and bat scripting hence maybe my question has already been answered but I couldn't find it because I am unfamiliar with the terminology.
Basically I am currently running CMD to create a txt file for a directory content, that works fine but I would like to improve this process and started to look into a batch file to run this for multiple directories and by date but only get confused with the commands.
I would really appreciated if you maybe show me the right direction to look up the possible commands. Here is was I am basically trying to achieve:
Scan Directory 1, create log file with all content (filename) with modification of date DDMMYYYY and save under Directory 1 (existing on Desktop)
Repeat above for Directory 2, 3, 4 etc.
Now I am not sure how to approach this and where to start. It looks so simply yet I am have not managed to get to work.
assuming you are on Vista or better and your date format is dd/mm/yyyy:
for /d %%a in ("%userprofile%\Desktop\Directory*") do (
for %%b in ("%%~fa\*") do (
set "fname=%%~fb"
for /f %%c in ("%%~tb") do set "fdate=%%c"
setlocal enabledelayedexpansion
echo !fname! !fdate:/=! >> "%%~fa\LOGFILE.TXT"
endlocal
)
)
First of all your batch script to parse current date-time will be locale specific. As long as you do not plan to use it on non-US Windows it will be fine. My solution was to use simple VBS script to generate current timestamps
So code of my batch file looks like
#echo off
call GetToday.bat
call %TEMP%\SetToday.bat
SET LOGFILE=Log.%TODAY%.log
echo %LOGFILE%
Use your log here
GetToday.bat:
#echo off
set TOOLS_HOME=%~dp0
cscript /NoLogo %TOOLS_HOME%\Today.vbs >%TEMP%\SetToday.bat
call %TEMP%\SetToday.bat
Today.vbs:
Dim d
d = Now
WScript.Echo "SET TODAY=" & Get2Digit(Year(d)) & Get2Digit(Month(d)) & Get2Digit(Day(d))
Function Get2Digit(value)
if 10 > value Then
Get2Digit = "0" & value
Else
Get2Digit = Right(value, 2)
End If
End Function
However given Today.vbs generates today date in form YYMMDD. From my experience such suffixes are much more useful, you could just sort you files by name to find specific date
In PowerShell something like this should work:
$folders = 'C:\path\to\Directory1', 'C:\path\to\Directory2', ...
$refDate = (Get-Date '2013-05-27').Date
$recurse = $false
foreach ($d in $folders) {
$output = Join-Path $d 'filelist.txt'
Get-ChildItem $d -Recurse:$recurse | ? {
-not $_.PSIsContainer -and $_.LastWriteTime.Date -eq $refDate
} | % { $_.Name } | Out-File $output
}
If you want to recurse into the subfolders of the scanned folders you need to change $recurse to $true and perhaps change $_.Name to $_.FullName, so you get the filename with the full path instead of just the filename.

Resources