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-12 03:42
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use batch commands to determine if two file names are the same? View 1,993 Replies 13
Original Poster Posted 2010-06-15 11:37 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
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
)
```
Floor 2 Posted 2010-06-15 12:09 ·  中国 浙江 杭州 电信
新手上路
Credits 11
Posts 8
Joined 2010-06-10 23:26
16-year member
UID 168569
Gender Male
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-18 14:55
Floor 3 Posted 2010-06-15 12:36 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
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?
Floor 4 Posted 2010-06-15 12:54 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
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
Floor 5 Posted 2010-06-15 12:55 ·  中国 浙江 杭州 电信
新手上路
Credits 11
Posts 8
Joined 2010-06-10 23:26
16-year member
UID 168569
Gender Male
Status Offline
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)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-18 15:01
Floor 6 Posted 2010-06-15 12:59 ·  中国 浙江 杭州 电信
新手上路
Credits 11
Posts 8
Joined 2010-06-10 23:26
16-year member
UID 168569
Gender Male
Status Offline
Originally posted by ChengXu at 2010-6-15 12:54:
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 files in S:\Source should be copied to C:\Target. So ...


for /f %%i in (C:\Target\1.txt) do (IF NOT EXIST "C:\Target\%%i" move "S:\Source\%%i" "C:\Target")
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-21 09:22
Floor 7 Posted 2010-06-15 13:42 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
No need to make a 1.txt:
xcopy S:\Source C:\Target /zfdi

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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-21 09:22
Floor 8 Posted 2010-06-15 16:23 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
Originally posted by keeds at 2010-6-15 12:59:

for /f %%i in (C:\Target\1.txt) do (IF NOT EXIST "C:\Target\%%i" move "S:\Source\%%i" "C:\Target")

Still not right, because that txt file is used to mark the version number, so its name is different each time. If the batch file has a fixed name, then I have to change the batch file every time.

Is there a way to first get the file name of the txt in C:\Target\ (there is only one txt file),
then judge whether there is a file with the same name in S:\Source. If there is, copy the content in S:\Source to C:\Target\
Floor 9 Posted 2010-06-15 16:27 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
Originally posted by Hanyeguxing at 2010-6-15 13:42:
No need to make a 1.txt:
xcopy S:\Source C:\Target /zfdi

1. If you need to include subdirectories, finally write /zfdis
2. If you need to include read-only files, finally write /zfdisr
3. If you need to include ...


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?
Floor 10 Posted 2010-06-15 16:51 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-15 20:46
Floor 11 Posted 2010-06-15 17:55 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
Originally posted by Hanyeguxing at 2010-6-15 16:51:
The /d parameter itself is a parameter for comparing whether a modification has occurred. Only when there is a modification or a difference will it automatically copy the necessary parts.
For example:
1. Some files exist in the source directory but not in the target directory, then...


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.
Floor 12 Posted 2010-06-15 19:02 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Run `xcopy /?` in cmd
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ChengXu +1 2010-06-15 20:45
Floor 13 Posted 2010-06-15 20:45 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
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!
Floor 14 Posted 2010-06-15 20:47 ·  日本 IIJ
新手上路
Credits 18
Posts 18
Joined 2010-06-15 11:12
16-year member
UID 168871
Gender Female
Status Offline
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!
Forum Jump: