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 04:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Problem with deleting files using batch processing (solved) View 5,648 Replies 15
Original Poster Posted 2008-03-08 00:52 ·  中国 广东 广州 白云区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Want to use batch processing to delete the files in the c:\Documents and Settings\CCTV\My Documents\YYGH\DATA folder and subfolders, while keeping the subfolders, please help! Thank you

cd c:\Documents and Settings\CCTV\My Documents\YYGH\DATA
del *.* /s /q This is what I wrote, but I think there is a problem,

1. If there is no CCTV path, it will delete all files on the local machine
2. If this script is started on another partition outside the C drive, then it will delete all files in the script startup directory (don't know how to avoid this situation)

[ Last edited by jpowe on 2008-3-10 at 12:39 AM ]
Floor 2 Posted 2008-03-08 01:45 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
简单!简单!再简单!
Floor 3 Posted 2008-03-08 13:43 ·  中国 广东 佛山 南海区 电信
初级用户
★★
Credits 131
Posts 72
Joined 2008-02-24 14:31
18-year member
UID 111317
Gender Male
Status Offline
del /s /q "%USERPROFILE%\My Documents\YYGH\DATA\*.*"

This is to delete all files in the yygh\data folder and its subdirectories under My Documents of the currently logged-in user. No matter where this script is placed, it will delete this location.
Floor 4 Posted 2008-03-08 14:09 ·  中国 广东 广州 白云区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
If it's not the current logged-in user, how should it be written?
Floor 5 Posted 2008-03-08 14:10 ·  中国 广东 广州 白云区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Originally posted by terse at 2008-3-8 01:45 AM:
Try adding quotes around the path

Excuse me. I'm a newbie, please help. Thank you
Floor 6 Posted 2008-03-08 16:02 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
It's just one line.
del "c:\Documents and Settings\CCTV\My Documents\YYGH\DATA\*.*" /s /q
简单!简单!再简单!
Floor 7 Posted 2008-03-08 20:54 ·  中国 广东 广州 白云区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Originally posted by terse at 2008-3-8 04:02 PM:
Just one line should be okay
del "c:\Documents and Settings\CCTV\My Documents\YYGH\DATA\*.*" /s /q

The BAT file made with del "c:\Documents and Settings\CCTV\My Documents\YYGH\DATA\*.*" /s /q is not working, and it prompts that the path is not found
Floor 8 Posted 2008-03-08 23:26 ·  中国 福建 福州 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
Originally posted by jpowe at 2008-3-8 00:52:
Want to use batch processing to delete the files in the folder c:\Documents and Settings\CCTV\My Documents\YYGH\DATA and its subfolders, while keeping the subfolders. Please help! Thank you

cd c:\Documents and Settings\CCTV\My Documents\YYGH\DATA
del *.* /s /q This is what I wrote, but I feel there is a problem,

1. If there is no CCTV path, it will delete all files on the local machine
2. If this script is started from another partition outside the C drive, then it will delete all files in the script's starting directory (don't know how to avoid this situation)


For the second point, you can add a /d switch after cd, that is

cd /d "c:\Documents and Settings\CCTV\My Documents\YYGH\DATA"

The first problem means that you can't be sure whether this CCTV folder exists?
Floor 9 Posted 2008-03-08 23:34 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
Just now, I carefully looked at the requirements of the original poster and found that there are some contradictions.

Since the DATA folder has been deleted, how can there still be subfolders?

Is it necessary to delete the files in this DATA folder and all its subfolders, but keep the entire folder directory structure?

If that's the case, you can try the following code


@echo off
set pth=c:\Documents and Settings\CCTV\My Documents\YYGH\DATA

if exist "%pth%" (del /q /s "%pth%">nul) else (echo The target folder "%pth%" does not exist)
pause


[ Last edited by suntb on 2008-3-8 at 11:43 PM ]
Floor 10 Posted 2008-03-09 20:46 ·  中国 广东 广州 荔湾区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Originally posted by suntb at 2008-3-8 11:34 PM:
Just now I carefully looked at the building owner's requirements and found some contradictions

Since the DATA folder has been deleted, how can there still be subfolders

Is it necessary to delete this DATA folder and all its subfiles...

Your understanding is completely correct, and I have also used your CODE, which is very good. Now there is another problem. I want to add a few more paths to delete. How should I write it? It's troublesome for you.
For example: to delete
c:\Documents and Settings\CCTV\My Documents\YYGH\DATA
c:\Documents and Settings\CCTV\My Documents\YYGH\DA

Because there are some files under the \YYGH\ path that I don't want to delete, but I need to delete the files in the \DATA\ and \DA\ folders and the files in the subfolders. Please help. Thank you
Floor 11 Posted 2008-03-09 21:32 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
```
@echo off
set pth=c:\Documents and Settings\CCTV\My Documents\YYGH
for %%i in (DATA DA) do (del /q /s "%pth%\%%i" 1>nul)
pause
```

[ Last edited by suntb on 2008-3-9 at 09:33 PM ]
Floor 12 Posted 2008-03-09 22:08 ·  中国 广东 广州 荔湾区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Can folders with Chinese names be added in the parentheses () as well?
Floor 13 Posted 2008-03-09 22:50 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
Delete all files in all subfolders under c:\Documents and Settings\CCTV\My Documents\YYGH, while keeping the file and subfolder structure under the YYGH folder

@echo off
set "pth=c:\Documents and Settings\CCTV\My Documents\YYGH"
for /r "%pth%" %%i in (.) do (if not "%%i"=="%pth%\." (del /q /s "%%i\*.*" 1>nul))
pause
Floor 14 Posted 2008-03-09 22:51 ·  中国 福建 福州 连江县 联通
高级用户
★★
Credits 581
Posts 277
Joined 2006-12-23 05:10
19-year member
UID 74328
Gender Male
Status Offline
Does this meet the LZ's requirements? No need to specify the subfolder name, can handle Chinese and subfolders with spaces included
Floor 15 Posted 2008-03-10 00:38 ·  中国 广东 广州 白云区 电信
初级用户
Credits 152
Posts 20
Joined 2004-03-03 00:00
22-year member
UID 19053
Gender Male
Status Offline
Originally posted by suntb at 2008-3-9 10:51 PM:
Does this meet the requirements of the original poster?

No need to specify the subfolder name. It can handle subfolders including Chinese characters and spaces.

Thank you suntb for your powerful processing ability!
Forum Jump: