Solved

How to Skip in FOR command if a Directory does not exist.

Posted on 2016-10-09
6
85 Views
Last Modified: 2016-10-09
Scenario:
\\Server\Folder\User1\Downloads
\\Server\Folder\User2\Downloads
\\Server\Folder\Apps
\\Server\Folder\Scripts


The weekly script
1) Wipes out anything in .\Downloads\Pending_deletion
2) Then moves files and directories from Downloads\ and moves it to Downloads\Pending_deletion
Note: This can only run on users directory that contain a Downloads folder

I am looking to skip the non user directories by using
 If EXIST \\Server\Folder\[username]\Downloads DO (
But it's not working as expected. I will still run even if I change it to If EXIST \\Server\Folder\[username]\blahblah DO (

Here is script

echo off
Set root=\\Server\Folder
set users=\\Server\Folder\temp\CLDrv_Users.txt

dir ab /b \\clouddrive\data\ >>%users%

For /F %%u in (%users%) DO (
IF Exist %root%\%%u\Downloads DO ( [NOTE- Runs even if not exist]
      RD /S /Q "%root%\%%u\Downloads\Pending_deletion"
      MD %root%\%%u\Downloads\Pending_deletion
      move "%root%\%%u\Downloads\*" "%root%\%%u\Downloads\Pending_deletion\"
      for /D %%d in (%root%\%%u\Downloads\*) do (
            move "%%d" "%root%\%%u\Downloads\Pending_deletion\"
              )      
      )
)
0
Comment
Question by:GGHC
6 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41836055
You can skip that using Continue keyword.

sample code..

for (int i = 0; i < 10; i++) {
    if(i == 0) {
        continue;
    }

    FunctionDoSomething(i);
}

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41836102
Pawan, it's a .cmd batch, and that does not know that syntax.

GGHC, there are several issues with the code you showed:
  1. What is dir ab /b \\clouddrive\data\ >>%users% supposed to do? In particular the ab part doesn't make sense to me.
  2. There is no construct like IF ... DO (. The DO is wrong.
  3. You are first moving files from Downloads into its subfolder Pending_Deletion, then move all subfolders of Downloads into Pending_Deletion - including that folder itself. This should result in an error message.

Usually checking a folder for existence the way you do works, but sometimes you have to use
IF Exist %root%\%%u\Downloads\ (
or
IF Exist %root%\%%u\Downloads\. (
1
 
LVL 24

Expert Comment

by:NVIT
ID: 41836124
In addition to Qlemo's comment, if the path has space chars, quoting the path may help...

IF Exist "%root%\%%u\Downloads"

Open in new window


> dir ab /b \\clouddrive\data\ >>%users% supposed to do?

Do you mean to filter the folder names i.e.
dir /ad /b \\clouddrive\data\ >>%users%

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:GGHC
ID: 41836134
Pawan,
Thanks. Good catches!
1. Yikes! accident :(   . It was suppose to  be  dir /b \\Server\Folder\ >>%users%
3. I couldn't find a clean way around that hurdle so I just swallowed that error for that entry.

Worked! > IF Exist %root%\%%u\Downloads\. (

Perhaps I Should try Robocopy to exclude folder in existing directory.... here is my draft

For /F %%u in (%users%) Do (
If Exist %root%\%%u\Downloads\. (
RD /S /Q "%root%\%%u\Downloads\Pending_deletion"
MD %root%\%%u\Pending_deletion
ROBOCOPY "%root%\%%u\Downloads\" "%root%\%%u\Downloads\Pending_deletion\" -XD Pending_deletion
)
)

How does that look ?
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41836147
That's one way, but looks a bit over the top. You also forgot to add /MOV or /MOVE.

The more simple solution is to first perform the subfolder move, and then the topmost files - just reverse the sequence. And let's improve more:
echo off
Set root=\\Server\Folder

For /F %%u in ('dir /a:d /b \\clouddrive\data\') do (
   IF Exist "%root%\%%u\Downloads\." (
      RD /S /Q "%root%\%%u\Downloads\Pending_deletion"
      MD %root%\%%u\Downloads\Pending_deletion
      for /D %%d in ("%root%\%%u\Downloads\*") do (
            move "%%~d" "%root%\%%u\Downloads\Pending_deletion\"
      )      
      move "%root%\%%u\Downloads\*" "%root%\%%u\Downloads\Pending_deletion\"
  )
)

Open in new window

1
 

Author Comment

by:GGHC
ID: 41836174
Nice Qlemo!!!!!
Thank you!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question