China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 13:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 在文件备份批处理文件方面需要帮助 View 1,748 Replies 14
Original Poster Posted 2005-09-13 11:03 ·  新西兰 奥克兰市理工大学
初级用户
Credits 38
Posts 8
Joined 2005-09-12 07:38
20-year member
UID 42442
Status Offline
### 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.
Floor 2 Posted 2005-09-14 08:07 ·  新西兰 奥克兰市理工大学
初级用户
Credits 38
Posts 8
Joined 2005-09-12 07:38
20-year member
UID 42442
Status Offline
如果在输入参数列表中提供了一个目录,那么该目录本身必须被复制,包括该目录中的所有文件和子目录,并保留它们的绝对路径。要将输入参数列表作为选定的文件和目录提供,请使用如下所示的鼠标拖放操作:

在拖放操作之后,带有复制文件和选定子目录的驱动器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 . . .
Floor 3 Posted 2005-09-14 08:32 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
I don't think batch can support drag and drop action. forget it!
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 4 Posted 2005-09-14 08:46 ·  新西兰 奥克兰市理工大学
初级用户
Credits 38
Posts 8
Joined 2005-09-12 07:38
20-year member
UID 42442
Status Offline
嗯……实际上当你把文件从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
Floor 5 Posted 2005-09-14 20:36 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-09-15 07:22 ·  新西兰 奥克兰市理工大学
初级用户
Credits 38
Posts 8
Joined 2005-09-12 07:38
20-year member
UID 42442
Status Offline
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
Floor 7 Posted 2005-09-15 22:32 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2005-09-17 11:23 ·  中国 湖北 武汉 联通
高级用户
★★★
Credits 587
Posts 302
Joined 2005-07-25 17:31
21-year member
UID 41046
Gender Male
Status Offline
不,你可能误解他了。
他想通过从资源管理器窗口将批处理文件拖到批处理文件的图标上来执行它。
就像将TXT文件拖到记事本.exe上一样。
批处理会将其复制到批处理文件所在的当前目录
(为什么批处理文件要做这样愚蠢的任务?你唯一应该做的是在从一个窗口向另一个窗口拖文件时按住Ctrl键)

如果指定了目标路径,批处理文件应该获取整个源路径,替换驱动器,传递给xcopy来构建目标目录。
我没见过他的照片

[ Last edited by fdsiuha on 2005-9-17 at 11:31 ]
欢迎造访DOS的小屋!
http://risky.ik8.com
Floor 9 Posted 2005-09-17 20:49 ·  新西兰
初级用户
Credits 38
Posts 8
Joined 2005-09-12 07:38
20-year member
UID 42442
Status Offline
It just my lecturer requested to do it
can any1 help it???
Floor 10 Posted 2005-09-18 14:38 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 220
Posts 82
Joined 2005-09-03 10:43
20-year member
UID 42197
Gender Male
Status Offline
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.
Floor 11 Posted 2005-09-18 15:13 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 12 Posted 2005-09-19 18:03 ·  中国 湖北 武汉 联通
高级用户
★★★
Credits 587
Posts 302
Joined 2005-07-25 17:31
21-year member
UID 41046
Gender Male
Status Offline
Oh, I'm still confused.
欢迎造访DOS的小屋!
http://risky.ik8.com
Floor 13 Posted 2005-09-19 18:26 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 14 Posted 2005-09-22 00:01 ·  中国 湖北 武汉 联通
高级用户
★★★
Credits 587
Posts 302
Joined 2005-07-25 17:31
21-year member
UID 41046
Gender Male
Status Offline
Then my previous understanding was correct! Dizzying...
欢迎造访DOS的小屋!
http://risky.ik8.com
Floor 15 Posted 2005-10-26 12:47 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: