简体   繁体   中英

Batch file to check the destination is empty and destination have the folder path

This is my batch file,I need to copy a file into a folder,for that I need to check the destination,whether it's empty or not,if it's not empty then I need to check whether the destination is a folder(if destination=dsjffj,then have to exit from batch file),the destination need to have valid folder path.

if "Destination"=="" (
    set errorlevel=1
    exit /b
) else (
   dir /ad /b "Destination" 1> NUL 2> NUL
   if %ERRORLEVEL% EQU 0 (
   copy "Source" "Destination" /Y
   ) else (
     exit /b
     )
 )

I refer some stack overflow links also,but This batch file is working to validate empty destination,but it's not properly checking for the folder

Avoid assigning values to volatile Windows environment variables , eg set errorlevel=1 . Read Dave Benham's reply to setting ERRORLEVEL to 0 question :

If you want to force the errorlevel to 0, then you can use this totally non-intuitive, but very effective syntax: (call ) . The space after call is critical. If you want to set the errorlevel to 1, you can use (call) . It is critical that there not be any space after call .

Next commented code snippet could help:

@ECHO OFF
set "sourcefolder=D:\test\fooSrcFldr"
set "targetfolder=D:\test\fooDstFldr"
set "sourcefiles="
rem check source folder existence
if exist "%sourcefolder%\" (
    rem check source folder for its content
    for /F "delims=" %%G in ('dir /B /A "%sourcefolder%\*" 2^>NUL') do (
        set "sourcefiles=%%G"
    )
    rem check whether source folder is empty
    if not defined sourcefiles (
        echo source folder "%sourcefolder%\" is empty
        exit /B 1
    ) 
) else (
    echo source folder "%sourcefolder%\" does not exist
    exit /B 2
)
rem check target folder existence
if exist "%targetfolder%\" (
    rem target folder "%targetfolder%\" exists
) else (
    rem check whether it's possible to create target folder 
    if exist "%targetfolder%" (
        echo cannot create target folder  "%targetfolder%\"
        echo as there exists another file "%targetfolder%" of the same name
        exit /B 3
    ) else (
        echo going to create target folder "%targetfolder%\"
        md "%targetfolder%"
    )
)
rem /L switch: do not copy but merely display items that would be copied
xcopy /E /C /H /R /L "%sourcefolder%\*" "%targetfolder%\"
rem remove /L switch from above line while debugged
exit /B 0

Output (note that initial copy and md commands are only for illustration that commonly used simplified md "D:\test\fooDstFldr\" 2>NUL could not suffice)

==> copy nul "D:\test\fooDstFldr"
        1 file(s) copied.

==> md "D:\test\fooDstFldr\"
A subdirectory or file D:\test\fooDstFldr\ already exists

==> D:\bat\SO\33603488.bat
source folder "D:\test\fooSrcFldr\" does not exist

==> md "D:\test\fooSrcFldr\"

==> D:\bat\SO\33603488.bat
source folder "D:\test\fooSrcFldr\" is empty

==> type nul>"D:\test\fooSrcFldr\foo.txt"

==> D:\bat\SO\33603488.bat
cannot create target folder  "D:\test\fooDstFldr\"
as there exists another file "D:\test\fooDstFldr" of the same name

==> del "D:\test\fooDstFldr"

==> D:\bat\SO\33603488.bat
going to create target folder "D:\test\fooDstFldr\"
D:\test\fooSrcFldr\foo.txt
1 File(s)

==>
@echo off
setlocal
set _folder="YOUR PATH TO FOLDER HERE\FOLDER NAME"
for /F %%A in ('dir /b /a %_folder%') Do (
    echo The folder is NOT empty
    pause >nul
    goto :ok
)
echo The folder is empty
:ok
exit

The script above will scan all files in the folder, which may be slow if there are many thousands of files, but it does not recursively scan through all the subfolders, if a single subfolder is found, empty or not, that is read as the parent folder is not empty.

To determine if a folder exists at all:

set _folder="YOUR PATH TO FOLDER HERE\FOLDER NAME"
if not exist %_folder% (Echo The folder does not exist)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM