[ms-dos] command output in variable

zdd43 -  
 plaiserman -
lu'

I'm looking to retrieve the result of a DOS command into a variable. To use it later.

17 réponses

SinDromX
 
I have something way simpler, works perfectly under Windows XP SP2

set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6,4%

My variable day will take the 2 characters starting from the first
My variable month will take the 2 characters starting from the third
My variable year will take the 4 characters starting from the sixth

This corresponds well to the display of my date which returns a result like this:
18/07/2007

Simple and practical, just the way I like it.

P.S: I know that the initial message is a bit dated, but for those who are still looking, it might be useful.
19
JoanPedroMoultoBueno
 
thank you it works under seven and it’s exactly what I was looking for
fast and effective
JP
0
Ch@oS Posted messages 46 Registration date   Status Membre 35
 
@echo off
if "%1"=="" goto passource
if not defined date goto pasdate
rem *****************************************************
rem The date stored in %date% contains the character "/"
rem which we cannot use to rename files because
rem it is a special character in Windows. Therefore, we replace
rem it with "_"
rem *****************************************************
for /f "tokens=1-3 delims=/" %%a in ("%date%") do set newdate=%%a_%%b_%%c_
rem We rename the file...
ren "%1" "%1_%newdate%.%2"

And there you go...
9
Christophe
 
Hello,
I'm having a bit of trouble renaming a file test.zip to test060303.zip in a batch file, so compiling the original name and the current date. If you can help me with this part, I thank you very much.
Chris from Mayotte.
0
jivef Posted messages 1024 Status Membre 306
 
Hello,
The proposed solutions are quite nice.
Personally, I mostly use a GNU/Linux system, but unfortunately, a good portion of my clients are too trapped in Windows because of their applications.

That said, regarding the retrieval of the date, in my opinion, the best approach is to use an ISO 8601 type format because files named with the date in this format will always be sorted in order and easier to find.
They will be in both numerical and chronological order, effortlessly.

To this end, I created a small batch snippet that I reuse whenever I need it and that allows me to create a date variable in the specified format.
From this variable, which I call GDH (for Date-Time Group), I can write to a file, rename a file, create a directory, etc.


For anyone who still believes that UNIX syntax is lengthy, here’s an example to the contrary.
Unix or GNU/Linux Syntax

$ gdh='date +%Y%m%d_%I%M%S' 

To verify the result:

$ echo $gdh 20110518_083849 $ 

With UNIX, it only took me one line...

With DOS, I'll let you check the work.
That said, I appreciate the solution from sindromix, which seems particularly elegant and could improve mine a bit.


echo this batch gives a date hour group (GDH) in the form YYYYMMDDHHMMss (ISO 8601 standard without separators.) echo Year month day minutes seconds. echo you can use this batch as part of another. echo This block can be used to rename a file indicating the date of the operation. echo example "daily backup" rem for loop to create variable for using a date-time group (GDH) for /F "delims=/ tokens=1,2,3" %%1 in ('echo %date%') do set VARDATE=%%3%%2%%1 rem Retrieve date in ISO 8601:2004 format (YYYYMMDD) for /F "delims=: tokens=1,2,3" %%1 in ('echo %time%') do set VARTIME=%%1%%2%%3 for /f %%1 in ('echo %VARTIME%') do SET VARTIME=%%1 rem Retrieve hour in HHMMSS format rem Create GDH (Date Time Group) SET GDH=%VARDATE%_%VARTIME% echo %GDH% rem create a journal file with ISO8601 date echo %GDH% > %GDH%.log rem end timestamp echo use the GDH variable to rename a file. Echo create the file sauve.log echo . sauve.log > sauve.log example below renaming ren sauve.log %GDH%.log rem feel free to put the file or folder to rename in a variable, it's cleaner. 
Example of a file renamed with this function:
20110510_170514.log
(file sauve.log renamed).
We can also write to a file to have the date at the precise time an event occurred on the system.



Best regards.
Jonas V.F.
0
Mellah2 Posted messages 2 Status Membre 3
 
Hello,

Here is another example.

file: test.bat

for /F "tokens=1,2,3,4 delims=/ " %%d in ('date /T') do ( set Day=%%d%%e%%f)
rename test.zip test%Day%.zip

Herve
3
zeep
 
Thank you for your efficient and elegant solution.
0
doudou
 
Awesome your solution mellah2, you saved my life.
0
jivef Posted messages 1024 Status Membre 306
 
Hello,
Under Windows or MS-DOS, the syntax is quite complex:

In my example, I put my public IP address into the variable and I use wget that I downloaded.

Inside a batch file, you would write
for /f %%1 in ('wget -qO- icanhazip.com') do set PUBLICIP=%%~n1
echo %PUBLICIP%

In a direct command line, you would write:
for /f %1 in ('wget -qO- icanhazip.com') do set PUBLICIP=%~n1
echo %PUBLICIP%

Microsoft is twisted, they can't even manage to have the same syntax in line and in batch...

If you want to put the content of a file, you would write:
for /f %1 in ('type monfichier.txt') do set MONFIC=%~n1
echo %MONFIC%

be careful, the file contains only one line, otherwise, it won’t work.

Best regards.
Jonas V. F.
--

A received idea is often a dead idea.
3
neo_selen Posted messages 237 Status Membre 15
 
0
jivef Posted messages 1024 Status Membre 306
 
Strange, the ""="" is normally forbidden in DOS.
In my opinion, you should list your files via the DOS command window, not through the graphical interface.
Make the list of your files and redirect the output to another file.

c:\> dir > %temp%\ficlist.txt
Then, to find your file, go to:
open %temp%\ficlist.txt
0
neo_selen Posted messages 237 Status Membre 15
 
ah I know that it's impossible under DOS
minecraft correctly names its directory in %appdata%:
.minecraft
and yet the game works perfectly..
DOS prevents this because there is no existing syntax to despecialize a character...
0
neo_selen Posted messages 237 Status Membre 15
 
In other languages, PHP, Javascript, it exists.
But for example, in VBScript, nothing! Absolutely nothing.
Thanks for the tip :)
0
jivef Posted messages 1024 Status Membre 306
 
The . at the beginning of a file is not prohibited by DOS.
It is the = sign that is prohibited.

Note that in my opinion, the most permissive system is still UNIX in all its variations. Only with UNIX can you name a file "*" By the way, this sometimes happens accidentally.
0
sam3000 Posted messages 1226 Status Membre 144
 
salut voici une solution qui marche avec "DATE" seulement, pour une autre commande, il faudrait que tu trouves le "truc" correspondant.

on a besoin de deux fichiers, le nom du premier "la.bat" est obligatoire, il ne faut pas le changer, le second tu es libre de le nommer:

---LA.BAT
 @echo off :repeter if "%1"=="" goto fin set vdt=%1 shift goto repeter :fin 


---VARAB.BAT
 @echo off echo. >f1.tmp echo @echo off >f1.bat echo @echo off >entrez.bat date <f1.tmp>>f1.bat call f1.bat del f1.tmp del f1.bat del entrez.bat echo %vdt% </f1.tmp>


le résultat (date) est dans la variable %vdt%
si tu ne comprends pas l'astuce utilisée, pose des questions.
sur ce, @*
--
L'erreur est humaine mais un véritable désastre
ne peut être qu'informatique.
2
SinDromX
 
Hello,
You cannot choose the date format that will be returned on Windows.

But you can always create a small .bat file that will contain the instructions I mentioned:

set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6,4%
echo %year%/%month%/%day%

Although I'm doing a test and I realize there's a space between the year and the first slash ( / )... :-(
2
yopyop
 
slu
You can create a new intermediate variable for the year and extract it, and it works
set nannee=%annee:~0,4%
echo %nannee%/%mois%/%jour%
0
jivef Posted messages 1024 Status Membre 306
 
Hello,
Well spotted, even simpler and no need for a for loop.



(I’m more used to Unix syntax which I find richer...)
On Linux, there’s even an option to get the ISO 8601 format right away...

tux:/home/jivef # date --iso-8601
2012-01-16

And to create the folder with that name:
tux:/home/jivef # mkdir 'date --iso-8601'

Let’s check:
tux:/home/jivef # ls -ld 2012*
drwxr-xr-x 2 root root 4096 2012-01-16 18:30 2012-01-16

There’s the job done.
With bash on GNU/Linux: 1 single line, with DOS a bunch.

See you later.
Jonas.
0
zdd43
 
Please!!!!
Doesn't anyone know DOS or what?
I can create a variable,
put text or anything else
but the date command that returns text
like "Wed 01/06/05"
I can't retrieve it in my variable
even with something like:

date /t > variable

but it doesn't work!!!

Does anyone have an idea??

Thanks in advance
1
jivef Posted messages 1024 Status Membre 306
 
Hello,
Thanks to an option in the set command, you can put the content of a file into a variable:
For the date, here’s the procedure to follow.
C:\temp>date /t > date.txt

C:\temp>set /p date=< date.txt

C:\temp>echo %date%
22/06/2011

A bit far-fetched, but it's M$.
0
plaiserman
 
all simply:

SET day=%date%
SET time=%time:~0,5%
echo %day%
echo %time%
0
sam3000 Posted messages 1226 Status Membre 144
 
Hi,
it's not as easy to use DOS
if it were Linux (or another *nix) it would be very doable!
actually the thing is to find out how to create a second Batch containing the value and the variable on the same line and then execute it
I'm looking for a solution and I'll get back to you.
--
To err is human but a true disaster
can only be technological.
1
zdd43
 
lu'

thank you very much for the attention you are giving me!!
I will try your idea right away, which seems great.
I will keep you updated.
0
zdd43
 
I made some progress but the problem now is the format
of the date... I hadn't thought about it but the date being in the form:
dd/mm/yy, it doesn't create a directory (as I want)
but a complete hierarchy.

I'm stuck...
0
zdd43
 
^^ sorry but I don't understand everything...
and I don't know what I did but when I run either script
it doesn't seem to work. I'm a bit clueless.
In any case, thank you for the time you spend on my issues.
But as I mentioned earlier, the format of the date is
causing me problems for creating a folder later that takes the date
as its name. This creates a structure.
It seems to be a dead end!!
1
sam3000 Posted messages 1226 Status Membre 144
 
your problem is the "/" separator in the date
you need to change your date display settings
if you are on Windows XP, go to:

+control panel
++regional and language options
+++Button [Customize] (in the "regional options" tab in the "Standards and formats" area)
++++Tab "Date"
in the "date separator", choose the "-" (minus sign)
in the short date format select "yyyy-mm-dd"

you confirm the changes and there you go! under DOS you have dates in the "2005-06-02" format which is valid for creating a folder

otherwise in my programs, you need to place both files in the same folder, and run the program "VARAB.BAT" and put all date processing in place of "echo %vdt%"

with that, @*

--
To err is human but a true disaster
can only be computer-related.
1
zdd43
 
^^ Yesterday it worked!!!!
I was able to create my directory, it's great
Thank you so much.
For your information, I was on Win NT4 but you couldn't have known
so I modified the registry
at the separator level.
HKEY_CURRENT_USER\Control Panel\International\sDate :
and I replaced "/" with "-"

^^ thanks again
1
sam3000 Posted messages 1226 Status Membre 144
 
You're welcome,
if you have any other questions, don't hesitate.
That said, @*
--
The error is human but a true disaster
can only be technological.
1
Med
 
Hello,
is there a command to manage the date format? For example, if the default displayed date is 15/10/2007, what command line allows me to change the date format so that the command date /t returns 2007/10/15?
thank you for your answers.

Med
1
jivef Posted messages 1024 Status Membre 306
 
Hello,
The downside is that with MS-DOS, you don't have an option in the date display that allows you to choose your presentation format.
With systems like GNU/Linux, you have plenty of choices, sometimes even too many.


To set your date in ISO-8601 format (Year-Month-Day), you need to use a "for" loop.


for /F "delims=/ tokens=1,2,3" %i in ('date /t') do set vardate=%3-%2-%1
echo %vardate%

Beware! Microsoft is tricky... In a batch file, the syntax will be slightly different:
for /F "delims=/ tokens=1,2,3" %%i in ('date /t') do set vardate=%%3-%%2-%%1
echo %vardate%
0
Ocean06
 
Hello,
I've been looking for a way to rename a file with ITS date since this morning at 8:00, not the system date, today's date, or anything else...

The issue is being able to retrieve the date of a file into a variable so that I can then rename the file in question.

Can anyone help me?
1
Date/
 
Hello,

and now how do I change the date format in / under Vista?
There is no "date separator" option in Date, Customize regional settings.

Thank you
1
Shatiyn
 
Hello,
I regularly use these lines to retrieve the day, month, year, hour, minute, second, and hundredth in individual variables.
If that can help you.

for /f "tokens=6-8 delims=:/ " %%a in ('"echo. ^|date|find /i "is""') do set day1=%%a&set month1=%%b& set year1=%%c
for /f "tokens=4-7 delims=:, " %%a in ('"echo. ^|time|find /i "is""') do set hour1=%%a&set minute1=%%b& set second1=%%c& set hundredth1=%%d

echo Start of processing = %hour1%:%minute1%:%second1%,%hundredth1%


It's up to you to set the desired separators.
1
neo_selen Posted messages 237 Status Membre 15
 
et for the delims option, I have a big problem: the files have such strange names that they all end with an = sign. how can I completely rename these files to toto.jpg instead of: (this is an example of what I have)

CWKlKODMVbRMwdc1yYpgQN4+sAA=
EB2FBs99ImnfjbZoGMpFVYJyvjIo=

and I found everything I need to rename files to .ext or something else but not for this rather difficult case. Can we replace with a %ext%
and declare that:
set ext = "=" ?
or another trick like that?
1
neo_selen Posted messages 237 Status Membre 15
 
I am creating a batch script that allows copying the cache of a software that has images with quite curious names, but when renamed to .jpg, they provide perfect visibility of the image. The only limitation of this batch is the one described! The length of the names seems to be variable.
0
karirovax Posted messages 3584 Status Membre 215
 
Hello

First, you need to know the location of the files, then use the following commands:

attrib -s -h -r -a "file path" /s /d
ren "file path\*.xxx" "file path\*.jpg"
0
neo_selen Posted messages 237 Status Membre 15
 
Hey! (This is my first topic revival)
Well, I still haven't found it. In the meantime, I've scripted some other stuff :) but unfortunately, in the file names I've mentioned, your methods won't work, and here's why:
There are no extensions! You put .xxx but there's no point where I can indicate the "token delims", except for the equal sign...
The only way is to combine my basic knowledge in batch scripting and vbscript:
Indeed, vbscript can execute commands just like batch, and you can even incorporate batch commands into vbscript. (exclusive to Windows, both.)
So, why not create a text variable where I read the file name, then replace it with another one, counted by a for loop?
Well, I'll test it and get back to you. I'm scripting a lot to learn, but that's not the only reason (I won't let the cigarette cool down xD)
0
librequetucrois
 
Hi

The image extension is at the beginning of the file; readable with Notepad.

If you can read the beginning of the file (redirected 'type' command), it can be useful.

This kind of name is the name of the image files for Windows Live Messenger.

The extension is ".dt2" ... ; files attached to ".id2" files; user profile images:

C:\Users\%USERNAME%\AppData\Local\Microsoft\Messenger\x@x.x\ObjectStore\UserTile

Check the images with a file hasher; they appear identical but are not actually.

The images are ".jfif" or ".png" depending on the version of Messenger, recent or not (...).

Check the color depth ... 24 => 32; the images are normalized.

Template for the name for your personal or non-personal images (the ones you want to save):

AvatarNOMNOM32BitsCRC32=12345678date20120117-XXhXXmXX.png

6 letters for the name max; the name visible on mouseover in WLM 2011 will be:

"AvatarNOMNOM32BitsCRC32=12345678date2012" or just "?"

I was looking for information on how to pass a variable to reduce commands in order to put more things; by launching the commands directly. (( Variable set with cmd k does not work | CommentCaMarche:
http://www.commentcamarche.net/forum/affich-24169826-variable-set-avec-cmd-k-ne-fonctionne-pas )) ; thanks in advance in case.
0
librequetucrois
 
slt re

dateheuretestbouclefor.bat

1.19 Ko (1,226 bytes)

CRC32: 8FED040E
MD5: 8C3482BC997E14865989478CF3D8E1A3
SHA-1: 21BABB0E153B7C4C948383C80673DA920FD731E7
SHA-256: 75207654EF29F3DA93EBD64BDF20C7076FA48AD3798D7409FA80F0E4D9033A77

@echo off setlocal :tettast cls echo test date heure boucle = add 0 for 'am' time and format date and time set mimi= echo. echo --------- echo %date% echo --------- echo %time% echo --------- echo. for /F "tokens=1,2,3,4,5,6 delims=/^:, " %%i in ("%date%%time%") do set mimi=%%k%%i%%j-%%lh%%mm%%n rem EQU - equal to rem NEQ - not equal to rem LSS - less than rem LEQ - less than or equal to rem GTR - greater than rem GEQ - greater than or equal to echo Display = 2 hours or 12 hours ... set mimitest24hoursam=%mimi:~0,8%-2h%time:~3,2%m%time:~6,2% echo %mimitest24hoursam% set mimitest24hourspm=%mimi:~0,8%-12h%time:~3,2%m%time:~6,2% echo %mimitest24hourspm% echo. echo Dynamical hours and format date and time ... if not %%l geq 10 goto notime10to24hours echo varfilename%mimi:~0,9%%mimi:~9,17%-CRC32=varcrc32file.varextfile = time10to24hours (pm) echo. :notime10to24hours if not %%l leq 9 goto notime0to9hours echo varfilename%mimi:~0,9%0%mimi:~9,17%-CRC32=varcrc32file.varextfile = time0to9hours (am) echo. :notime0to9hours rem echo %mimi:~0,9% rem echo Z%mimi:~10,17% echo. echo Ctrl + c to exit the loop echo. timeout /t 5 goto tettast :eof endlocal exit /B


à vous lire
0