?
Solved

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

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

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 70

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
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.

 

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 70

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

762 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