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 07:03
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Delete non-empty folders under the specified directory View 1,471 Replies 2
Original Poster Posted 2009-07-31 12:16 ·  中国 四川 成都 电信
新手上路
Credits 2
Posts 2
Joined 2009-07-28 11:47
17-year member
UID 149509
Gender Male
Status Offline
I have made a file upload system. When employees use it, the system will prompt them to first enter their employee number and then click "Submit". (At this time, a folder named after the employee's work number will be generated in a directory called "scan" on my server A.) Then the employee can enter the next interface to select some files on the local computer and then click "Upload".

I will run a batch script on my server A. It will first check if there are folders in the "scan" directory. If there are, it will copy this folder and the files inside it to a file server B accessible to all employees, and then the batch script will immediately delete the folder in the "scan" directory. (That is, to achieve the purpose of cutting.)

Now the problem is that when the batch script on server A is performing the copy + delete operation, an employee is preparing to upload a file. The folder named after his employee number has been generated in "scan", but he hasn't started uploading the file yet! At this time, the deletion action on server A has also deleted this empty folder, so he fails when uploading the file.

So my batch script should be modified. I hope to only delete the non-empty folders in the "D:\scan\" directory.

Please help me figure out a way. Thanks a lot!
Floor 2 Posted 2009-07-31 13:02 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Folders with files inside will be deleted:
@echo off
for /f "delims=" %%a in ('dir /ad /b D:\scan') do (
dir /a-d /b /s "D:\scan\%%a" && rd /s/q "D:\scan\%%a"
) >nul 2>nul
pause


---------------
Folders with files or subfolders inside will be deleted:
@echo off
pushd D:\scan
for /f "delims=" %%a in ('dir /ad /b') do (
for /f %%i in ('dir /a/b "%%a"') do if exist "%%a" rd /s/q "%%a"
)
POPD
pause


[ Last edited by tireless on 2009-7-31 at 14:18 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zidian +1 2009-08-03 23:41
Floor 3 Posted 2009-08-03 23:42 ·  中国 四川 成都 电信
新手上路
Credits 2
Posts 2
Joined 2009-07-28 11:47
17-year member
UID 149509
Gender Male
Status Offline
The method on the 2nd floor seems feasible. I'll give you points first. Thanks for your support!
Forum Jump: