Batch For Loop w/ Timestamp - datetime

I need to rename multiple files in a loop and include a time stamp... My struggles begin with updating the timestamp for each file so the file has a unique name.
setlocal enabledelayedexpansion
SET date=%date:~-4,4%%date:~-10,2%%date:~-7,2%
for /f %%a in ('dir /b TCA_*') do (SET
time=%time:~-11,2%%time:~-8,2%%time:~-5,2%%time:~-2,2%
ren %%a %date%TCA_%time%.txt)
Thanks

While in a loop, variables don't update. To circumnavigate this, the setlocal enabledelayedexpansion script was added.
Basically, if you want them to update, you need to encase them in exclamation marks (!) instead of percent signs (%).

Related

Probleme with batch - script

i have 1000 photos with extension .jpg and every name of photo is a code like this 12345.jpg.
And i have 1000 folders with names like this: text_text_text_code(000000)
the job is put every photo in every single folder, i can solve this, but not working very well.
I found the problem, but i don't know how to solve.
in name folder i have text_text_text_code(000000), i need the code to stop when it arrives at character (.
#echo off
chcp 1250
echo.
echo.
cd "C:\Users\folderexample\Desktop\123"
for /r "C:\Users\folderexample\Desktop\imagem" %%a in (*.jpg) do (
for /f %%b in ('dir /b *"%%~na"*') do copy "%%a" "%%~fb"
)
I would like the search to go to the character ( and go to next search up until finish photos.

How to get yesterday's date in a particular format?

I need to set Today and yesterday's date in a variable in a fixed format YYYYMMDD.
For today date, when i did
SET TODAY=%date:~10,4%%date:~4,2%%date:~7,2%
it worked and displayed '20190426'.
But how to set yesterday's date so I get it in the format - 20190425 ?
Update The original unix and linux tags were later changed to cmd and batch-file, which this Linux / Bash / sh solution won't apply to.
To get yesterday's date:
$ date +%Y%m%d --date yesterday
20190425
To get it into a var:
$ var=$(date +%Y%m%d --date yesterday)
$ echo $var
20190425
There are literally hundreds/thousands of questions just here on SO.
I suggest you use a PowerShell one-liner for this, which you can call from a batch file as follows:
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'{0:yyyyMMdd}' -f (Get-Date).AddDays(-1)"
`) do set YESTERDAY=%%A
%YESTERDAY% will then contain 20190824 when invoked on 25 August 2019, for instance.
A slightly longer variant, incorporating both today and yesterday in only one PowerShell invocation.
:: Q:\Test\2019\04\26\SO_55862158.cmd
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'yesterday={0:yyyyMMdd}' -f (Get-Date).AddDays(-1);'today={0:yyyyMMdd}' -f (Get-Date)"
`) do set "%%A"
The PowerShell part issues two lines
yesterday=20190824
today=20190825
which are parsed by the for /f and set as environment variables yesterday/today respectivly.
You can use this batch/vbs hybrid, you need to save it as a .bat or .cmd extension file.:
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%yyyy%%mm%%dd%"
echo %final%
Note, you can toggle the number of days in the set day=-1 line.
Previously posted answers are not pure Batch-file solutions... You may use the method explained at this answer or just use this simpler approach:
#echo off
setlocal
set /A "YYYY=%date:~10,4%, MM=1%date:~4,2%, M=MM-100, DD=1%date:~7,2%, D=DD-100"
echo TODAY: %YYYY%%MM:~1%%DD:~1%
set /A "C1=!(D-=1),M-=C1*(1-12*(C2=!(M-1))),YYYY-=C1*C2,MM=100+M,DD=100+(D+=C1*(30+((M+(M>>3))&1)-!(M-2)*(2-!(YYYY%%4))))"
echo YESTERDAY: %YYYY%%MM:~1%%DD:~1%

How can i use the variable %TIME:~0,2% in a batch file so that times with a leading space do not cause errors?

I am trying to run a batch file, which runs an XSLT transformation against an XML file and writes out a second XML file.
This XML filename is determined by the following line in the batch file:
ICS_%DATE:~-4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%_%TIME:~3,2%_DATA.xml
When the time has a leading space (that is, any time before 10:00 am), the variable %TIME:~3,2% returns a result with a leading space, which causes the filename to be truncated. The result file is empty.
If I run the batch after 10:00am, everything works fine. How can I generate a value similar to %TIME:~3,2%that works before 10:00am?
This will solve the space in the name issue, and replace it with a zero so it sorts correctly in a list.
set name=ICS_%DATE:~-4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%_%TIME:~3,2%_DATA.xml
set name=%name: =0%
How about this one ?
#ECHO OFF
FOR /F "tokens=1-4 delims=., " %%i IN ('DATE /t') DO SET cpdate=%%k_%%j_%%i
FOR /F "tokens=1-4 delims=:" %%b IN ('TIME /T') DO SET cptime=%%b_%%c
set filename=blabla_%cpdate%_%cptime%_%TIME:~-5,2%.xml
echo %filename%
The locale here is german, so you might have to adjust the order in "set cpdate..." for your needs.
(This is an old question but this answer could be of help for somebody else.)
Using %time: =% gets rid of the spaces in the time variable.
Example:
C:\>echo %time%
04:00:00,00
C:\>echo %time: =%
04:00:00,00
You could save the time variable to another using this method and then work from that one:
set "tim2=%time: =%"
Setting a variable for the hour value as follows solves the problem for me:
FOR /F "tokens=* delims= " %%a IN ("%TIME:~0,2%") DO SET hour=%%a
set time=%time:~0,5%
if "%time:~0,1%" == " " set time=0%time:~1,5%
echo %time:~0,2%%time:~3,2%
my time is: 9:48:27,00
I add a zero if it is missing and
echo %time:~0,2%%time:~3,2% is now 0948
I use a simple trick to do this:
set h=1%TIME:~0,2%
set /a h=%h%-100
REM This .bat script gives you a parameter timetext with a zero instead of a space
#echo off
set timetext=%time:~0,2%%time:~3,2%
echo TIME BEFORE WITH SPACE %timetext%
set digit1=%time:~0,1%
if "%digit1%"==" " set digit1=0
set timetext=%digit1%%time:~1,1%%time:~3,2%
echo TIME AFTER WITH ZERO.. %timetext%
pause
This maybe more helpful in Dos Batch ~~ try this
if "%time:~0,1%" == " " (set dtstamp=0%time:~1,1%) ELSE set dtstamp=%time:~0,2%
echo dtstamp=%date:~6%%date:~3,2%%date:~0,2%_%dtstamp%%time:~3,2%

Batch file: get file timestamp date only and age in days

The following few lines output the names of certain files in a folder, a delimiter, and a timestamp.
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do echo %indent%%fileBullet% %%F%delimeter% %%~tF
So, produces something like this
Response.docx; 02/07/2013 12:13 PM
I'd like to remove the time portion of the timestamp (so date only), followed by how many days old the file is. So
Response.docx; 02/07/2013; 14
I've found some fairly lengthy solutions online that contain a dozen or so lines. Is there a short and sweet approach?
Here's something shorter and sweeter. It's not as short and sweet as you'd like, but at least it's not 12 lines of code. :)
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do call :datediff "%indent%%fileBullet% %%F%delimeter%" %%~tF
goto :EOF
:datediff
echo wscript.echo DateDiff^("d", "%2", Date^(^)^)>"%temp%\dd.vbs"
set /P i="%~1 %2%delimeter% "<NUL
cscript /nologo "%temp%\dd.vbs"
del /q "%temp%\dd.vbs"

DOS extract directory from find command

I am writing a dos script and I want to get the path of a file that is known so that the path can be used within the script to change to that directory to use the file specified and ouput log files to the same directory.
The script adds some directories to the path and changes to the required directory to execute a command using input files in the same directory. The command generates a number of files that are saved to the same directory.
Here is what i have so far
#ECHO OFF
:: Check argument count
set argC=0
for %%x in (%*) do Set /A argC+=1
IF %argC% LSS 3 (echo WARNING must include the parent Directory, the filename and the timestep for the simulation)
:: Assign meaningfull names to the input arguments
set parentDirectory=%1
set filename=%2
set scenarioTimestep=%3
:: Check validaty of the input arguments
:: TODO: implement check for directory, filename exists, and possibly limits to the timestep
IF "%parentDirectory%"=="" (
set parentDirectory=P:Parent\Directory
)
IF "%filename%"=="" (
set filename=ship2.xmf
)
IF "%scenarioTimestep%"=="" (
set scenarioTimestep=0.1
)
echo parent Directory: %parentDirectory%
echo filename: %filename%
echo timestep: %scenarioTimestep%
set MSTNFYURI=file:mst.log
set MSTNFYLEVEL=debug
set MSTNFYFLUSH=1
set XSFNFYURI=file:xsf.log
set XSFNFYLEVEL=debug
set XSFNFYFLUSH=1
set parentNFYURI=file:parent.log
set parentNFYLEVEL=debug
set parentNFYFLUSH=1
:: Add the parent directories to the path
set PATH=%parentDirectory%\bin\;%parentDirectory%\bin\ext\;%parentDirectory%\bin\gtkmm\;%parentDirectory%\bin\osg\;%PATH%
:: Change to the target directoy
set tagetDirectory=%parentDirectory%\examples\testing_inputs
cd %tagetDirectory%
echo command will be: ft -c %filename% -T %scenarioTimestep%
::ft -c %filename% -T %scenarioTimestep%
#ECHO ON
What i want to be able to do is instead of using the hard coded directory path examples\testing_inputs for targetDirectoy, i want to be able to search for the filename supplied and change directory to that path.
I know i can get the information displayed using
"dir filename.ext /s"
DOS ouptut
Volume in drive C is OS
Volume Serial Number is XXXX-XXXX
Directory of C:\Users\Me\parent\examples\testing_input
15/11/2012 02:51 PM <size> filename
...
...
How do i extract the directory form this info to be used within the script? Also if there is more than one file of the same name, how can i select the path based on the timestamp of the file?
for /f %%F in ('dir /B /S /A:-D filename.ext ') do set file_path=%%F
pushd %file_path%\..
dir_path=%CD%
popd
echo %file_path%
echo %dir_path%
is this what you looking for?
EDIT: Check dbenham's comment.

Resources