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-08 04:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] Copying specified N files with relative paths of folders View 2,252 Replies 15
Original Poster Posted 2008-04-11 17:05 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
Hello everyone!

I am currently making a batch script for backing up files for operating system configuration. The desired effect is: place the batch script in any folder and run it, then it copies the files listed in the file path in the batch script to the current folder, and retains the original file path.

===============
@echo off
@eho c:\IBM\123\456\789.nsf
@eho c:\IBM\abc\cd.id
@eho c:\Lotus\abc\cd.js
:This line of code is the key, which is to COPY the above two files to the current folder, and these 2 files are copied to the directory under the current folder + original path. ???
exit
================
Example:
After running this batch script in the bak folder under the root directory of drive E, the following effect will be produced:
E:\bak\IBM\123\456\789.nsf
E:\bak\IBM\abc\cd.id
E:\bak\Lotus\abc\cd.js
After running this batch script in the load folder under the root directory of drive F, the following effect will be produced:
F:\load\IBM\123\456\789.nsf
F:\load\IBM\abc\cd.id
F:\load\Lotus\abc\cd.js


Please ask everyone how to write the batch script? Both bat or vbs are acceptable, thank you!


[ Last edited by flying008 on 2008-4-15 at 12:35 PM ]
Floor 2 Posted 2008-04-11 17:18 ·  中国 浙江 嘉兴 平湖市 电信
初级用户
★★
Credits 157
Posts 67
Joined 2007-05-13 11:03
19-year member
UID 88378
Gender Male
Status Offline
This is simple, the following line meets your requirement.

xcopy /d/r/y/e/h %cd% E:\bak\
Floor 3 Posted 2008-04-11 17:24 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
Originally posted by tvzml at 2008-4-11 17:18:
This is simple, the following line meets your requirement.

xcopy /d/r/y/e/h %cd% E:\bak\


I haven't tried the effect of this sentence yet, but what if it runs in the up folder on drive F? Hehe... The purpose is to generate the current folder path + the original path + the file.
COPY the above N files to the current folder, and these N files are copied into the directory under the current folder + the original path.
So it is inappropriate to write E:\bak\ as the current folder path in the batch processing, but still thank you for your help!


[ Last edited by flying008 on 2008-4-11 at 05:25 PM ]
Floor 4 Posted 2008-04-12 12:28 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
I just feel that such a function should be implemented with FOR plus XCOPY, but I haven't got a clue yet. Please give me some guidance, sir!
Floor 5 Posted 2008-04-12 12:40 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
@echo off
set a=c:\IBM\123\456\789.nsf&set b=c:\IBM\abc\cd.id&set c=c:\Lotus\abc\cd.js
for /f "delims=" %%i in ('cd') do copy %a% %%i%a:~2%&copy %b% %%i%b:~2%&copy %c% %%i%c:~2%

[ Last edited by zw19750516 on 2008-4-12 at 12:43 PM ]
批处理之家新域名:www.bathome.net
Floor 6 Posted 2008-04-12 15:15 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
@echo off
set a=C:\Documents and Settings\Administrator\Application Data\Microsoft\AddIns\polynimials.chm
for /f "delims=" %%i in ('cd') do copy %a% %%i%a:~2%


After testing, it can only copy files in the root directory of the disk to the current folder. Such a function can be achieved with the COPY command alone.
If it is a file in a certain folder or a file in a multi-level subfolder, no matter whether the path has spaces or not, it prompts that the specified file is not found by the system! And it cannot generate the required multi-level file directory.
The revolution is not yet successful, still please big shots give pointers! Thank you...


[ Last edited by flying008 on 2008-4-12 at 03:25 PM ]
Floor 7 Posted 2008-04-12 15:37 ·  中国 北京 华为云
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
```batch
@echo off
for /f "tokens=*" %%i in ('more +7 "%~0"') do (
echo f|xcopy /e/q "%%i" "%cd%%%~pnxi">nul
)
pause
goto :eof

c:\IBM\123\456\789.nsf
c:\IBM\abc\cd.id
c:\Lotus\abc\cd.js
```

[ Last edited by abcd on 2008-4-12 at 03:38 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flying008 +2 2008-04-14 11:12
Floor 8 Posted 2008-04-14 11:13 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
Hello, Mr. abcd!

When running this bat, an internal error is prompted. May I ask how to modify it so that this bat can be used to copy the following 3 files?
c:\IBM\123\456\789.nsf
c:\IBM\abc\cd.id
c:\Lotus\abc\cd.js


[ Last edited by flying008 on 2008-4-14 at 11:15 AM ]
Floor 9 Posted 2008-04-14 12:48 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
set a=c:\IBM&set b=c:\IBM&set c=c:\Lotus
for /f "delims=" %%i in ('cd') do xcopy /e %a% %%i%a:~2%\&xcopy /e %b% %%i%b:~2%\&xcopy /e %c% %%i%c:~2%\
```

[ Last edited by zw19750516 on 2008-4-14 at 01:18 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flying008 +2 2008-04-14 12:51
批处理之家新域名:www.bathome.net
Floor 10 Posted 2008-04-14 13:35 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-4-14 12:48:
@echo off
set a=c:\IBM&set b=c:\IBM&set c=c:\Lotus
for /f "delims=" %%i in ('cd') do xcopy /e %a% %%i%a:~2%\&xcopy /e %b% %%i%b:~2%\&xcopy /e %c% %%i%c:~2%\
...


This code doesn't specify the source file. How to copy?
If you change the above to set a=c:\IBM\456\78j9.txt, then only this file is copied to the current directory, and the original path-level folders are still not there


[ Last edited by flying008 on 2008-4-14 at 01:43 PM ]
Floor 11 Posted 2008-04-14 15:05 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
The xcopy /e command means to copy all the contents in the folder (including empty folders). Did you test it?
批处理之家新域名:www.bathome.net
Floor 12 Posted 2008-04-14 15:31 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
There is no more foolish way than this, but it must be achievable (if the number of directory levels is different, make corresponding changes):

@echo off
set aa=c:\IBM\123\456\789.nsf
set ab=c:\IBM\abc\cd.id
set ac=c:\Lotus\abc\cd.js
set path=%cd%
for /f "tokens=2-4 delims=\" %%a in ("%aa%") do (
md %%a&cd %%a&&md %%b&cd %%b&&md %%c&cd %%c
copy /y "%aa%"
)
cd %path%
for /f "tokens=2,3 delims=\" %%i in ("%ab%") do (
md %%i&cd %%i&&md %%j&cd %%j
copy /y "%ab%"
)
cd %path%
for /f "tokens=2,3 delims=\" %%m in ("%ac%") do (
md %%m&cd %%m&&md %%n&cd %%n
copy /y "%ac%"
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flying008 +2 2008-04-14 17:27
Floor 13 Posted 2008-04-14 17:25 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 245
Posts 103
Joined 2006-06-30 00:00
20-year member
UID 57801
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-4-14 15:05:
The xcopy /e command means to copy all the contents in the folder (including empty folders). Have you tested it?


Thank you, sir! But my original file path is relatively deep, and there are very many system files. Copying the folder in this way brings unwanted files, which is not the result of the specified file I wanted. Thanks...
Floor 14 Posted 2008-04-14 20:00 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Then we have to create empty folders first, and the code will be much more.
批处理之家新域名:www.bathome.net
Floor 15 Posted 2008-04-15 11:55 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 109
Posts 30
Joined 2007-09-05 14:27
18-year member
UID 96621
Gender Male
Status Offline
Originally posted by flying008 at 2008-4-14 11:13 AM:
Hello, Mr. abcd!

When running this bat, an internal error is prompted. May I ask how to modify it so that this bat can be used to copy the following 3 files?
c:\IBM\123\456\789.nsf
c:\IBM\abc\cd.id
c:\Lotus ...


Remove /e
Forum Jump: