This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed last year.
to follow up on the last solution in this link https://www.windows-commandline.com/get-date-time-batch-file/
I know there's no windows CMD format with this but I've tried to batch replace and arrays apparently don't have good solutions if you Google for an answer since most of the time people find and replace things in files I keep getting those answers.
If you are on a supported Windows system, powershell.exe will be available.
C:>#FOR /F "delims=" %A IN ('powershell.exe -NoLogo -NoProfile -Command Get-Date -Format 'M-d-yy'') DO #(SET "THEDATE=%~A")
C:>echo %THEDATE%
1-21-22
If this is in a .bat file script, double the PERCENT SIGN characters on the loop variable. %%A
If you want a pure batch solution, wmic provides a few date-related strings without leading zeroes:
#echo off
setlocal
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set "_%%x"
set "Mydate=%_Month%-%_Day%-%_Year%"
echo %mydate%
echo maybe also useful sometimes:
set _
Related
This question already has answers here:
How to correct variable overwriting misbehavior when parsing output?
(3 answers)
Why is "ECHO is off" displayed after output of data of interest on running WMIC in a FOR loop?
(2 answers)
Closed 2 years ago.
I have following batch script but instead of variable value I get "ECHO is on." printed. Could you please help? I don't know if this has any impact but just to note, test.bat gives current CPU temperature, for ex. 3200
SET RESULT
FOR /F %%a in ('test.bat') do SET RESULT=%%a
ECHO %RESULT%
EDIT:
Now I understood that result variable is empty but didn't understand why? I have executed following command separately and it returns temperature:
wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
Your problem here isn't primarily the ugly wmic line ending of CRCRLF, but an empty line at the end (which for doesn't recognize as empty, because it contains a superfluous CR). Just filter the output of test.bat for non-empty lines. As a bonus, the piping to findstr also gives you a proper Windows line ending.
FOR /F %%a in ('test.bat^|findstr "."') do SET RESULT=%%a
ECHO %RESULT%
Note: with this method you get the last line only (previously the empty line, now the last non-empty line). The wmic command you use, gives me two temperatures (where only the last one is kept by the variable) on my system. you might want to consider that.
Here's what I would do:
The problem is that the output for:
wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
...results in an blank line at the end, which overrides the set command with the actual temperature.
To work around this, the output is directed to file, and then used to set the value.
even though the output file contains the blank line, SET will only read the first line of any file.
Console
IF EXIST "var_$CPU_TEMPERATURE.txt" del /q "var_$CPU_TEMPERATURE.txt"
for /F "skip=1 tokens=1 delims=" %P IN ('wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature') DO (echo %P>> var_$CPU_TEMPERATURE.txt) && (SET /P $CPU_TEMPERATURE= <var_$CPU_TEMPERATURE.txt)
echo %$CPU_TEMPERATURE%
Script
IF EXIST "var_$CPU_TEMPERATURE.txt" del /q "var_$CPU_TEMPERATURE.txt"
for /F "skip=1 tokens=1 delims=" %%P IN ('wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature') DO (echo %%P>> var_$CPU_TEMPERATURE.txt) && (SET /P $CPU_TEMPERATURE= <var_$CPU_TEMPERATURE.txt)
%echo $CPU_TEMPERATURE%
I want to write a batch script which does the following.
We get files with naming convention similar to 2604230_VIJAY_TAX_02-NOV-2018.xls in C:\temp\
I want to extract the strings before the Underscore(_) from the filename and save it to variables
For example from 2604230_VIJAY_TAX_02-NOV-2018.xls filename, I want to extract 2604230, VIJAY, TAX and 02-NOV-2018 and save them to variables.
Can someone please help me in this?
Your question is in fact off topic, but I will show you one way at least on how to echo each token, regardless.
for /f "tokens=1-4 delims=_" %%i in ('dir /B /A-D "c:\temp\*_*.xls"') do echo %%i %%j %%k %%l
This simply splits the string by the _ and assigns 4 tokens to each value split.
If you want to actually assign them to vaiables, you would need to use the set command, if inside a loop, you would probably need delayed expansion so I suggest you open cmd.exe and run the following commands to get the help files
for /?
set /?
setlocal /?
This question already has answers here:
windows cmd: problems with for /f with a quoted command with quoted parameters
(2 answers)
Closed 5 years ago.
In a batch file I want to set a variable to the one-line output of a program - a task explained in multiple questions and internet ressources.
In my specific task I want to execute the command
"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"
As you can see, the path for the program I want to execute, FileVersion.exe contains spaces, and the second parameter handed to FileVersion.exe contains a full path with spaces as well.
When I call the above command either directly at the command line or by using CALL in the batch file it correctly executes the program prints out the expected output.
Now, to capture the output of executing the program I use the follow batch command
FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
echo %%i
)
The FOR documentation says
usebackq Use the alternate quoting style:
- Use double quotes for long file names in "filenameset".
- Use single quotes for 'Text string to process'
- Use back quotes for `command to process`
I think I did this correctly by using back quotes around the command, and using quotes around the filenames.
However, the call fails with the message (translated from German)
The command "D:\My" is either incorrect or couldn't be found.
I think this points to a problem with escaping the spaces.
The weird part: If I either replace the path of the exe to call, or the parameter with a path sans spaces, it works. So both
FOR /F "usebackq tokens=* delims=" %%i IN (`FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
echo %%i
)
and
FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n Test.exe`) DO (
echo %%i
)
work.
Why don't two quote-escaped strings work in the same command and how can I fix it?
I wouldn't use usebackq and first resolve the complex path:
#Echo off
Pushd "D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release"
FOR /F "tokens=*" %%i IN (
' FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe" '
) DO echo %%i
PopD
This question already has answers here:
What does symbol ^ mean in Batch script?
(4 answers)
Closed 6 years ago.
I have a simple command within my bat file that is causing it to explode, I think it has to do with using a string within the command but I am a bit clueless now.
The command in question is:
for /f %%i in ('dir *.nupkg /b/a-d/od/t:c | findstr "symbols"') do set LAST=%%i
What is the right way of using the "symbols" string in the above line?
I don't understand why you're piping the results through Findstr.
For /F "Delims=" %%A In ('Dir/B/A-D/OD/T:C *symbols*.nupkg') Do Set "LAST=%%A"
If you're not sure as to whether the last file will contain the string symbols in it's name, and that is what you're trying to ascertain, then you could still check that after the loop.
For /F "Delims=" %%A In ('Dir/B/A-D/OD/T:C *.nupkg') Do Set "LAST=%%A"
If "%LAST:symbols=%"=="%LAST%" (Echo= NOT a symbols file) Else (
Echo= WAS a symbols file)
I have a java properties file to parse, it contains key value pairs of the form key=value, one on each line.
After digging around on the site, I've found this site that explains the FOR syntax, and also this question.
I constructed the following line to get the value of the backupdir.windows property in config.properties:
for /f "delims== tokens=2" %i in ('findstr backupdir.windows= config.properties') do #echo %i
The above works if you type it at the command prompt, but if I save it as a file 'test.cmd' and then execute that, I get 'i was unexpected at this time.'
Same thing happens if I change the extension to .bat (in case doing so would make it use
earlier MSDOS syntax).
What's going wrong here? I'm running Windows 7.
Better to do it like this:
#echo off
for /f "tokens=2 delims==" %%a in ('findstr /b /i "backupdir.windows" config.properties') do echo %%a
it needs to be %%i in a batch file. And just %i on the command line.