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-02 00:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can any experts help me out and see how this batch file should be done? View 1,057 Replies 12
Original Poster Posted 2006-08-14 13:07 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Suppose there is a directory AAA, and inside it there are many subdirectories a1, a2, a3..., and each subdirectory contains N files
with the same names but different contents, such as a1.txt, a2.txt, a3.txt. Now I want to back up
the AAA directory. The backup directory can be specified arbitrarily (for example, BBB), but the subdirectory structure inside AAA must remain unchanged, and I only want to back up
the a1.txt, a2.txt, and a3.txt files in each subdirectory.
AAA
|---a1
| |-----XXXX
| |-----XXXX
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a2
| |-----XXXX
| |-----XXXX
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a3
| |-----XXXX
| |-----XXXX
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a4
|-----XXXX
|-----XXXX
|-----a1.txt
|-----a2.txt
|-----a3.txt
The structure after backup should be as follows:
BBB
|---a1
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a2
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a3
| |-----a1.txt
| |-----a2.txt
| |-----a3.txt
|
|---a4
|-----a1.txt
|-----a2.txt
|-----a3.txt

Can any experts help me out and make a batch file for this? Thanks in advance, everyone.
Floor 2 Posted 2006-08-14 13:38 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
If it's just for backup, I recommend using compressed backup with WinRAR or PKZIP
@echo off
start /wait WinRAR u -ep1 -r BBB AAA\*.TXT

or
@echo off
Pkzip -a -p -r BBB AAA\*.TXT
Floor 3 Posted 2006-08-14 13:48 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Supplement: another kind of directory structure
@echo off
start /wait WinRAR u -es1 -r BBB AAA\*.TXT

or
@echo off
Pkzip -a -P -r BBB AAA\*.TXT

In post #2 and post #3, pay attention to the capitalization of the compression command parameters
Floor 4 Posted 2006-08-14 17:55 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  After testing, the following code works. This batch file can be placed anywhere:

@echo off
setlocal enabledelayedexpansion
cd /d "path to AAA"
xcopy /i /s /t %cd% "path to BBB"
for /r %%i in (.) do (
set cur_root=%%~dfi
set "new_root=!cur_root:path to AAA=path to BBB!"
xcopy /y "!cur_root!\a1.txt" "!new_root!"
xcopy /y "!cur_root!\a2.txt" "!new_root!"
xcopy /y "!cur_root!\a3.txt" "!new_root!"
)
explorer "path to BBB"
pause


[ Last edited by namejm on 2006-8-14 at 19:57 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-08-14 17:59 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
This is frustrating. Brother above, your method can't meet my requirements. Sorry to trouble you, but please work a bit harder on it!
Floor 6 Posted 2006-08-14 18:03 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  I forgot to mention that this code needs to be run under directory AAA. Try it again and see whether it works.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-08-14 18:39 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Is compressed backup no good? xcopy shouldn't be that complicated
@echo off
xcopy /e /h /r /y .\aaa\*.txt .\bbb\
or
@echo off
FOR %%A IN (a1 a2 a3) DO xcopy /e /h /r /y .\aaa\%%A.txt .\bbb\
Floor 8 Posted 2006-08-14 19:18 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Originally posted by namejm at 2006-8-14 17:55:
  Try the following code, it may be useful (untested; if there are problems please report back, but it shouldn't damage the original data):

@echo off
setlocal enabledelayedexpansion
for /r %%i in (.) do (
...
『Post #6』:

  I forgot to mention that this code needs to be run under directory AAA. Try it again and see whether it works.


Man, or maybe little bro, it doesn't work. It shows “parse error”

[ Last edited by jtyuer on 2006-8-14 at 19:19 ]
Floor 9 Posted 2006-08-14 19:42 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Originally posted by fastslz at 2006-8-14 18:39:
Is compressed backup no good? xcopy shouldn't be that complicated
@echo off
xcopy /e /h /r /y .\aaa\*.txt .\bbb\
or
@echo off
FOR %%A IN (a1 a2 a3) DO xcopy /e /h /r /y .\aaa\%%A.txt .\bbb\

Based on the hint, after making suitable changes, it's done.
Thanks to all the bosses, thanks!
Floor 10 Posted 2006-08-14 20:04 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by fastslz at 2006-8-14 18:39:
Is compressed backup no good? xcopy shouldn't be that complicated
@echo off
xcopy /e /h /r /y .\aaa\*.txt .\bbb\
or
@echo off
FOR %%A IN (a1 a2 a3) DO xcopy /e /h /r /y .\aaa\%%A.txt .\bbb\


  If you want to use xcopy, it won't be that simple, because the OP wants to copy the three files a1.txt, a2.txt, and a3.txt from each subdirectory, and also requires that the location of the bbb directory can be specified arbitrarily. Looking at your two pieces of code, the first one just copies all txt files into the bbb folder under the same directory as the aaa folder, while the second one does copy only those three files, but can only copy them into the bbb folder under the same directory. The common shortcoming of both is that they cannot arbitrarily specify the location of the bbb folder.

  The modified code is in post #4. The original code was not tested, so there were some typos and omissions. OP, please test it again.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-08-14 21:06 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Originally posted by namejm at 2006-8-14 20:04:


  If you want to use xcopy, it won't be that simple, because the OP wants to copy the three files a1.txt, a2.txt, and a3.txt from each subdirectory, and also requires that the location of the bbb directory can be specified arbitrarily. ...


I tried it according to your method, boss, and it wasn't ideal. While it copied the needed files into the BBB directory, it also moved the other grandchild directories inside AAA (subdirectories of subdirectories—don't know if that's the right term?) into the BBB directory as well.

After tinkering with it all day, I finally made one myself. It's not bad and basically meets my requirements. I'll post it here, boss—please help analyze it and see whether it can be made more concise. Mainly I'm using it to make a WindowsXP OEM 20-in-1 disc. Before, manually extracting and copying files was really annoying, and it was easy to make mistakes.

This thing I cobbled together myself:
@Echo Off
Set TT=WindowsXP OEM File Extraction and Copy Tool
COLOR 1F
TITLE %TT%

:start
CLS
Echo.
Echo ╭══════════════════╮
Echo ║ ║
Echo ╭═══┤ WindowsXP OEM File Extraction and Copy Tool ├═══╮
Echo ║ ║ ║ ║
Echo ║ ╰══════════════════╯ ║
Echo ║ ║
Echo ║ Do you want to extract OEM files from a CD or hard disk? ║
Echo ║ Or do you want to copy existing OEM files to the hard disk? ║
Echo ║ ║
Echo ║ Extract OEM files from a CD or hard disk ║
Echo ║ Copy existing OEM files to the hard disk ║
Echo ║ Extract OEM icons from a CD or hard disk ║
Echo ║ Copy existing OEM icons to the hard disk ║
Echo ║ Exit ║
Echo ║ ║
Echo ╟──────────────────────────╢
Echo ║ Note: 1. Enter the absolute path of the CD or hard disk file directory yourself according to the prompt.║
Echo ║ 2. Spaces are not allowed in the file path. ║
Echo ╟══════════════════════════╢
Echo ║ Copyright: jtyuer For exchange and study by forum friends, reprints not prohibited ║
Echo ╰──────────────────────────╯
Echo.
Set Choice=
Set /P Choice= Please choose the operation to perform (1/2/3/4/Q), then press Enter:

If Not '%Choice%'=='' SET Choice=%Choice:~0,1%
If /I '%Choice%'=='1' GOTO funa
If /I '%Choice%'=='2' GOTO funa
If /I '%Choice%'=='3' GOTO funa
If /I '%Choice%'=='4' GOTO funa
If /I '%Choice%'=='Q' EXIT
Goto start

:funa
If /I '%Choice%'=='1' (
TITLE %TT% - Extract OEM files from CD or hard disk
)
If /I '%Choice%'=='2' (
TITLE %TT% - Copy existing OEM files to hard disk
Goto funb
)
If /I '%Choice%'=='3' (
TITLE %TT% - Copy existing OEM icons to hard disk
Goto func
)
If /I '%Choice%'=='4' (
TITLE %TT% - Copy existing OEM icons to hard disk
Goto fund
)
If "%Err%"=="" (
CLS
)

:funa1
cls
Echo Please enter the directory name where the OEM files are located, such as c:\1234
Echo.
Echo Do not include the i386 subdirectory path when entering the path
echo.
Set input=
Set /P input= Enter path:
If "%input%"=="" Goto funa
If Not Exist %input% (
Set Err=1
Echo.
CLS
Echo The OEM file directory path you entered is incorrect, please enter it again
Echo.
Goto :funa1
)

::funa2
cls
Echo Please enter the directory name to save the OEM files, such as d:\1234
echo.
Set output=
Set /P output= Enter path:
If "%input%"=="" Goto funa1
If Not Exist %output% (
Set Err=1
Echo.
CLS
Echo The directory %output% for saving OEM files does not exist; %output% will be created automatically
Echo.
Echo.
Echo.
@md %output%
)

:Copy
@Echo dpcdll.dl_>%temp%\oem.txt
@Echo oembios.bi_>>%temp%\oem.txt
@Echo oembios.ca_>>%temp%\oem.txt
@Echo oembios.da_>>%temp%\oem.txt
@Echo oembios.si_>>%temp%\oem.txt
@Echo pidgen.dll>>%temp%\oem.txt
@Echo setupp.ini>>%temp%\oem.txt
@Echo winnt.sif>>%temp%\oem.txt
for /f %%a in ('dir /ad /b %input%') do md %output%\%%a\i386 & FOR /f %%b in (%temp%\oem.txt) do copy /y %input%\%%a\i386\%%b %output%\%%a\i386\
Goto Exit

:funb
cls
Echo Please enter the directory name where the OEM files are located, such as c:\1234
Echo.
Echo Do not include the i386 subdirectory path when entering the path
echo.
Set input=
Set /P input= Enter path:
If "%input%"=="" Goto funb
If Not Exist %input% (
Set Err=1
Echo.
CLS
Echo The OEM file directory path you entered is incorrect, please enter it again
Echo.
Goto funb
)

:funb1
cls
Echo Please enter the directory name to copy the OEM files to, such as d:\1234
echo.
Set output=
Set /P output= Enter path:
If "%input%"=="" Goto funb1
If Not Exist %output% (
Set Err=1
Echo.
CLS
Echo The directory %output% for saving OEM files does not exist; %output% will be created automatically
Echo.
Echo.
@md %output%
)

:Copy
For /f %%m In ('dir /ad /b %input%') do md %output%\%%m\i386 & Copy /y %input%\%%m\i386\*.* %output%\%%m\i386\
Goto Exit

:func
cls
Echo Please enter the directory name where the OEM icons are located, such as c:\1234
Echo.
Set input=
Set /P input= Enter path:
If "%input%"=="" Goto func
If Not Exist %input% (
Set Err=1
Echo.
CLS
Echo The OEM icon directory path you entered is incorrect, please enter it again
Echo.
Goto func
)

:func1
cls
Echo Please enter the directory name to copy the OEM icons to, such as d:\1234
echo.
Set output=
Set /P output= Enter path:
If "%input%"=="" Goto funb1
If Not Exist %output% (
Set Err=1
Echo.
CLS
Echo The directory %output% for saving OEM icons does not exist; %output% will be created automatically
Echo.
Echo.
@md %output%
)

:Copy
If Exist %temp%\ml.txt Del %temp%\ml.txt
If Exist %temp%\oem.txt Del %temp%\oem.txt
@Echo oeminfo.ini>%temp%\oem.txt
@Echo oemlogo.bmp>>%temp%\oem.txt
for /f %%a in ('dir /ad /b %input%') do md %output%\%%a\$OEM$\$$\SYSTEM32 & FOR /f %%b in (%temp%\oem.txt) do copy /y %input%\%%a\$OEM$\$$\SYSTEM32\%%b %output%\%%a\$OEM$\$$\SYSTEM32\
Goto Exit



:fund
cls
Echo Please enter the directory name where the OEM icons are located, such as c:\1234
Echo.
Set input=
Set /P input= Enter path:
If "%input%"=="" Goto fund
If Not Exist %input% (
Set Err=1
Echo.
CLS
Echo The directory path where the OEM icons are located is incorrect, please enter it again
Echo.
Goto fund
)

:fund1
cls
Echo Please enter the directory name to copy the OEM icons to, such as d:\1234
echo.
Set output=
Set /P output= Enter path:
If "%input%"=="" Goto fund1
If Not Exist %output% (
Set Err=1
Echo.
CLS
Echo The directory %output% for saving OEM files does not exist; %output% will be created automatically
Echo.
@md %output%
)

:Copy
For /f %%m In ('dir /ad /b %input%') do md %output%\%%m\$OEM$\$$\SYSTEM32 & Copy /y %input%\%%m\$OEM$\$$\SYSTEM32\*.* %output%\%%m\$OEM$\$$\SYSTEM32\
Goto Exit

:Exit
CLS
Echo.
Echo ╭══════════════════╮
Echo ║ ║
Echo ╭═══┤ WindowsXP OEM File Extraction and Copy Tool ├═══╮
Echo ║ ║ ║ ║
Echo ║ ╰══════════════════╯ ║
Echo ║ ║
Echo ║ The operation you needed has been completed. Please check. . . ║
Echo ║ Thank you for using it! ║
Echo ║ Are you going to continue copying files? ║
Echo ║ ║
Echo ║ ║
Echo ║ Continue copying files ║
Echo ║ ║
Echo ║ Exit ║
Echo ║ ║
Echo ╟──────────────────────────╢
Echo ║ Note: 1. Enter the absolute path of the CD or hard disk file directory yourself according to the prompt.║
Echo ║ 2. Spaces are not allowed in the file path. ║
Echo ╟══════════════════════════╢
Echo ║ Copyright: jtyuer For exchange and study by forum friends, reprints not prohibited ║
Echo ╰──────────────────────────╯
Echo.
Set Choice=
Set /P Choice= Please choose the operation to perform (y/n), then press Enter:

If Not '%Choice%'=='' SET Choice=%Choice:~0,1%
If /I '%Choice%'=='y' GOTO start
If /I '%Choice%'=='n' GOTO end
Goto Exit

:End
Floor 12 Posted 2006-08-15 08:34 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by jtyuer at 2006-8-14 21:06:


I tried it according to your method, boss, and it wasn't ideal. While it copied the needed files into the BBB directory, it also moved the other grandchild directories inside AAA (subdirectories of subdirectories—don't know if that's the right term?) into the BBB directory as well.


  Looking at your post in the first post, the subdirectories under your AAA directory are only one level deep, and you never said there were subdirectories under those subdirectories either. Let me sweat first. Please post the complete directory structure of AAA and the directories and files you want, then we can talk. Before your exact requirements are made clear, nobody's code is correct.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-08-15 08:54 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re jtyuer『Post #11』:

  There really is room to simplify your code further, and there are also several errors in it. Let me briefly mention one or two:

  1. “Spaces are not allowed in the file path.” Actually there's no need to be so strict. Paths can contain spaces too, as long as you put the path in quotes;

  2. In the line If Not '%Choice%'=='' SET Choice=%Choice:~0,1%, the if statement is unnecessary; just keeping the set statement is enough;

  3. In the line If /I '%Choice%'=='1' GOTO funa, the /I is unnecessary, while in If /I '%Choice%'=='Q' EXIT it should be kept. Think about the specific reason a couple more times and you'll understand;

  4. In the :funa section, I don't know where %Err% in the line If "%Err%"=="" comes from;

  5. In the :funa1 section, the line goto :funa1 is wrong; what puzzles me is that for sections like :funa1, :funa2, and later :funb1, there is no explicit statement before them indicating a jump to that section. Will they get executed?

  6. The label name :Copy is inappropriate because it conflicts with the copy command in CMD. If you ever need to use goto copy, who knows what the result will be (I haven't tested it here yet). Even if nothing unexpected happens, naming a label exactly the same as a command is not a good practice;

  7.
@Echo dpcdll.dl_>%temp%\oem.txt
@Echo oembios.bi_>>%temp%\oem.txt
@Echo oembios.ca_>>%temp%\oem.txt
@Echo oembios.da_>>%temp%\oem.txt
@Echo oembios.si_>>%temp%\oem.txt
@Echo pidgen.dll>>%temp%\oem.txt
@Echo setupp.ini>>%temp%\oem.txt
@Echo winnt.sif>>%temp%\oem.txt

The @ symbols in this section are completely unnecessary. It can be simplified to:

(Echo dpcdll.dl_
Echo oembios.bi_
Echo oembios.ca_
Echo oembios.da_
Echo oembios.si_
Echo pidgen.dll
Echo setupp.ini
Echo winnt.sif)>%temp%\oem.txt


  8. The only function of the :funa section is to change the CMD window title. Actually, you could just put the title changes directly into the corresponding sections, so it feels like this section has no real reason to exist.

  I've rambled on about quite a few points. I hope you'll make the corresponding changes according to your specific needs. There's plenty of room for simplification
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: