![]() |
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-12 07:05 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » How to use batch commands to determine if two file names are the same? |
| Printable Version 1,997 / 13 |
| Floor1 ChengXu | Posted 2010-06-15 11:37 |
| 新手上路 Posts 18 Credits 18 | |
|
To determine if the two file names are the same using batch commands:
First, get the file names of the two files. Then compare them. If they are the same, do nothing. If they are different, copy all contents from the S:\Source\ folder to the C:\Target\ folder. The following is the batch code example: ```batch @echo off set "target_file=C:\Target\1.txt" set "source_file=S:\Source\1.txt" for %%i in ("%target_file%") do set "target_name=%%~nxi" for %%j in ("%source_file%") do set "source_name=%%~nxi" if "%target_name%"=="%source_name%" ( echo The file names are the same, no operation. ) else ( xcopy "S:\Source\" "C:\Target\" /e /y ) ``` |
|
| Floor2 keeds | Posted 2010-06-15 12:09 |
| 新手上路 Posts 8 Credits 11 | |
|
dir /a-d /b "Source folder path" > temp.txt
for /f %%i in (temp.txt) do (IF NOT EXIST "Target folder path\%%i" move "Source folder path\%%i" "Target folder path") del temp.txt |
|
| Floor3 ChengXu | Posted 2010-06-15 12:36 |
| 新手上路 Posts 18 Credits 18 | |
|
I don't quite understand, especially the first line dir /a-d /b "source folder path">temp.txt
And for /f %%i in (temp.txt) What do these two sentences mean? What role do they play? |
|
| Floor4 ChengXu | Posted 2010-06-15 12:54 |
| 新手上路 Posts 18 Credits 18 | |
|
My purpose is like this: The contents in the two folders C:\Target and S:\Source should be the same. If the contents in S:\Source change, all the files in S:\Source should be copied to C:\Target. So I created a txt file in both C:\Target and S:\Source to identify the version (according to the file name). There is only this txt - formatted file in the entire folder.
So the content that this batch processing should execute is the following content: 1. Get the file name of the txt file in C:\Target 2. Judge whether there is a file with the same name as the txt file in C:\Target in S:\Source 3. If there is a file with the same name, do nothing. If there is no file with the same name, copy all the contents in the S:\Source\ folder to the C:\Target\ folder |
|
| Floor5 keeds | Posted 2010-06-15 12:55 |
| 新手上路 Posts 8 Credits 11 | |
|
dir /a-d /b "C:\Target\">temp.txt
Export the names of files (excluding folders) in C:\Target\ to temp.txt for /f %%i in (temp.txt) Loop to read each line in temp.txt (which are the file names in C:\Target\, such as 1.txt, 2.txt) |
|
| Floor6 keeds | Posted 2010-06-15 12:59 |
| 新手上路 Posts 8 Credits 11 | |
Originally posted by ChengXu at 2010-6-15 12:54: for /f %%i in (C:\Target\1.txt) do (IF NOT EXIST "C:\Target\%%i" move "S:\Source\%%i" "C:\Target") |
|
| Floor7 Hanyeguxing | Posted 2010-06-15 13:42 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
No need to make a 1.txt:
1. If you want to include subdirectories, finally write /zfdis 2. If you want to include read-only files, finally write /zfdisr 3. If you want to include hidden or system attribute files, finally write /zfdisrh 4. If you want to include empty directories, finally write /zfdisrhe 5. If you want to carry DACL permissions, finally write /zfdisrheo 6. If you want to carry SACL permissions, finally write /zfdisrhex [ Last edited by Hanyeguxing on 2010-6-15 at 13:45 ] |
|
| Floor8 ChengXu | Posted 2010-06-15 16:23 |
| 新手上路 Posts 18 Credits 18 | |
Originally posted by keeds at 2010-6-15 12:59: |
|
| Floor9 ChengXu | Posted 2010-06-15 16:27 |
| 新手上路 Posts 18 Credits 18 | |
Originally posted by Hanyeguxing at 2010-6-15 13:42: Because the entire folder is relatively large, it will take a long time to copy all. If there is no change in S:\Source (that is, the file name of the txt used to mark the version number in S:\Source is the same as that in C:\Target), then do not perform the copy action; if different, then copy. Is there a way? |
|
| Floor10 Hanyeguxing | Posted 2010-06-15 16:51 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
The /d parameter itself is the parameter for comparing whether there is a modification. Only when there is a modification or difference will the required part be automatically copied.
For example: 1. Some files exist in the source directory but not in the target directory, then these files are copied. 2. Some files exist in both the source directory and the target directory, then compare the last modification time. Only when the file in the source target is newer than the file in the target directory will these files be copied. The /u parameter takes the target directory as a sample and only copies the files that exist in the target directory. When /d and /u are used together, it excludes the files that are only in the source directory, and only compares the last modification time of the files that exist in both directories: /du [ Last edited by Hanyeguxing on 2010-6-15 at 17:00 ] |
|
| Floor11 ChengXu | Posted 2010-06-15 17:55 |
| 新手上路 Posts 18 Credits 18 | |
Originally posted by Hanyeguxing at 2010-6-15 16:51: Oh, so that's how it is! It's really amazing! I want to ask where I can find the meanings of parameters like z, f, d, i, etc.? To be honest, when I searched on Baidu, I didn't know what keywords to search for. |
|
| Floor12 Hanyeguxing | Posted 2010-06-15 19:02 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
Run `xcopy /?` in cmd
|
|
| Floor13 ChengXu | Posted 2010-06-15 20:45 |
| 新手上路 Posts 18 Credits 18 | |
|
Run `XCOPY /?` directly in the command line.
Thanks to all the friends who helped me solve the problem upstairs. The problem is solved, thank you! |
|
| Floor14 ChengXu | Posted 2010-06-15 20:47 |
| 新手上路 Posts 18 Credits 18 | |
|
Oh, what a bummer! You can only rate 2 points within 24 hours?!
Sorry! There are still friends above who haven't rated, I'll come back to rate next time! Thank you everyone! |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |