I have multiple files with dates on them I would like to strip.
exOpTimer01232018.txt
exOpProcess01232018.txt
exOpFac01232018.txt
exOpProd01232018.txt
I would like to have a batch script remove the date and leave result such as
exOpTimer.txt
exOpProcess.txt
exOpFac.txt
exOpProd.txt
These are monthly file and the date stamp changes every month.
I have tried doing
RENAME C:\temp\*????????.txt *.txt
But wasn't successful.
Example based on my comment:
#Echo Off
For /F "Delims=" %%A In ('Where .:exOp*.txt 2^>Nul') Do Call :Loop "%%A"
Pause
Exit
:Loop
Set "fName=%~n1"
Ren %1 "%fName:~,-8%%~x1"
Related
I have a batch script to get two dates; one of a folder, and the current system date.
I want to specifically compare the two by seeing if the date of the folder is 10 minutes or less older than the current date. This essentially checks if the user has modified this folder at the most 10 minutes ago.
Here's my current code (not complete, but the base):
#echo off
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a/%%b/%%c)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b)
setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{recycle-bin-date}=%%a")
for %%a in ("!{recycle-bin-date}!") do (
Set "data=%%~ta"
)
popd
)
set date1=%mydate% %mytime%
set date2=!data!
echo Date 1 (Current): %date1%
echo Date 2 (Recycle): %date2%
pause
::We have the dates above, how do I achieve what I'm trying to do?
If anyone could help me here, I'd really appreciate it.
You CAN do date time math in pure batch but it is quite cumbersome.
(See Ritchie Lawrence batch function library)
I recommend to use PowerShell as a tool for this
PowerShell one liner:
[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes
Wrapped in a batch
#Echo off
For /f "usebackq" %%A in (`
Powershell -NoP -C "[int]([datetime]::Now - (gci 'c:\$Recycle.BIN\S-1-*1001' -Force).LastWriteTime).TotalMinutes"
`) Do Set "AgeMinutes=%%A"
Echo Age in minutes %AgeMinutes%
I need to do a batch file which order all files by a date interval, example:
#echo off
echo Input the date(dd/mm/yyyy):
set /p compDate=
::After that I will compare from the actual day (%date%), example:
set interval = %compDate% - %date%... *Something like that*
::After that I need to list all files from a specific directory, example:
echo Input the directory:
set /p directory=
SET Exit= %UserProfile%\Desktop\test.txt
::After that I might need dir /tc to get the creation date, example:
pushd "%directory%"
dir /s /tc /a-d > %Exit%
::After that I don't know how to get only the lines which are in date interval, example:
Today is 19/08/2014, but I want to search all files created from day 10/07/2014.
So I have to copy all lines which have the date 10/07/2014, 11/07/2014, 12/07/2014 and so on until stop on today created files.
I tried with findstr, but I can't set the date interval, just a specific date to search in the .txt created.
Somebody know how to do that?
If I correctly understood the request, you really don't want files created in a given interval, but files created after a given date. The Batch file below assume that the date used by the system appear in DD/MM/YYYY order:
EDIT: Some modifications as reply to the comments
#echo off
setlocal EnableDelayedExpansion
echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a
echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt
pushd "%directory%"
(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
set "fileDate=%%a"
if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
if !fileDate! geq %compDate% (
set "fileSize= %%e"
echo %%a %%b %%c %%d !fileSize:~-16! %%f
)
)
)) > %Exit%
popd
A solution that uses WMIC and is independent from time/date settings:
#echo off
setlocal
set /p compDate=Input the date(yyyymmdd):
set /p directory=Full directory path (with no slash at the end):
set exit_file= %UserProfile%\Desktop\test.txt
break>%exit_file%
for /f "tokens=1,2 delims=:" %%a in ("%directory%") do (
set "dir_drive=%%~a:"
set "dir_path=%%~b\"
)
set dir_path=%dir_path:\=\\%
setlocal enableDelayedExpansion
for /f "useback skip=1 tokens=1,2* delims=:" %%f in (`" wmic datafile where (drive='!dir_drive!' and path like '%dir_path%') get CreationDate^,name"`) do (
set creation_date=%%f
set creation_date=!creation_date:~0,8!
set "file_name=%dir_drive%%%~g"
if 1!creation_date! GTR 1%compDate% (
echo !file_name!>>%exit_file%
)
)
exit /b 0
I am trying to rename some log files to yesterday's date when the batch file creates a new file of same name every night.
We can rename the file to today's date using the below cmd
ren SampleDTE.TXT SampleDTE-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
This results in file renamed to // SampleDTE-YYYYDDMM_hhmm.TXT
SampleDTE-20132712_1243.TXT
I wanted to know how to re-name the file to yesterday's date. Something like
SampleDTE-20132612_1243.TXT
Thanks in advance
The easy way - assuming that you run this regularly, once per day
FOR /f %%a IN (sampledteyesterday.txt) DO ECHO ren SampleDTE.TXT SampleDTE-%%a_%time:~0,2%%time:~3,2%.txt
> sampledteyesterday.txt ECHO %date:~10,4%%day%%date:~4,2%
Note - ren command simply ECHOed. when verified, remove the ECHO keyword before the REN to activate.
You'll need to set up your sampledteyesterday.txt file containing a single line YYYYDDMM for yesterday to initialise.
Suggestion: use YYYYMMDD which sorts easier or more logically...
You will have to use a variable and do the math:
set /a day=%date:~7,2% - 1
ren SampleDTE.TXT SampleDTE-%date:~10,4%%day%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
To avoid date arithmetics, you can store yesterday date in, eg, file.
yesterday.txt (contains today and yesterday):
20131227 20131226
Batch file:
REM Get today (to check if yesterday.txt is valid):
SET today=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
REM Read file:
FOR /F "TOKENS=1,2" %%d IN (yesterday.txt) DO (
SET stored_today=%%d
SET yesterday=%%e
)
REM If stored_today not equal to today, assume yesterday is stored_today and update file:
IF NOT "%stored_today%" == "%today%" (
SET yesterday=%stored_today%
>yesterday.txt ECHO %stored_today% %today%
)
REM Test if yesterday is set, exit otherwise.
IF "%yesterday%"=="" ECHO Yesterday unknown! Try again tomorrow.&GOTO:EOF
To make it work correctly first time, yesterday.txt must be manually filled.
This will get yesterdays date, using VBS in a batch file.
It's reliable in all locales, whereas the %date% variable can be different on different computers, and different users.
#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 "date-yesterday=%yyyy%-%mm%-%dd%"
echo Yesterday was "%date-yesterday%"
pause
How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd
I can't seem to understand how batch files add yyyy/mo/dd/hh/mm/ss to the beginning of filenames. (Using Windows 7) Accuracy to the second is important.
It doesn't actually have to be a batch file, it just has to be a small program which can be executed by Directory Monitor whenever I add files to a folder: http://brutaldev.com/page/Directory-Monitor.aspx
I only imagine that a batch file would be the simplest and most efficient approach, but any other suggestions are welcome.
I work with many sequentially numbered files with overlapping filenames and I need a quick way to rename them whenever I add them to a folder such that there will never be any file with the same name yet they will still remain in sequential order. This is how I thought of adding the current date and time to the beginning of the filename and why seconds are important, since I can easily add multiple sets to a folder in under a minute but certainly not under a second. It would be ideal if the batch file could ignore file extensions and simply add the current date/time to the beginning of any file added to the folder.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & 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 "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file
for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"
pause
#ECHO off
SETLOCAL
IF [%1] NEQ [] goto s_start
:: Author - Simon Sheppard, July 2003
:: Tested for Windows NT, 2K, XP
ECHO STAMPME.cmd
ECHO REName a file with the DATE/Time
ECHO.
ECHO SYNTAX
ECHO STAMPME TestFile.txt
ECHO.
ECHO STAMPME "Test File.txt"
ECHO.
ECHO STAMPME "c:\docs\Test File.txt"
ECHO.
ECHO In a batch file use CALL STAMPME ...
:: To change the filename format just change around the last line below
GOTO :eof
:s_start
SET _file=%~n1%
SET _pathname=%~f1%
SET _ext=%~x1%
::Get the date
:: note ISO 8601 date format would require 4 digit YYYY Year)
FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
SET _mm=%%G
SET _dd=%%H
SET _yy=%%I
)
:: Get the time
FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
SET _hr=%%G
SET _min=%%H
SET _sec=%%I
GOTO :done
)
:done
ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
ECHO The time is: [%_hr%]:[%_min%]:[%_sec%]
REN "%_pathname%" "%_hr%-%_min%-%_sec%#%_file%%_ext%"
This seems to work for me
I'd prefer solutions, that are not dependent to local settings (wmic gives always the same format):
#echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!
echo seconds ### ren %%a !LocalDateTime:~0,14!%%a
echo milliseconds ### ren %%a !LocalDateTime:~0,18!%%a
echo with separator ### ren %%a !ldt!-%%a
)