1

I currently have this script I run as a scheduled task:

@echo on
START /WAIT c:\windows\system32\Robocopy.exe "D:\folder" "\\192.168.2.87\folder" /E /MT:20 /R:50 /W:10 /V /ETA /LOG:c:\robocopy.txt

I want to convert and run this as a PowerShell script because of two reasons:

  • Its more modern
  • The most important reason is that I want to store the log with date and time as C:\robocopylog3004201510214043.txt and I am literally finding no information on how to strip characters like ":" and "/" from a batch script and I know PowerShell can.

That last number is not random. It is the date and time.

Date for example is "30/04/2015" (striping it would leave 30042015) Time for example is "10:21:40,43" (striping it would leave 10214043)

So it would leave a file name of robocopylog3004201510214043.txt

1 Answer 1

3

There is little difference between CMD and PowerShell when it comes to running external programs. I'd recommend using the call operator (&), though, even if it isn't mandatory in this particular case.

& c:\windows\system32\Robocopy.exe "D:\folder" "\\192.168.2.87\folder" /E /MT:20 /R:50 /W:10 /V /ETA /LOG:c:\robocopy$(Get-Date -format 'ddMMyyyyHHmmss').txt

robocopy runs synchronously anyway, so no "wait" instruction required.

The number format ddMMyyyyHHmmss produces a timestamp consisting of day, month, year, hour, minutes and seconds. I wouldn't recommend to include milliseconds as well, because you probably won't run robocopy several times within the same second. If you must include milliseconds, append ff to the format string (or f, fff, etc., depending on the number of digits you need). You may want to consider using an ISO date format (yyyyMMddHHmmss), though, because that simplifies listing the log files in creation order.

As for replacing characters in batch scripts, that can be done via string replacements:

C:\>echo %DATE:/=%
30042015

C:\>echo %TIME::=_%
10_01_22.39
Sign up to request clarification or add additional context in comments.

5 Comments

Currently, the batch script is returning a date of "30/04/2015" and "10:21:40,43". So I want to remove the "/", the ":" and the ",". It would be as 3004201510214043. Ill add this to the original question.
& c:\windows\system32\Robocopy.exe "D:\folder" "\\192.168.2.87\folder" /E /MT:20 /R:50 /W:10 /V /ETA /LOG:c:\robocopy$(Get-Date -format 'ddMMyyyHHmmssfff').txt
What exactly is the "&" for?
Like I said: it's the call operator. It allows you to run executables in double quotes (& "C:\path\to\some\executable"). Without the call operator PowerShell would consider a path between quotes a string and simply echo it to the default output stream. It's not required in this particular case, because the path to your executable isn't in quotes.
Yeah, i actually went for that format instead of the one I originally commented as it seemed clearer. I include MS because I do indeed do several robocopies on the same second.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.