0

How can I add pattern to the following batch script in order to look for file (3) and not for all the files, after finding (3) then have to add [%DailyloadDateTime%] at the end of file.

1. ODS_ATI_NSource-[201712071636]-[201712071637].csv
2. ODS_ATI_NSource-[201712071637].csv
3. ODS_ATI_NSource.csv

for /F "delims=" %%j in ('dir /s /b *.csv') do (
    rename "%%j" "%%~nj-[%DailyloadDateTime%]%%~xj"  
  )
pm1359
  • 622
  • 1
  • 10
  • 31

1 Answers1

0
for /F "delims=" %%j in ('dir /s /b *.csv^|findstr /L /V "["') do (

which directs the dir output through findstr which looks for lines that /v do not contain /L the literal (whatever's in the quotes)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you, this works for me, but I am wondering how to use regular expression, I've tried something like ^|findstr /V /R [0-9]* , does it work? – pm1359 Dec 07 '17 at 15:51
  • Evidently it does not do what you expected it to do. `findstr` implements a small subset of `regex` - see `findstr /?` from the prompt for documentation. Essentially, literal characters+[range]+.+* and a few others. Your expression *should* exclude (because of the `/v`) any fully-numeric string, which doesn't fit the pattern you nominated in your question. – Magoo Dec 07 '17 at 15:57
  • Ah, Ok, so it means I have to write a pattern like [a-zA-Z._]*[0-9]*, right? – pm1359 Dec 07 '17 at 16:00
  • @MaryamPashmi: one should think so, [but...](https://stackoverflow.com/q/8844868/2152082) – Stephan Dec 07 '17 at 16:05
  • 1
    A guarded yes. You can use `/i` to make the search case-insensitive so instead of using `a-zA-Z`, either `a-z` or `A-Z` will match. `[` as a literal would need to be escaped (`\[` I believe). You can use `/b` and `/e` to mean `beginning` and `end` (`^` and `$` also work) and `/x` to mean exact match (= `/b/e`). Really, it's a matter of experimentation - just don't expect too much, like `{}` – Magoo Dec 07 '17 at 16:09
  • Feel free if you want to add it in response, it would be appreciated. Thank you for your answer. – pm1359 Dec 07 '17 at 16:12
  • 1
    @MaryamPashmi, `findstr /V /R [0-9]*` does not work because you are asking for zero or more occurrences of the previous character. Remove the Asterisk and it will find any file without a number in it. If you want it to match the date and time stamp exactly then you need to specify 12 number ranges. If you need it to match at least one number and any amount of numbers after that you can use `findstr /V /R [0-9][0-9]*` – Squashman Dec 07 '17 at 16:30