0

I create a directory using MD. Once you plug in the USB drive, you select it and it create the directory to the drive, however i am trying to copy files from the source to the new directory on usb. I have tried so many things but nothing seems to work. Here is the .bat file

@echo OFF


diskpart

set source=C:\Users\Public\Documents

set DESTINATION= %computername% %DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%/

@echo DESTINATION =%DESTINATION%

:: Create new directory
md "%1\%DESTINATION%"

xcopy %source% %DESTINATION% /y

It creates the directory but it doesnt copy the files from the source to the newly created directory. How can i do that? How do you pass an argument? This the first time i am writting a script. THanks!

Jeff2013
  • 1
  • 1
  • 2

2 Answers2

0

There are several issues here.

  1. You are creating the destination directory as %1\%DESTINATION% but in XCOPY you've specified just %DESTINATION%. It is likely that %1\ is not the current directory at the time of copying, which must explain why the intended target directory doesn't receive any files. Therefore, you should add %1\ in front %DESTINATION% in XCOPY.

  2. The destination directory name, as set by the SET DESTINATION command, contains spaces. That means that the name must be enclosed in double quotes in most file operation contexts. By "most" I mean that there are some commands that do not require the names to be quoted, although I would still not recommend omitting the quotes. And XCOPY is not one of those commands anyway, so, your target path should actually be "%1\%DESTINATION%", just like in the MD command.

  3. Possibly not an issue but I'm not sure. You've got a space just after = in SET DESTINATION command. That space actually becomes part of the value. You'll probably want to remove it unless it was your intention that your destination directory name start with a space character.

  4. Similarly half-issue, the trailing / at the end of the name is unnecessary. It may not be an issue in this particular script, but in general you'd have to be more careful using such a path as part of a more complex path, particularly when the name goes at the beginning or in the middle of the more complex path. It'd be less troublesome to use \ instead or just omit the trailing separator all together.

Finally, you could possibly omit the MD command and let XCOPY create the target directory for you if it didn't exist. Add the /I switch to XCOPY to avoid the request about whether the target is a file or a directory.

Only note that if the source directory is totally empty, the target won't be created. To ensure it was created, you'd need to add /E also. However, that would also imply copying the entire directory structure, including empty subdirectories, if any, as well. If that doesn't agree with your intention, stick with your current MD + XCOPY method.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
0

try this code, it worked for me. It did copy all the files in source folder. Just assign the source and dest variables with appropriate values.

@ECHO OFF
setlocal enabledelayedexpansion
set SOURCE="C:\Users\Public\Documents"
SET DEST="%computername% %DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%"

echo %SOURCE% %DEST%

IF NOT EXIST %DEST% mkdir %DEST%

xcopy %SOURCE%\*.* %DEST%\*.* /Y