1

Where the text file contains a list of files, as below code, but it errors saying the %%a is an invalid parameter even though it correctly sees %%a as the file name.

Any way around this?

EDIT:

for /f "delims=" %%a in (C:\audit\test.txt) do (
  robocopy "%%~dpa" "Z:" "%%~nxa" /S /E /COPY:DAT /PURGE /MIR /R:1000000 /W:30
)
pause
PnP
  • 3,133
  • 17
  • 62
  • 95

1 Answers1

1

A file name argument of ROBOCOPY cannot include the path, whether absolute or relative. It should be just a name (with the extension). (Alternatively it can be a mask.)

If your text file contains full paths, you can extract names and extensions only using the ~nx combined modifier:

for /f "delims=" %%a in (C:\audit\test.txt) do (
  robocopy "C:\Test1" "C:\Test2" "%%~nxa" /S /E /COPY:DAT /PURGE /MIR /R:1000000 /W:30
)

Also, consider enclosing all your path/file names in double quotes, as you can see above, to avoid having issues with names containing spaces and/or special characters.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • Can I remove file names from a fully qualified path? i.e. I have C:\Folder A\test.txt in the text file, and I want to end up with just C:\Folder A\ for the source, and Test.txt for the file (where %%~nxa does now) – PnP Jan 27 '14 at 17:54
  • `%%~dpa` will extract the drive and path, without name and extension. You can read more about these modifiers in the `for` command's built-in help (`for /?`, they are listed nearer the end). – Andriy M Jan 27 '14 at 17:57
  • I thought that, however, see my edit, it does not seem to work. – PnP Jan 27 '14 at 17:58
  • Seems to do with the trailing backslash. Please post a new question as that is an entirely different problem from the one you asked originally. – Andriy M Jan 27 '14 at 18:07
  • See: http://stackoverflow.com/questions/21388400/batch-for-loop-removing-file-from-folder-names – PnP Jan 27 '14 at 18:11