Reputation: 199
In trying to find a way to check for files in directories, I found this:
>nul 2>nul dir /a-d /s "folderName\*" && (echo Files exist) || (echo No files found)
It works well enough except that I really need to be able to check in only half of the sub-directories that follow a specific naming convention.
Is there anyway to rework this line to only check in the "ToBeMoved" folders?
I tried writing it this way:
>nul 2>nul dir /a-d/b/s "\\SERVER\Path\*ToBeMoved" && (echo Files exist) || (echo No files found)
But it doesn't work. I'm guessing because its looking for folders at the user level with the string "ToBeMoved".
Upvotes: 0
Views: 92
Reputation: 130919
I believe the first code you list is missing the /b
option. Assuming it is actually there, then your original code lists the full path of every file within the directory hierarchy, but you direct the output to null, and conditionally take action depending on whether file(s) were found or not.
Instead of redirecting to null, you can simply pipe to FIND
, logically looking for a path that contains *ToBeMoved\
. That output can be redirected to null, and you can conditionally take action based on the FIND
return code. I would ignore case when looking for the string.
dir /b /a-d /s "folderName" 2>nul|find /i "ToBeMoved\" >nul && (echo Files exist) || (echo No files found)
You could use FINDSTR
instead, but then the backslash must be escaped
dir /b /a-d /s "folderName" 2>nul|findstr /i "ToBeMoved\\" >nul && (echo Files exist) || (echo No files found)
Upvotes: 1
Reputation: 14330
Give this a try. Change the root path of where you need to be with the PUSHD command.
@echo off
PUSHD "H:\users"
FOR /F "delims=" %%G IN ('dir /ad /b /s ToBeMoved') DO (
pushd "%%~G"
>nul 2>nul dir /a-d/b/s && (echo Files exist in %%~G) || (echo No files found in %%~G)
popd
)
popd
pause
Output for me.
No files found in H:\users\johndoe\ToBeMoved
No files found in H:\users\maryjane\ToBeMoved
Files exist in H:\users\UserWithFiles\ToBeMoved
Press any key to continue . . .
EDIT: Just to show you that I do have a file in a folder named ToBeMoved I ran the dir cmd from the prompt. I only put one file in for testing.
H:\users>dir /a-d/b/s
H:\users\UserWithFiles\ToBeMoved\moveme.txt
Upvotes: 1