![]() |
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-08-02 01:29 |
48,037 topics / 350,122 posts / today 2 new / 48,250 members |
| DOS批处理 & 脚本技术(批处理室) » Can any experts help me out and see how this batch file should be done? |
| Printable Version 1,059 / 12 |
| Floor1 jtyuer | Posted 2006-08-14 13:07 |
| 初级用户 Posts 29 Credits 73 | |
|
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. |
|
| Floor2 fastslz | Posted 2006-08-14 13:38 |
| 铂金会员 Posts 2,315 Credits 5,493 From 上海 | |
|
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 |
|
| Floor3 fastslz | Posted 2006-08-14 13:48 |
| 铂金会员 Posts 2,315 Credits 5,493 From 上海 | |
|
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 |
|
| Floor4 namejm | Posted 2006-08-14 17:55 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
After testing, the following code works. This batch file can be placed anywhere:
[ Last edited by namejm on 2006-8-14 at 19:57 ] |
|
| Floor5 jtyuer | Posted 2006-08-14 17:59 |
| 初级用户 Posts 29 Credits 73 | |
|
This is frustrating. Brother above, your method can't meet my requirements. Sorry to trouble you, but please work a bit harder on it!
|
|
| Floor6 namejm | Posted 2006-08-14 18:03 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
I forgot to mention that this code needs to be run under directory AAA. Try it again and see whether it works.
|
|
| Floor7 fastslz | Posted 2006-08-14 18:39 |
| 铂金会员 Posts 2,315 Credits 5,493 From 上海 | |
|
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\ |
|
| Floor8 jtyuer | Posted 2006-08-14 19:18 |
| 初级用户 Posts 29 Credits 73 | |
Originally posted by namejm at 2006-8-14 17:55: |
|
| Floor9 jtyuer | Posted 2006-08-14 19:42 |
| 初级用户 Posts 29 Credits 73 | |
Originally posted by fastslz at 2006-8-14 18:39: Based on the hint, after making suitable changes, it's done. Thanks to all the bosses, thanks! |
|
| Floor10 namejm | Posted 2006-08-14 20:04 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by fastslz at 2006-8-14 18:39: 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. |
|
| Floor11 jtyuer | Posted 2006-08-14 21:06 |
| 初级用户 Posts 29 Credits 73 | |
Originally posted by namejm at 2006-8-14 20:04: 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 |
|
| Floor12 namejm | Posted 2006-08-15 08:34 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by jtyuer at 2006-8-14 21:06: 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. |
|
| Floor13 namejm | Posted 2006-08-15 08:54 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. The @ symbols in this section are completely unnecessary. It can be simplified to: 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 ![]() |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |