0

I have to use batch only . The destination folder should have the copied file along with the directory structure of the source directory (for the file copied only). Example :

Source: C:\folder1\folder2\folder3\text1.txt Destination: C:\backup

After the command is executed The destination folder should look like : C:\backup\folder1\folder2\folder3\text1.txt

I must use the command from C:\ (root directory) only . In my source there are multiple files with name ="text1.text" ,but with different folder structure. I want only that "text1.txt" to be copied whose path i am providing (source), and not all files named "text1.txt" [this can be achieved using--- xcopy Source\text1.txt* Destination /S /Y].PLease help.

4

1 回答 1

1
@ECHO OFF
SETLOCAL
:: This command alone will accomplish the task using ONLY XCOPY
:: BUT I'm changing the directorynames to suit my system
::
XCOPY c:\sourcedir\a\b\text1.txt u:\backup\sourcedir\a\b\
::
:: Or leave the last XCOPY out and this batch will do the same
:: if you supply the parameter "c:\sourcedir\a\b\text1.txt"
::
:: IE. at the prompt, enter
::
:: thisbatchname "c:\sourcedir\a\b\text1.txt"
::
:: (where the quotes are optional UNLESS the parameter contains 
::  a special character or a space"
::
XCOPY "%~1" "u:\backup2%~p1"

第一个 XCOPY 应该很明显。

第二个通过使用%~1which 是第一个参数,减去封闭引号(如果有) - 然后重新引用以确保生成的字符串被解析为单个字符串。

XCOPY 字符串的第二个参数U:\BACKUP2加上%~p1-p参数 1 的 - PATH,然后引用整个内容。

因此,"c:\sourcedir\a\b\text1.txt"作为参数执行的命令将是

xcopy "c:\sourcedir\a\b\text1.txt" "u:backup2\sourcedir\a\b\"

根据需要创建目标树。

于 2013-04-18T10:06:20.643 回答