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-06-27 08:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Remove files in the directory except those with .td and .cfg extensions View 2,059 Replies 8
Original Poster Posted 2008-05-28 11:33 ·  中国 广东 惠州 电信
初级用户
Credits 114
Posts 6
Joined 2005-04-04 00:00
21-year member
UID 37816
Gender Male
Status Offline
[Help] Remove files in the directory except those with .td and .cfg extensions

I know I need to first find the filenames other than these two extensions, but I don't know how to do it. Please give me some advice, thank you
Floor 2 Posted 2008-05-28 12:36 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Assume moving from d:\test to d:\temp


@echo off&setlocal enabledelayedexpansion
set str=.td .cfg
for /f "delims=" %%i in ('dir /s /a-d /b d:\test\*.*') do (
set "flag="
for %%a in (%str%) do if "%%a"=="%%~xi" set flag=A
if not defined flag move /y "%%i" d:\temp\
)
echo Done!
pause>nul


[ Last edited by zw19750516 on 2008-5-28 at 01:08 PM ]
批处理之家新域名:www.bathome.net
Floor 3 Posted 2008-05-28 16:04 ·  中国 广东 惠州 电信
初级用户
Credits 114
Posts 6
Joined 2005-04-04 00:00
21-year member
UID 37816
Gender Male
Status Offline
I just want to say thank you.
But this processing will also move the files in the subdirectories. It is suggested that when using dir /s /a-d /b, it should be changed to dir /a-d /s
Floor 4 Posted 2008-05-28 16:17 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by bsmqu at 2008-5-28 16:04:
I just want to say thank you.
But this process will also move the files in the subdirectories. It is recommended to change dir /s /a-d /b to dir /a-d /s when using it.

I thought the owner wanted to move all, so I added /s. If you don't want to move the files in the subdirectories, you can remove /s.

[ Last edited by zw19750516 on 2008-5-28 at 04:23 PM ]
批处理之家新域名:www.bathome.net
Floor 5 Posted 2008-05-28 16:46 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
Originally posted by zw19750516 at 2008-5-28 04:17 PM:

I thought the owner wanted to move all, so I added /s. If you don't want to move subdirectories, just remove /s.

[ Last edited by zw19750516 on 2008-5-28 at 04:23 PM ]



@echo off&setlocal enabledelayedexpansion
set str=.td .cfg
for /f "delims=" %%i in ('dir /a-d /b D:\batch\test\*.*') do (
set "flag="
for %%a in (%str%) do if "%%a"=="%%~xi" set flag=A
if not defined flag move /y "%%i" D:\batch\test\temp\
)
pause

Why does this error occur when I run it? The error message is:
D:\batch>moveFile
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .


In addition, 'dir /a-d /s' also errors:

D:\batch>moveFile
The system cannot find the file specified.
The system cannot find the file specified.
The filename, directory name, or volume label syntax is incorrect.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the file specified.
The filename, directory name, or volume label syntax is incorrect.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the file specified.
The filename, directory name, or volume label syntax is incorrect.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the file specified.
" Total Files Listed:" is not a recognized device.
" Total Files Listed:" is not a recognized device.
The filename, directory name, or volume label syntax is incorrect.
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .

Please analyze it, thank you
無求一生光輝 唯朢鬥志不會斷
Floor 6 Posted 2008-05-28 17:24 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Is the path D:\batch\test\temp\ actually existing?
批处理之家新域名:www.bathome.net
Floor 7 Posted 2008-05-28 17:31 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
Originally posted by zw19750516 at 2008-5-28 05:24 PM:
Does this path D:\batch\test\temp\ actually exist?


It exists. I ran
@echo off&setlocal enabledelayedexpansion
set str=.td .cfg

for /f "delims=" %%i in ('dir/b/a-d "D:\batch\test\*.*"') do (
set "flag="
for %%a in (%str%) do if "%%a"=="%%~xi" set flag=A
if not defined flag move /y "%%i" D:\batch\test\temp\
)
pause
It works! Very strange, it doesn't work without /s
-------------------------------------------------------------
In addition, I tested

for /f "delims=" %%i in ('dir/b/a-d "D:\batch\test\*.*"') do (
echo %%~nxi
)
It can traverse the files under test, very strange哦
無求一生光輝 唯朢鬥志不會斷
Floor 8 Posted 2008-05-28 17:32 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
Mistaken, it should be running
for /f "delims=" %%i in ('dir/b/a-d/s "D:\batch\test\*.*"') do (
set "flag="
for %%a in (%str%) do if "%%a"=="%%~xi" set flag=A
if not defined flag move /y "%%i" D:\batch\test\temp\
)
pause
is okay
無求一生光輝 唯朢鬥志不會斷
Floor 9 Posted 2008-05-28 17:36 ·  中国 江苏 南京 电信
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
It should be that this 'dir/b/a-d "D:\batch\test\*.*"' command is okay! Now the situation is that there are several files under the test folder, but it just shows a few lines of "The system cannot find the file specified." error messages. It's very strange. What's the reason? Why does it say the path is incorrect?
無求一生光輝 唯朢鬥志不會斷
Forum Jump: