简体   繁体   中英

Windows XP Batch IF & XCOPY

I've been struggling with trying to get the below (example) batch file to work on Windows XP SP3. The IF NOT EXIST part seems to work, but I keep receiving the "Does <path\\filename.ext> specify a file name or directory name on the target" message despite using the /I flag on XCOPY :

    @IF NOT EXIST "\\SERVER\PATH\TO\FILE DIR" (
      MKDIR "\\SERVER\PATH\TO\FILE DIR"
      XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
    ) ELSE (
      XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
    )

My understanding is that with the /I switch, XCOPY should create the directory structure if it doesn't exist - at least it does when I don't specify a file name. Unfortunately for the requirements of this project, I must specify a file name and cannot keep the original as it's a template file that gets manipulated with an automated process every day.

So, I tried to get around the issue with XCOPY and the directory path not existing by checking for the existence of the path, and if it's not there, creating it with the MKDIR command and then copying the file - but XCOPY still prompts as to whether the destination is a file or directory, which doesn't make sense but maybe I'm missing something.

Just to be clear, this is on Windows XP SP3.

Any ideas?

You might find it easier to do something like this:

md "\\SERVER\PATH\TO\FILE DIR" 2>NUL
copy "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"

The initial 'md' will attempt to create the directory. If it already exists, it will output an error message to STDERR. The 2>NUL redirects that to Windows' built-in "null device", which is to say, it just swallows the error message. Assuming you have the appropriate permissions, you can be sure that this directory exists now.

The copy command just copies your file. No need to use xcopy to copy a single file - that's both overkill and fraught with little gotchas like being prompted whether it's a file or directory.

Since the destination file doesn't exist before you copy, xcopy isn't sure if it needs to create a new directory called "FILE FINAL.XLSM", and put the file in there. By the way, since you already create the destination dir, you don't need the /I on your xcopy. Here are a couple ways to do what you want:

  1. echo F | xcopy .... (feed the "F" answer to xcopy)
  2. copy .... (you don't need to use xcopy for a single file)
 echo f|XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"

should copy the file AND create the directory. No idea why the option to specify "this is a file" wasn't made available, but RTFM - the /i switch is only effective if you are copying MORE than one file, and specifying \\ as the last character of the destination name tells XCOPY that the target is a directoryname in any case, so /i sems redundant.

However, be careful if you follow the copy route. It's better in general to use copy /b because plain copy may fail to properly copy some filetypes (like .MPGs) - it may stop on the first ^Z. copy /b appears safe however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM