I've created a timestamp variable in a batch script like so...
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
There is an issue though when the HH is only a single digit I get...
YYYY-MM-DD- 2-MM-SS
instead of
YYYY-MM-DD-02-MM-SS
How do I consistently generate the timestamp without spaces?
set "timestamp=%timestamp: =0%"
replaces spaces with zeroes.
See set /? from the prompt for documentation.
Don't use locale/user settings dependent date time variables but wmic:
#Echo off
For /f "delims=." %%A in (
'wmic os get LocalDateTime^|findstr ^^20'
) Do Set DT=%%A
Set "TIMESTAMP=%DT:~0,4%-%DT:~4,2%-%DT:~6,2%-%DT:~8,2%-%DT:~10,2%-%DT:~12,2%"
Set TimeStamp
Sample output:
> SO_45465890.cmd
TIMESTAMP=2017-08-02-18-42-07
Or use PowerShell and let it do the formatting:
#Echo off
For /f %%A in ('powershell -NoP -C "get-date -f \"yyyy-MM-dd-HH-mm-ss\""') Do Set TimeStamp=%%A
Set TimeStamp
Related
I tried searching but couldn't find anything specific to what I need.
So I want to fetch, maybe use curl for Windows, the guid string generated by this website without having to save the html file first. The sources are more or less like this:
<input name="YourGuidLabel" type="text" id="YourGuidLabel" onclick="this.focus(); this.select();" readonly="readonly" class="guidinput" value="852dd74c-4249-4390-85d3-6e9e2116ef2b" /></p>
What I want is this one: 852dd74c-4249-4390-85d3-6e9e2116ef2b. The string is then stored into a variable and echoed to view it.
In linux terminal I can do it in this simple way:
curl -s "https://www.guidgen.com/" | grep -o 'me="YourGuid.*value=.*/>' | cut -d '"' -f14
Does this thing by being able to use a batch file?.
This can do the trick with a batch file on Windows using a PowerShell Command and set it as variable with for /f .. do loop :
#echo off
Title Extract GUID Value from Input Field from site https://www.guidgen.com
#For /f %%a in ('Powershell -C "$(IWR https://www.guidgen.com -UseBasicParsing).InputFields.value"') do Set "GUID=%%a"
Echo GUID=%GUID%
pause
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74909468.txt"
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO SET "html=%%e"
SET "html=%html:"=%"
SET "html=%html:<=%"
SET "html=%html:>=%"
SET "html=%html:)=%"
SET "html=%html:(=%"
SET "html=%html:;=%"
FOR %%e IN (%html%) DO if "%%e" neq "//p" SET "guid=%%e"
ECHO GUID=%guid%
GOTO :EOF
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.
You haven't told us where the html is located - I've presumed a file.
Sadly "more or less like" is not specific enough to generate a reliable solution.
Read the file line to a variable, html
Remove all " < > ) ( ; from that variable.
process the result, assigning each token in turn to guid, unless the token is //p
Assumes the required string is that string which precedes //p which is the last string in the (original text - deleted character set)
The following idea not using PowerShell may also perform the task you've laid out in your question.
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "value=" & For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\curl.exe -s "https://www.guidgen.com" ^| %SystemRoot%\System32\findstr.exe /RIC:" value=\"[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*-[0123456789abcdef][0123456789abcdef]*\""') Do (Set "value=%%G" & SetLocal EnableDelayedExpansion & For /F Delims^=^"^= %%H In ("!value:* value=!") Do EndLocal & Set "value=%%H")
If Defined value Echo %value% & Pause
I'm trying to create a mask file from several input images using gdal with a batch windows file. However, the system is sending me a error when I use the "!" on the comparison calc, and after the first round, all the variables had read as a string.
My code is the following:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mypath=F:\my_in_path\
SET path_salida=F:\my_out_path\
FOR /F %%i IN ('DIR /B %mypath%*.tif') DO (
SET infile=%%i
SET outfile=!infile!
echo %mypath%!infile!
echo %path_salida%!outfile!
gdal_calc -A %mypath%!infile! --outfile %path_salida%!outfile! --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet
)
As you've provided no feedback over half a day since my comment, here is an example to test and provide feedback on:
#Echo Off
Set "mypath=F:\my_in_path"
Set "path_salida=F:\my_out_path"
Set "calc_params=-A "%%A" --outfile "%path_salida%\%%~nxA" --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet"
For %%A In ("%mypath%\*.tif") Do gdal_calc %calc_params%
I want to create a batch file which when clicked will create a folder with the name 12012016.
I tried with the command
mkdir "E:\Meru\Work\Trace Reports\%date:~6,4%%date:~3,2%%date:~0,2%
But its creates with the name 20160112.
Please help
This question implies that you have not tried to understand what is going on with this command...
Split in part: mkdir will create a directory with the name you have given.
The name you have given is build together using a fixxed string, you decided to use E:\Meru\Work\Trace Reports\ and three substrings from the system variable %date%.
Substring in batch works like this: %variable_name:~last character NOT to use,number of characters you need%. In your case it takes the year first, then the month and at last the day. You would just simply change the parts from %date:~6,4%%date:~3,2%%date:~0,2% to %date:~3,2%%date:~0,2%%date:~6,4%.
Notice!
The variable %date% has a different value based on the system settings for the time format. An alternative is the command wmic os get localdatetime which I covered in another answer here that will always have the same output format no matter what the settings are.
If you have read this ==> Windows batch file redirect output to logfile with date/time
You can be able to do like this one :
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
pause
echo datestamp: "%datestamp%"
pause
echo timestamp: "%timestamp%"
pause
set MyDateVar=%MM%%DD%%YYYY%
echo My desired Variable Date to use is : %MyDateVar%
pause
mkdir "E:\Meru\Work\Trace Reports\%MyDateVar%"
pause
Because several hours have now elapsed:
mkdir "E:\Meru\Work\Trace Reports\%date:~0,2%%date:~3,2%%date:~6,4%"
I want to create a folder that uses TIME formatted in a specific way. I want the format to be in hh.mm.ss since you can't use a : in a folder name.
When I use set CurTime=%time:~0,2%.%time:~3,2%.%time:~6,2%, I get an output with a leading space before 10:00 AM and five trailing spaces as well (not sure why). I can remove the spaces by adding the line set CurTime=%CurTime: =%, but I want to add a leading zero if the time is earlier than 10:00 AM.
How can I do this?
Edit for clarification:
I have the time formatted the way I want it, but I want to replace the leading space with a 0 if the hh portion is less than 10. This is not a duplicate question of How to get current datetime on Windows command line, in a suitable format for using in a filename?.
If you have read this ==> Windows batch file redirect output to logfile with date/time
You can be able to do like this one :
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
pause
echo datestamp: "%datestamp%"
pause
echo timestamp: "%timestamp%"
pause
set MyVar=%HH%.%Min%.%Sec%
echo My desired Variable in this format hh.mm.ss to use is : %MyVar%
pause
mkdir "c:\%MyVar%"
pause
set "mytime=%time:~0,-8%"
set "mytime=%mytime: =0%"
set "mytime=%mytime::=.%"
echo "%mytime%"
should show the result you want. The first line removes the last 8 characters from time (which should be the 5 spaces + .dd) and the other two make the substitution of unwanted characters.
The reason for the 5 trailing spaces may be that some time formats allow " a.m." and the easy way is to simply replace the unwanted parts with spaces.
There are many ways to achieve your goal.
Here's a crazy untested one:
#ECHO OFF
SET "CurTime="
FOR /F "TOKENS=2-4 DELIMS=: " %%A IN ('ROBOCOPY/NJH /L "\|" NULL'
) DO IF NOT DEFINED CurTime SET "CurTime=%%A.%%B.%%C"
ECHO(%CurTime%
TIMEOUT -1
I have multiple TraceRT log files containing 30 hops. I'm only looking for similar IP (ex. 192.168.1) and would like to log it on one file with:
1) Successful: %IP% found in %Filename%
2) Fail: Specified IP not found in %Filename%
I'm trying to use:
rem************************************************************
:START
# ECHO OFF
rem US date
set YEAR=%DATE:~10,4%
set MONTH=%DATE:~4,2%
set DAY=%DATE:~7,2%
rem US hour
set HOUR=%TIME:~0,2%
set MIN=%TIME:~3,2%
set SEC=%TIME:~6,2%
set HUNDREDS=%TIME:~9,2%
set HOURMIN=%HOUR%%MIN%
rem Make sure that hour has two digits
IF %HOUR% GEQ 10 goto twoh
set HOUR1=%TIME:~1,1%
set TWOHOUR=0%HOUR1%
goto fulltid
:twoh
set TWOHOUR=%HOUR%
:fulltid
set FULLTIME=%TWOHOUR%'%MIN%'%SEC%'%HUNDREDS%
set FTIME=%TWOHOUR%:%MIN%:%SEC%
#echo off & setLocal EnableDELAYedeXpansion
findstr /m "192.168.1" *.txt > FILENAME
echo on
for /f "tokens=*" %%a in (*.txt ^| find "192.168.1") do (
IF %%a neq %%b (
echo Suscessful: %%a %FILENAME% >> Log%YEAR%%MONTH%%DAY%.txt
) ELSE (
echo Fail: Specified IP not found in %FILENAME% >> Log%YEAR%%MONTH%%DAY%.txt
)
)
goto START
rem************************************************************
You have specified an invalid pipe | find. You cannot pipe (a) text file(s) into a command.
Either provide the file(s) as argument(s) to find, or use redirection (this works for a single file only but not for multiple ones nor */? patterns though).
You are using for /f not correctly.
It looks as if you wanted to parse the output of find. To accomplish that, but you must enclose the command within single-quotes '. Type for /? and see the help text for more details.
The following line of code should work:
for /f "tokens=*" %%a in ('find "192.168.1" *.txt') do (
To get current date and time, I strongly recommend to read variables %DATE% and %TIME% once only and within a single line! Otherwise you might run into problems, especially concerning the fractional seconds, which might not be equal between consecutive expansions.
To ensure %HOUR% to have two digits, you simply need to use set HOUR=0%HOUR% then set HOUR=%HOUR:~-2%.
Since a one-digit %HOUR% is prefixed by a space here, you can have it even simpler (thanks for your comment, #Stephan!) by just replacing the space by a zero: set HOUR=%HOUR: =0%.