Solved

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

Posted on 2016-10-09
6
59 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 17

Expert Comment

by:Pawan Kumar Khowal
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 68

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 23

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 68

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now