0

So far I have a batch file that renames the file updating the date string in parenthesis. This works fine , but the date could be any number. How do I get the batch to update any date string contained in parentheses.

echo on
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%


set "_year=%MyDate:~0,4%"
set "_month=%MyDate:~4,2%"
set "_day=%MyDate:~6,2%"

REN C:\Users\xyz125\Documents\Erics's Docs\scripts\"test file (20150112).txt" "test file (%_year%%_month%%_day%).txt"

pause
EJohnson
  • 23
  • 1
  • 4

2 Answers2

0

Try dir/b | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])"

This results in the following file this (20150609)something.txt and the folder also contains a file named test(1234)else.txt which is not found because the regex is looking for 8 numeric digits not 4. That pattern would be dir/b | Findstr /r "([0-9][0-9][0-9][0-9])"`

This post helped Piped Variable Into FINDSTR w/ Regular Expressions and Escaped Double Quotes you could do something similar, and set a variable with the string you want the file renamed to.

-Editing to add a note about String Concatenation see here

Also see the answers here Generate unique file name with timestamp in batch script

Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57
  • Im not familiar with reg express. Would it look anything close to this: echo ON for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2% set "_year=%MyDate:~0,4%" set "_month=%MyDate:~4,2%" set "_day=%MyDate:~6,2%" C:\Batch | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])" REN "C:\Batch\ | Findstr /r (%_year%%_month%%_day%).txt" rem ^ pause – EJohnson Jun 11 '15 at 01:02
  • Please edit the question with the code update, trying to make sense out of code in a comment is not fun. – user4317867 Jun 12 '15 at 03:16
0
@4317867.  Sorry about that.  For some reason I could not edit the code while pasting a response comment. Im not familiar with reg express. Would it look anything close to this: 



echo ON
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%


set "_year=%MyDate:~0,4%"
set "_month=%MyDate:~4,2%"
set "_day=%MyDate:~6,2%"

 C:\Batch | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])"

REN "C:\Batch\ | Findstr /r (%_year%%_month%%_day%).txt"

rem ^

pause
EJohnson
  • 23
  • 1
  • 4