|
ngen
初级用户
 
积分 38
发帖 8
注册 2005-9-12
状态 离线
|
『楼 主』:
Needs help in Files Backup Batch file
使用 LLM 解释/回答一下
How to create a batch file that appeals the copy file from original destination to copied destination, by using for, set, if and xcopy??? the file is drag from one directory to the batch file where the new directory located
### Step 1: Understand the requirements
We need to create a batch file that can copy files from the original destination to the copied destination when a file is dragged onto the batch file, which is located in the new directory. We'll use `for`, `set`, `if`, and `xcopy` commands.
### Step 2: Write the batch file code
Here is a sample batch file code:
```batch
@echo off
setlocal enabledelayedexpansion
rem Get the path of the batch file's directory
for %%i in (%0) do set "batch_dir=%%~dpi"
rem Get the files dragged onto the batch file
set "source_files="
for %%a in (%*) do (
set "source_files=!source_files! "%%a""
)
rem The destination directory is the batch file's directory
set "dest_dir=%batch_dir%"
rem Copy the files using xcopy
for %%f in (%source_files%) do (
xcopy "%%f" "%dest_dir%" /i /y
)
endlocal
```
Explanation:
- `@echo off` turns off the display of commands in the batch file.
- `setlocal enabledelayedexpansion` is used to enable delayed expansion for variables within loops.
- `for %%i in (%0) do set "batch_dir=%%~dpi"` gets the directory path where the batch file is located.
- The next `for` loop collects the files that are dragged onto the batch file.
- Then we set the destination directory as the batch file's directory.
- Finally, another `for` loop uses `xcopy` to copy each dragged file to the destination directory. The `/i` option is used to treat the destination as a directory if it doesn't exist, and `/y` suppresses the prompt to confirm overwriting files.
Please note that you can save the above code with a `.bat` extension (e.g., `copy_files.bat`) and then drag files onto it when it's in the target directory.
|
|
2005-9-13 11:03 |
|
|
ngen
初级用户
 
积分 38
发帖 8
注册 2005-9-12
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
If a directory is supplied in a list of input parameters, the directory itself must be copied including all the files and subdirectories from that directory preserving their absolute paths. To supply the list of input parameters as selected files and directories, use mouse drag and drop action as depicted in figure below:
After the drag and drop action, the drive Q: directory structure with copied files and selected subdirectories should look as (in the figure below):
Your output should look similar to the lines in SAMPLE Screen Display (see word-wrapped listing below)
NB: Selected directories must be displayed before copying their content.
SAMPLE Screen Display:
DIRECTORY: ver_02
E:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd
E:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd
2 File(s) copied
E:\Current Work\tmpITHW\2004_S2\bkp.bat -> Q:\Current Work\tmpITHW\2004_S2\bkp.bat
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd -> Q:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url -> Q:
\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\M-bkp.cmd
1 File(s) copied
Press any key to continue . . .
如果在输入参数列表中提供了一个目录,那么该目录本身必须被复制,包括该目录中的所有文件和子目录,并保留它们的绝对路径。要将输入参数列表作为选定的文件和目录提供,请使用如下所示的鼠标拖放操作:
在拖放操作之后,带有复制文件和选定子目录的驱动器Q:的目录结构应如(在下图中)所示:
注意:选定的目录必须在复制其内容之前显示。
示例屏幕显示:
目录:ver_02
E:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd
E:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd
已复制2个文件
E:\Current Work\tmpITHW\2004_S2\bkp.bat -> Q:\Current Work\tmpITHW\2004_S2\bkp.bat
已复制1个文件
E:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd -> Q:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd
已复制1个文件
E:\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url -> Q:
\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url
已复制1个文件
E:\Current Work\tmpITHW\2004_S2\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd
已复制1个文件
按任意键继续 . . .If a directory is supplied in a list of input parameters, the directory itself must be copied including all the files and subdirectories from that directory preserving their absolute paths. To supply the list of input parameters as selected files and directories, use mouse drag and drop action as depicted in figure below:
After the drag and drop action, the drive Q: directory structure with copied files and selected subdirectories should look as (in the figure below):
Your output should look similar to the lines in SAMPLE Screen Display (see word-wrapped listing below)
NB: Selected directories must be displayed before copying their content.
SAMPLE Screen Display:
DIRECTORY: ver_02
E:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd
E:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\t.cmd
2 File(s) copied
E:\Current Work\tmpITHW\2004_S2\bkp.bat -> Q:\Current Work\tmpITHW\2004_S2\bkp.bat
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd -> Q:\Current Work\tmpITHW\2004_S2\Drv_Q.cmd
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url -> Q:
\Current Work\tmpITHW\2004_S2\Rob van der Woude's Scripting Pages Batch Files, Rexx, KiXtart, Perl, VBScript.url
1 File(s) copied
E:\Current Work\tmpITHW\2004_S2\M-bkp.cmd -> Q:\Current Work\tmpITHW\2004_S2\ver_02\M-bkp.cmd
1 File(s) copied
Press any key to continue . . .
|
|
2005-9-14 08:07 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
I don't think batch can support drag and drop action. forget it!
I don't think batch can support drag and drop action. forget it!
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2005-9-14 08:32 |
|
|
ngen
初级用户
 
积分 38
发帖 8
注册 2005-9-12
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Well...
Actually when you drag the file from point A, to point B. You put the dragged file on the batch file which is already in point B. The batch file will shows you the original directory of the file to the new directory of the file. Eg: "DIRECTORY: ver_02" on the top
嗯……实际上当你把文件从A点拖到B点时,你把被拖动的文件放到已经在B点的批处理文件上,批处理文件会向你显示文件的原始目录到文件的新目录。例如:顶部显示“DIRECTORY: ver_02”
Well...
Actually when you drag the file from point A to point B. You put the dragged file on the batch file which is already in point B. The batch file will show you the original directory of the file to the new directory of the file. Eg: "DIRECTORY: ver_02" on the top
|
|
2005-9-14 08:46 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Re ngen:
你的意思是说,在批处理运行过程中,将指定文件拖动到批处理所在的命令行窗口中,然后批处理继续运行,将此文件拷贝到批处理所在路径,并显示文件拷贝前后的完整路径吗?
若是如此,则难点在于如何在拖动文件至命令行窗口后,激活批处理继续执行,使用 set /a 接受文件路径,需要按回车来确认,相信不是你所想看到的;而使用自动探测命令行缓冲区的程序(比如choice),则难以接受字符串。如果可以解决这个问题,那么我以上的问题假设就不会很难了。
Re ngen:
Do you mean that during the batch processing run, you drag the specified file to the command line window where the batch is located, and then the batch continues to run, copies this file to the path where the batch is located, and displays the complete paths before and after the file copy?
If so, the difficulty lies in how to activate the batch to continue execution after dragging the file to the command line window. Using set /a to accept the file path requires pressing Enter to confirm, which I believe is not what you want to see; while using a program that automatically detects the command line buffer (such as choice) is difficult to accept strings. If this problem can be solved, then the above problem assumption will not be difficult.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-9-14 20:36 |
|
|
ngen
初级用户
 
积分 38
发帖 8
注册 2005-9-12
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
yep, but still don't really know how 2 do it... because my lecturer didn't talk much about it.
I will send you an email, can you help me have a look please
yep, but still don't really know how to do it... because my lecturer didn't talk much about it.
I will send you an email, can you help me have a><<
|
|
2005-9-15 07:22 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Re ngen:
当然,你可以给我 Email ,地址是 <id>@163.com 或者 <id>@gmail.com ,请最好使用中文。
Re ngen:
Of course, you can send me an email. The addresses are <id>@163.com or <id>@gmail.com. Please use Chinese if possible.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-9-15 22:32 |
|
|
fdsiuha
高级用户
    闷
积分 587
发帖 302
注册 2005-7-25
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
No,u perhaps misunderstood him.
he want to execute a batch file by draging it from windows of Explorer to the icon of the batch file.
just like drag a TXT file to Notepad.exe.
the batch will copy it to the current directory where the bacth file exist
(why should a batch file do such a stupid task?the only thing u sould do is press the CTRL when drag the file form window to window)
if dest path was designated,the batch file should get the whole source path,replace the drive,pass to the xcopy to built the dest directory.
i haven't seen his picture
Last edited by fdsiuha on 2005-9-17 at 11:31 ]
不,你可能误解他了。
他想通过从资源管理器窗口将批处理文件拖到批处理文件的图标上来执行它。
就像将TXT文件拖到记事本.exe上一样。
批处理会将其复制到批处理文件所在的当前目录
(为什么批处理文件要做这样愚蠢的任务?你唯一应该做的是在从一个窗口向另一个窗口拖文件时按住Ctrl键)
如果指定了目标路径,批处理文件应该获取整个源路径,替换驱动器,传递给xcopy来构建目标目录。
我没见过他的照片
Last edited by fdsiuha on 2005-9-17 at 11:31 ]
|

欢迎造访DOS的小屋!
http://risky.ik8.com |
|
2005-9-17 11:23 |
|
|
ngen
初级用户
 
积分 38
发帖 8
注册 2005-9-12
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
It just my lecturer requested to do it
can any1 help it???
It just my lecturer requested to do it
can any1 help it???
|
|
2005-9-17 20:49 |
|
|
born2be
中级用户
  
积分 220
发帖 82
注册 2005-9-3
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
You all misunderstood him.
He meant that the taget directories and files are to be dragged and dropped by a batch processing file whose directory is the destination directory. The drag and drop is not the WINDOWS conception. You are required to fulfil the task by only using for, set, if and xcopy.
But NGEN did not define his problem very clearly.
You all misunderstood him.
He meant that the taget directories and files are to be dragged and dropped by a batch processing file whose directory is the destination directory. The drag and drop is not the WINDOWS conception. You are required to fulfil the task by only using for, set, if and xcopy.
But NGEN did not define his problem very clearly.
|
|
2005-9-18 14:38 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 11 楼』:
:
使用 LLM 解释/回答一下
Re All:
这就是语言在交流中产生的障碍了,因为各自的语言基础和文化背景不同,误解是难免的。好在我已通过邮件基本了解了 ngen 的意图。大致如下:
从源路径(比如E:\Current Work\tmpITHW\2004_S2)向目标路径(比如Q:\)拷贝一个或多个文件(夹),并且要保留拷贝文件的从根目录起的目录结构,拷贝方式为用鼠标选中文件(夹)后拖放。但是因为直接拖放到目标路径会丢失源文件(夹)的目录结构,而直接拖放目录树,又会多拷贝一些冗余的没有选择的文件(夹)。所以决定将源文件(夹)拖放到目标路径中的一个批处理(比如M_bkup.cmd)中,将它们作为一个或多个命令行参数传递给批处理,通过 for/set/if 等命令分析这些参数,将它们所引用的文件(夹)通过 xcopy 拷贝到它所在的路径下(即Q:\),同时保留其目录结构。
这确实是一个比较新颖的程序构思。目前,我已基本完成代码的编写,并交由 ngen 查阅,待 ngen 确定后,将以回复贴出全部代码。
Last edited by willsort on 2005-9-18 at 15:20 ]
Re All:
This is the barrier caused by language in communication, because of different language foundations and cultural backgrounds, misunderstandings are inevitable. Fortunately, I have basically understood the intention of ngen through emails.大致如下:
Copy one or more files/folders from the source path (such as E:\Current Work\tmpITHW\2004_S2) to the target path (such as Q:\), and keep the directory structure starting from the root directory of the copied files. The copying method is to select the files/folders with the mouse and drag and drop. But directly dragging and dropping to the target path will lose the directory structure of the source files/folders, and directly dragging and dropping the directory tree will copy some redundant unselected files/folders more. So it is decided to drag the source files/folders to a batch file (such as M_bkup.cmd) in the target path, pass them as one or more command line parameters to the batch file, analyze these parameters through commands such as for/set/if, and copy the files/folders they refer to to the path where it is located (that is, Q:\) through xcopy, while keeping its directory structure.
This is indeed a relatively novel program concept. At present, I have basically completed the code writing and handed it over to ngen for review. After ngen confirms it, the full code will be posted in the reply.
Last edited by willsort on 2005-9-18 at 15:20 ]
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-9-18 15:13 |
|
|
fdsiuha
高级用户
    闷
积分 587
发帖 302
注册 2005-7-25
状态 离线
|
|
2005-9-19 18:03 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Re fdsiuha:
你可以在测试目录创建一个批处理程序 test.bat ,其内容如下:
@echo off
echo copy file %1...
pause
然后,用鼠标任意选取一个文件,并拖动到此批处理程序的图标上放开。此时,就会发现 test.bat 被执行,显示选取文件的路径。
ngen 要求这个 test.bat 可以拷贝这个选中文件到其所在目录,并且保留它在源盘的目录结构。这与某些文件备份程序设计需求相类似。
Re fdsiuha:
You can create a batch program test.bat in the test directory with the following content:
@echo off
echo copy file %1...
pause
Then, arbitrarily select a file with the mouse and drag it to the icon of this batch program and release it. At this time, you will find that test.bat is executed, and the path of the selected file is displayed.
ngen requires that this test.bat can copy the selected file to its directory and retain the directory structure on the source disk. This is similar to the design requirements of some file backup programs.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-9-19 18:26 |
|
|
fdsiuha
高级用户
    闷
积分 587
发帖 302
注册 2005-7-25
状态 离线
|
|
2005-9-22 00:01 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Re All:
因为 Ngen 兄以后没有反馈任何意见,我就将这个主题忘在脑后了。近日,在编写另一个文件名处理的程序时,遇到了一些问题,因此连带想起了这个程序,再次作了更新,代码如下。遗憾的是,它现在还无法处理选择拖放隐藏文件或文件夹的情况,这是for的缺憾,if exist虽然可以识别隐藏,但是却无法用于严谨的目录判定。不知道各位还有什么办法?
:: M-bkup.cmd - Backup files and directories preserving their full pathnames by Drag&Drop
:: Will Sort - 2005-09-18 - CMD@WinXP
:: Wrote according to request of Michael Ngen
:: Link: Chine DOS Union Forum - http://www.cn-dos.net/forum/viewthread.php?tid=16947
:: Update: 2005-10-26
@echo off & setlocal EnableExtensions
if not '%1'=='' goto GetPath
:Help
echo NO ACTION TAKEN
echo Usage: Drag and drop files or directories over this file.
echo.
goto End
:GetPath
for %%p in (%0) do set _base=%%~dpp
:Backup
for %%p in (%1.*) do echo File: %%~nxp && xcopy /d /f /g /h /k /r %1 %_base%%%~pp
for /d %%p in (%1.*) do echo Directory: %%~nxp && xcopy /d /e /f /g /h /i /k /r %1 "%_base%%%~pnxp"
if errorlevel 2 goto End
shift
if not '%1'=='' goto Backup
:End
pause
Last edited by willsort on 2005-10-26 at 12:58 ]
Re All:
Because Brother Ngen didn't give any feedback later, I forgot about this topic. Recently, when writing another program for file name processing, I encountered some problems, so I remembered this program again and updated it again. The code is as follows. Unfortunately, it still can't handle the situation of selecting and dragging hidden files or folders. This is a shortcoming of for. Although if exist can identify hidden, it can't be used for strict directory judgment. I wonder if you all have any other ways?
:: M-bkup.cmd - Backup files and directories preserving their full pathnames by Drag&Drop
:: Will Sort - 2005-09-18 - CMD@WinXP
:: Wrote according to request of Michael Ngen
:: Link: Chine DOS Union Forum - http://www.cn-dos.net/forum/viewthread.php?tid=16947
:: Update: 2005-10-26
@echo off & setlocal EnableExtensions
if not '%1'=='' goto GetPath
:Help
echo NO ACTION TAKEN
echo Usage: Drag and drop files or directories over this file.
echo.
goto End
:GetPath
for %%p in (%0) do set _base=%%~dpp
:Backup
for %%p in (%1.*) do echo File: %%~nxp && xcopy /d /f /g /h /k /r %1 %_base%%%~pp
for /d %%p in (%1.*) do echo Directory: %%~nxp && xcopy /d /e /f /g /h /i /k /r %1 "%_base%%%~pnxp"
if errorlevel 2 goto End
shift
if not '%1'=='' goto Backup
:End
pause
Last edited by willsort on 2005-10-26 at 12:58 ]
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-10-26 12:47 |
|