![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 15:27 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » 在文件备份批处理文件方面需要帮助 |
| Printable Version 1,894 / 14 |
| Floor1 ngen | Posted 2005-09-13 11:03 |
| 初级用户 Posts 8 Credits 38 | |
|
### 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. |
|
| Floor2 ngen | Posted 2005-09-14 08:07 |
| 初级用户 Posts 8 Credits 38 | |
|
如果在输入参数列表中提供了一个目录,那么该目录本身必须被复制,包括该目录中的所有文件和子目录,并保留它们的绝对路径。要将输入参数列表作为选定的文件和目录提供,请使用如下所示的鼠标拖放操作:
在拖放操作之后,带有复制文件和选定子目录的驱动器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 . . . |
|
| Floor3 Climbing | Posted 2005-09-14 08:32 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
I don't think batch can support drag and drop action. forget it!
|
|
| Floor4 ngen | Posted 2005-09-14 08:46 |
| 初级用户 Posts 8 Credits 38 | |
|
嗯……实际上当你把文件从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 |
|
| Floor5 willsort | Posted 2005-09-14 20:36 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor6 ngen | Posted 2005-09-15 07:22 |
| 初级用户 Posts 8 Credits 38 | |
|
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><<[censha 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 look please |
|
| Floor7 willsort | Posted 2005-09-15 22:32 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor8 fdsiuha | Posted 2005-09-17 11:23 |
| 高级用户 Posts 302 Credits 587 | |
|
不,你可能误解他了。
他想通过从资源管理器窗口将批处理文件拖到批处理文件的图标上来执行它。 就像将TXT文件拖到记事本.exe上一样。 批处理会将其复制到批处理文件所在的当前目录 (为什么批处理文件要做这样愚蠢的任务?你唯一应该做的是在从一个窗口向另一个窗口拖文件时按住Ctrl键) 如果指定了目标路径,批处理文件应该获取整个源路径,替换驱动器,传递给xcopy来构建目标目录。 我没见过他的照片 [ Last edited by fdsiuha on 2005-9-17 at 11:31 ] |
|
| Floor9 ngen | Posted 2005-09-17 20:49 |
| 初级用户 Posts 8 Credits 38 | |
|
It just my lecturer requested to do it
can any1 help it??? |
|
| Floor10 born2be | Posted 2005-09-18 14:38 |
| 中级用户 Posts 82 Credits 220 | |
|
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. |
|
| Floor11 willsort | Posted 2005-09-18 15:13 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 ] |
|
| Floor12 fdsiuha | Posted 2005-09-19 18:03 |
| 高级用户 Posts 302 Credits 587 | |
|
Oh, I'm still confused.
|
|
| Floor13 willsort | Posted 2005-09-19 18:26 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re fdsiuha:
You can create a batch program test.bat in the test directory with the following content: 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. |
|
| Floor14 fdsiuha | Posted 2005-09-22 00:01 |
| 高级用户 Posts 302 Credits 587 | |
|
Then my previous understanding was correct! Dizzying...
|
|
| Floor15 willsort | Posted 2005-10-26 12:47 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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? [ Last edited by willsort on 2005-10-26 at 12:58 ] |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |