My requirement is I am Creating a text using dos command to store date using
Echo %date% > Sample.txt
Now I should read the Sample.txt to check for the date value and based on the current date I should call another batch file
This thread ponders your question and has at least one solution:
http://www.computerhope.com/forum/index.php?topic=112106.0
create another file with today's date
compare the files with FC
use FIND to check if FC's output says no differences
Use FIND's returned error level to branch your batch file
FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > nul
IF ERRORLEVEL 1 goto different
echo Files are the same.
goto end
:different echo Files are different.
:end
WRITE
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo %ldt% > test.txt
READ
#echo off
for /f %%a in (test.txt) do (
echo %%a
pause
)
Related
I decided to write a small batchfile to make daily copies of a folder and its contents. I used WMIC to properly format the date in order to name the folders automatically. While it works perfectly on the command line, it seems that it dislikes being executed by the TaskScheduler - it does make a copy of the folder, but doesn't retrieving any date, and saves the files into a folder named -~-2-~-2. It seems to skip the first part of the code altogether, and jump directly to the Pad digits part, from where it gets the ~-2 bit. Any suggestions would be most welcome!
Here's my batch file:
#Echo off
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
:: Display the date/time in ISO 8601 format:
Set _date=%_yyyy%-%_mm%-%_dd%
Echo %_date%
:: Xcopy the files!
xcopy /s /i "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\"%_date%\
GOTO:EOF
:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo:
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.
I would suggest you use a quicker method for determining your date than wmic.
The following example uses robocopy:
#Set "_date="
#For /F "Tokens=1-3Delims=/ " %%A In ('^""%__AppDir__%RoboCopy.exe" /NJH /L "\|" Null^"')Do #If Not Defined _date Set "_date=%%A-%%B-%%C"
#"%__AppDir__%XCopy.exe" "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\%_date%\" /S
If you want to get ISODate from WMIC, you should try something like this code :
#echo off
Title GET ISODATE FROM WMIC
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo Today : %today%
set "year=%MyDate:~2,2%"
set "month=%MyDate:~4,2%"
set "day=%MyDate:~6,2%"
echo %day%%month%%year%
pause
How do I look for a file and assign file name to a variable?
Example:
Let's say I am looking for a file in below location. File name constantly changes and only way to pick up the right file is by looking for today's date in file name string:
X:\VoyagerBackups\PickupLocation\tsiuiolou_live_Full_PCV048DB42_201812140000.Lts.bak
Here's what I have so far in my .bat file. Execution works as expected if I hard-code file name:
echo on
Rem Determine date
Set mm1=%date:~4,2%
Set dd1=%date:~7,2%
Set yyyy1=%date:~10,4%
Set rundate1=%yyyy1%%mm1%%dd1%
Set executefile1=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.Lts.bak
set downloadfile=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.bak
extractor64.exe -F %executefile1% -E %downloadfile%
move %executefile1% X:\VoyagerBackups\BackupFiles
Don't use the locale/user settings dependant %date% variable.
Use either wmic or PowerShell instead, they both have more stable methods.
:: Q:\Test\2018\12\14\SO_53787816.cmd
#Echo off
for /f "usebackq" %%A in (`
powershell -NoP -C "(Get-Date).ToString('yyyyMMdd')"
`) Do Set "Today1=%%A"
Echo PowerShell today:%Today1%
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set "Today2=%%A"
Set "Today2=%Today2:~0,8%"
Echo wmic today:%Today2%
Set "Base=X:\VoyagerBackups\PickupLocation\"
PushD "%Base%" || (Echo can't find base:%Base%&Pause&Goto :Eof)
For /f "delims=" %%A in ('Dir /B "*%Today1%*" ') Do (
Echo found file:%%~fA
)
PopD
So, basically I want a Batch file to read a .txt. The problem is that the Batch file needs to update everytime a new line gets written to the .txt
#echo off
set "pc=%1"
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen=%%A"
type %pc%
set /A zeilen1=%zeilen%
:loop
if not %zeilen% == %zeilen1% (
set "line="
set zeilen2=%zeilen% - 1
for /f %%a in ('more/e +%zeilen2% ^< %pc%') do (
if not defined line set "line=%%a"
)
echo %line%
set /A zeilen+=1
)
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen1=%%A
goto loop
I also can't use the type command (line 9-13) because I don't want to refresh the whole .txt only the last line.
sry for my poor english
Thanks
To start the Batch you need to do something like this call batch.cmd txtname.txt
A basic tail command can be written like so. Credit to #dbenham for his initial solution on DosTips.com
#echo off
call :Loop <"tailme.txt"
exit
:Loop
set "line="
set /p "line="
if defined line (
echo %line%
) else (
pathping -q 1 -p 300 localhost >nul
)
goto :loop
If you don't wish to use third party options and wish to keep it pure batch, it is very possible. From your question, it sounds like you wish to read the last line of a text file and have it update that text each time the text file is edited. Further more, this batch file much be call'ed to when it needs to be used.
To do this, we can compare the date it was last modified using forfiles in an for loop. The reason for this is that if we use the file properties EX: ECHO Last-Modified Date : %%~ta we will not get the properties down to seconds. Thus the file will only compare down to the minutes.
Now that we can grab the last modified properties we can use an IF statement to look for when the file get a new time stamp. From there we can use a modified script that reads only the last line of a text file (Configurable by set /a LINES=LINES+1 LINES+1 - Infin) made by #Patrick Cuff
To call this batch file you will want to use call ReadFile.bat txtname.txt
Call - Command
ReadFile.bat - Name of batch script
txtname.txt - Name of textfile to read
Bellow is the full script.
ReadFile.bat
#ECHO OFF
#GOTO READ
:LOOP
Rem | Look for changes
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo #fdate-#ftime"') DO (set FileTimeCurrent=%%a)
IF "%FileTimeLoad%"=="%FileTimeCurrent%" (goto LOOP) else (goto READ)
:READ
cls
Rem | Get current date
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo #fdate-#ftime"') DO (set FileTimeLoad=%%a)
Rem | Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (%1) do (
set /a LINES=LINES+1
)
Rem | Print the last line
set /a LINES=LINES-1
more +%LINES% < %1
goto LOOP
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
So on.
I recently took interest in batch file coding and now i have a project i am working on.
in the project, i have created a basic program that require users to input their Licence code which i have stored in a text file(about 50 lines of licenses) and embeded it in my batch file with a third pary application that convert my batch file to .exe(about 50 lines of licenses). i have about 42 computers in my organization branch where i want to deploy the program.
My challenge however is that since the batch file and the Licence file are all in one .exe, One key would work on all 42 computers. which is very bad.
i have tried using wmic csproduct get uuid to compare computers but i just could not find my way.
Please i really need help with this.
here are my work so far:
SETLOCAL
:startup
SET "Password_File=%UserProfile%\Desktop\Password_File.txt"
set "fail="
set "Activation_Code="
set /p "Activation_Code=Your Activation Code Here: "
REM cls
IF NOT DEFINED Activation_Code ( GOTO :startup
)
findstr /X /l /c:"%Activation_Code%" <"%Password_File%" >nul || set fail=1
if defined fail (
echo Invalid Activation Code!
"%UserProfile%\Desktop\Msgbox Failed.vbs"
GOTO :startup
)
:yes
REM cls
echo Success, you have access!
REm >> %Password_File% echo %Activation_Code%
"%UserProfile%\Desktop\MsgboxSuccess.vbs"
"C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe"
:: This line remove the already entered activation code from the licence file copied to the computer
type %Password_File% | find /V "%Activation_Code%" >XXFile.txt
del "%UserProfile%\Desktop\Password_File.txt"
ren %UserProfile%\Desktop\XXFile.txt Password_File.txt
goto :eof
:nope
REM cls
echo Activation Code has already been used or Invalid!
"%UserProfile%\Desktop\Msgbox Failed.vbs"
:: ping locahost -n 3 >nul
GOTO :startup
i want my batch file to log the following to a destkop file
where LOG_FILE = %Userprofile%\Desktop\LogFolder\LOG_FILE.txt
wmic csproduct get name >>%LOG_FILE%
wmic csproduct get UUID>>%LOG_FILE%
then compare the UUID log in %LOG_FILE is the same as the current computer's UUID
if so then it should verify the same for the Computer name.
the whole idea
#echo off
if wmic csproduct get UUID == "DAC2D186-1AB9-E111-A120-B888E34233CEE" (echo Done! ) else ( echo Not Done!) pause
The future as stated by Microsoft is PowerShell and CIM. I have not yet found a CIM class that includes UUID, but this will work.
FOR /F %%u IN ('powershell -NoLogo -NoProfile -Command "(Get-CimInstance -ClassName Win32_ComputerSystemProduct).UUID"') DO (SET "UUID=%%u")
ECHO UUID is set to %UUID%
use a for /f loop to get the output of a command:
for /f "delims=" %%a in ('wmic csproduct get UUID^, name /format:list ^|find "="') do set "%%a"
echo "%uuid%"
echo "%name%"
I really have no idea how to make the result from the following text file in to a variable in my batch file.
First I need to run this WMIC line.
wmic PAGEFILE get AllocatedBaseSize /Value > test.txt
The result from test.txt occupies 6 lines, only the third line has the characters and this is the one I need to set as my variable.
Content of test.txt
AllocatedBaseSize=16328
This will directly set the variable without an intermediate file
for /f "tokens=*" %%a in (
'wmic PAGEFILE get AllocatedBaseSize /value^|find "="'
) do for %%b in (%%a) do set "AllocatedBaseSize=%%b"
echo %AllocatedBaseSize%
The double for loop is to eliminate an additional CR (0x0D) character and the end of the output lines from WMIC
To get the line you want into a file:
wmic PAGEFILE get AllocatedBaseSize /Value | FINDSTR "Allocated" > test.txt
To get the contents of the file into a variable:
set /p Base=<test.txt
wmic PAGEFILE get AllocatedBaseSize /Value |find "=" > test.txt
Another way to skin the cat (without creating a file):
for /f %%i in ('wmic PAGEFILE get AllocatedBaseSize ^|findstr "." ^|find /v "Allocated"') do set base=%%i
or in this case even easyier:
for /f %i in ('wmic PAGEFILE get AllocatedBaseSize ^|findstr "[0-9]"') do set as=%i
Not sure if it is better to post this as a new topic.
Now I am almost done with my batch script, for now I am testing on one target host. Batch file have issues with division and found this good script from http://helloacm.com/math-example-using-windows-batch-script/ . I barely understand how it works but no idea how to change the final result of the last echoed "echo %o%"
Example:
AllocatedBaseSize = 3931
CurrentUsage = 94
PeakUsage = 94
Here's the code from the link above, I changed the C value to 3:
setlocal
set /a a=%CurrentUsage%
set /a b=%AllocatedBaseSize%
set /a c=3
set /a d=a/b
set o=%a%/%b%=%d%.
:work
set /a a=(a-d*b)*10
if "%a%"=="0" goto clean_up
set /a d=a/b
set /a c=c-1
if "%c%"=="0" goto clean_up
set o=%o%%d%
goto work
:clean_up
echo %o%
endlocal
Now the problem is the script echoes "96/3931=0.02", I need to display the result as "CurrentUsagePct=0.02" or even better if I can display it as ""CurrentUsagePct=2.44"
If there is already a batch file here that does the same, will love to use as well.