Solved

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

Posted on 2016-10-09
6
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

749 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