0

i have a routine that sends generic .txt files named "orders.txt" every hour. as i receive them, i want to run a batch file to rename the file to: orders_DDMMYYYYMMSS_XXX.txt where XXX would be the total number of rows contained within the originating file

so far i have figured how to add the date/time stamp, but not the row count.

thanks in advance!

ren *.txt Completed_Orders_%date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%.txt
Carlo B.
  • 113
  • 1
  • 13
  • 4
    `find /c /v ""` gets you the line count. Put a `for /f` loop around to get it into a variable (see `for /?`). – Stephan Jun 17 '19 at 19:57
  • I'd advise, if you're relatively satisfied with limited user settings, changing `%date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%` to `%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%%TIME:~0,2%%TIME:~3,2%` instead. This would increase decrease the number of users not having settings to achieve `DDMMYYYYMMSS`, because some settings do not prefix `Day` to their `%DATE%`. – Compo Jun 17 '19 at 21:02
  • thanks guys. i added the loop and it seems to be working. – Carlo B. Jun 18 '19 at 12:57

2 Answers2

0

i did this:

set file=*.txt
set /a cnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a cnt=%%a
ren *.txt "Completed_Orders_%date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%_%cnt%.txt"
Stephan
  • 47,723
  • 10
  • 50
  • 81
Carlo B.
  • 113
  • 1
  • 13
  • that's fine when you are sure, there is just one `.txt` file (as you seem to be). Be aware this depends on your local settings, so it might not work on another computer (especially you probably will get a space in the date/time string before `10:00`, so I quoted your destination file name to take care of that space - although I`d prefer a [language independent solution](https://stackoverflow.com/a/18024049/2152082)). Be sure to set the working folder when you schedule it to run each hour or it won't find your file(s). – Stephan Jun 18 '19 at 13:34
  • thanks for the feedback. if there are more than one .txt files in the folder, it doesn't seem to work as you have indicated. how would i alter this to deal with multiple .txt files in the folder? – Carlo B. Jun 18 '19 at 14:06
0

This is how I would do it:

@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set "datetime=%%I"
set "datetime=%datetime:~0,12%"

for %%F in (*.txt) do (
  for /f %%a in ('type "%%F"^|find "" /v /c') do (
    ECHO ren "%%F" "Completed_Orders_%%~nF_%datetime%_%%a.new
  )
)

Changes:

  • another way to get the date/time string without being dependent on locale settings
  • a surrounding for %%F loop to process each *.txt file
  • keeping the original filename (%%~nF) as part of the new name
  • another extension for the renamed files (to avoid reprocessing them again and again) It would be better to filter the to-be-renamed files at the beginning, but I don't know how they look like (instead *.txt something that matches only preprocessed files)
  • disarmed the ren command with ECHO. Remove the ECHO after troubleshooting (when you are sure, it works as intended)
Stephan
  • 47,723
  • 10
  • 50
  • 81