1

Have a dos command to look for a specific file in a directory. If its there then it moves and renames it with a datetime stamp to another directory else exit. Works great if it is executed between 10 am to 11:59 pm. But it leaves a space when it runs between 12 am - 9:59 am. See attached snippet. Not sure how to add a zero '0' where are they are spaces when it runs between 12 to 10 am. Appreciate the help and thank you in advance.

if exist "\serverdrive\in\File.txt" (move /Y "\serverdrive\in\File.txt" "\anotherdrive\out\OutFile-%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~0,2%%time:~3,2%%time:~6,2%.txt") else (exit 0)

OUTPUT that I am looking for is "OutFile-20200831162153.txt"

OutFile

  • Does this answer your question? [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) or [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351/time-is-set-incorrectly-after-midnight). – Mofi Aug 31 '20 at 05:01
  • Obviously, it's the resulting syntax error that ruins your day, not actually the space itself. Proper quoting cures that: `if exist "\\serverdrive\in\File.txt" (move /Y "\\serverdrive\in\File.txt" "\\anotherdrive\out\OutFile-%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~0,2%%time:~3,2%%time:~6,2%.txt") else (exit 0)`. Although I agree with Mofi: better use a method independent of local settings – Stephan Aug 31 '20 at 16:07
  • @Mofi..... Sorry, I am new at this so not sure how to edit/parse the time to avoid any empty spaces or zeros. Looked at the links but it all still seems a bit confusing. – user14182813 Aug 31 '20 at 19:08
  • @Stephan... Will update the command as you stated and see what happens. But the same syntax works fine after 10 am with no issues so not sure if that is the issue. – user14182813 Aug 31 '20 at 20:20
  • The space before 10am results in `move` having three parameters, which leads to the syntax error: It's `move alpha beta gamma` (three parameters) vs. `move "alpha" "beta gamma" (two parameters). I'm confident, you'll see it working as intended. – Stephan Aug 31 '20 at 20:27
  • It looks like my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/a/60126994/3074564) was too long for you and so you decided not really reading it carefully from top to bottom to get a command line which uses a country independent date/time string. Okay, here is the command line to use in a __batch file__ for your task: `if exist "\\serverdrive\in\File.txt" for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do move /Y "\\serverdrive\in\File.txt" "\\anotherdrive\out\OutFile-%%I%%J%%K%%L%%M%%N.txt" & goto :EOF` – Mofi Sep 01 '20 at 05:37
  • The command `goto :EOF` results in exiting the __FOR__ loop and the batch file processing after processing the first line with date/time output by __ROBOCOPY__ in a country independent format on which the hour has always two digits with first digit being `0` on hour is less than `10`. `:EOF` can be replaced also by a different label like `FileMoved` with `:FileMoved` being the next line in batch file after the long line with __IF__ condition, __FOR__ loop, __MOVE__ and __GOTO__ to exit loop after processing the first line of output of __ROBOCOPY__. – Mofi Sep 01 '20 at 05:43
  • @Stephan.... Its not the command that is the issue. Tried it as you suggested but it still failed due to the empty space in time at the beginning. Ran it at 7 am and it failed but completed successfully when ran at 10 am. Only way to resolve this is take out any empty spaces in time. This will resolve the issue. – user14182813 Sep 01 '20 at 14:16
  • @Stephan... You are correct about the syntax error. Enclosed the command as you suggested and it worked when I ran it at 7:55 am. THANKS. But it still gives me a space where there should be a zero, "MY-OUT-FILE-20200902 75553.txt" How do you add the zeros where there are spaces? – user14182813 Sep 02 '20 at 21:32
  • if you don't want to use a localization-independent method (as preferred and already suggested. There are a lot of possible formats of `%date%`. It's a bit better with `%time%`, but there are at least 12h vs 24h formats), replace the space with a zero with `set "Mytime=%time: =0%"` and use `%Mytime%` to build your string. – Stephan Sep 03 '20 at 13:29
  • @Mofi...thanks for the .bat file. Will try it out. Thanks. – user14182813 Sep 09 '20 at 17:11

0 Answers0