[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2016-10-09
6
Medium Priority
?
137 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 37

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 71

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 25

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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