Solved

Move dirs batch MS-DOS program

Posted on 2014-04-01
13
539 Views
Last Modified: 2014-04-03
Hello I have this code called massive.bat:

@ECHO OFF
goto :EndOfRemarks

--- remarks section ---
desired process:
  loop
    wait 2 minutes
    select directory (if exists) from receiver
    move selected directory to results
  end loop
--- end remarks section ---

:EndOfRemarks

SETLOCAL ENABLEDELAYEDEXPANSION

SET BaseDir="C:\task\receiver"
SET DestDir="C:\task\results"
SET resultDir=
SET resultDirrace=
SET resultDirborra=

FOR /D %%F IN (%BaseDir%\*.*)  DO (
  TIMEOUT /T 2 /NOBREAK

  SET resultDir="%%F"
  SET resultDirborra=%%F
  SET resultDirrace="%%~nxF"

  MOVE /Y !resultDir! %DestDir%

REM DEL !resultDirborra!\*.* /S /F /Q

  timeout /T 1 /nobreak

REM  rd /s /q !resultDir!


  ECHO ... Directory moved:  !resultDirrace!


)

REM explicitly end local changes caused by script (optional)
REM done implicitly at end of script if not specified
ENDLOCAL
massive.bat

Open in new window


There we have directories from one dir "receiver" and are moved to "results" now the question is in the same directory we have 2 files called "secure1" and "secure2" now what is needed to modify in the batch program to move all directories like actually it is working but that ignor the 2 files "secure1" and "secure2" in other words no try to move the "secure1" and "secure2" files leave in the direcotry and not copy anything also to the "results" directory. Of course the variable " !resultDirrace!" must work like it is now giving the name of the directory moved. If sometimes we have directories with sub directories and contents are not moved is there a way if we have one directory with sub directories inside we can move all?
Thank you
0
Comment
Question by:coerrace
[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
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 39971586
I do not exactly know what you want!

But give my below code a try...

@ECHO OFF
goto :EndOfRemarks

--- remarks section ---
desired process:
  loop
    wait 2 minutes
    select directory (if exists) from receiver
    move selected directory to results
  end loop
--- end remarks section ---

:EndOfRemarks

SETLOCAL ENABLEDELAYEDEXPANSION

SET BaseDir="C:\task\receiver"
SET DestDir="C:\task\results"
SET resultDir=
SET resultDirrace=
SET resultDirborra=

FOR /D %%F IN (%BaseDir%\*.*)  DO (
  TIMEOUT /T 2 /NOBREAK

  SET resultDir="%%F"
  SET resultDirborra=%%F
  SET resultDirrace="%%~nxF"

  IF NOT EXIST !resultDirborra!\secure1 (
    IF NOT EXIST !resultDirborra!\secure2 (

  MOVE /Y !resultDir! %DestDir%

REM DEL !resultDirborra!\*.* /S /F /Q

  timeout /T 1 /nobreak

REM  rd /s /q !resultDir!


  ECHO ... Directory moved:  !resultDirrace!

    )
  )
)

Open in new window

0
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 39972164
if you want to move all subdirectories but not files you could change the for command into

FOR /F "delims=" %%F IN ('DIR /B /AD %BaseDir%') DO (

Open in new window


the "delims=" makes to ignore spaces in folder names. the /B makes listing 1 row for each folder and the /AD makes list of directories only.

Sara
0
 

Author Comment

by:coerrace
ID: 39972250
ok and to avoid move the 2 files "secure1" and "secure2?
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!

 
LVL 19

Expert Comment

by:Thommy
ID: 39972595
You should use ROBOCOPY
It has a MOVE switch and also full functionality to exclude specific files and folders from being moved...

For Windows XP you have to install Windows Server 2003 Resource Kit Tools

For Windows 7 and Windows 8 it is already part of the system.

For help simply type "ROBOCOPY /?"
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39972848
I'm not certain I understood your request. Do you want to stop moving folders if both (or one of) the files are found in the top-level folder? Or are you trying to move everything but those two files?
0
 

Author Comment

by:coerrace
ID: 39972972
Yes I just miss the part where in the same directory of the directories to move we have 2 files called "secure1" and "secure2" the source directory is like I said  C:\task\receiver then when all the directories inside C:\task\receiver will be copied with all sub directories to C:\task\results the files "secure1" and "secure2" must stay in C:\task\receiver in other words will never be copied that 2 files to C:\task\receiver the rest all. For example

Initial directory  C:\task\results has this:

directory1-filled with sub directories and files
directory2-emtpy directory
directory3-filled with sub directories and files
secure1-file the script must not touch never must stay here in C:\task\results
secure2-file the script must not touch never must stay here in C:\task\results

And so on could be more directories filled or not then after run the script the results must be all directories copied to C:\task\results except the files "secure1" and "secure2". The results in C:\task\results must be then:

directory1-filled with sub directories and files
directory2-emtpy directory
directory3-filled with sub directories and files

Like you see in the results direcotry the files "secure1" and "secure2" were not copied, however the directory1,directory2,directory3 were copied like it was with all the files and sub directories like the original.
And if you note the variable "!resultDirrace!" that we use in "ECHO ... Directory moved: " !resultDirrace!" will store like it is now the actual directory moved and so on for all that variable will change according the actual directory is moving.
Thank you
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39973778
Well, your script is not touching the files in C:\task\receiver, only folders. Do you want to test the secure1 and secure2 files in each subfolder, and leave them alone? According to your last response, the answer should be "no" - the files in basedir are not moved, and so both files stay as-is.
0
 

Author Comment

by:coerrace
ID: 39973788
Ok I was testing the oposite but you are right, just one more question do you know why some directories are not moved when have sub directories or files?
Thank you
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 39973794
If, for any reason, the file or folder cannot be moved, it will stay. Reasons might be permission issues, or read-only/hidden/system file attributes, or the files/folders might be in use currently. For example you will never succeed in moving the current folder.
0
 

Author Closing Comment

by:coerrace
ID: 39973908
Thank you
0
 
LVL 34

Expert Comment

by:sarabande
ID: 39974218
ok and to avoid move the 2 files "secure1" and "secure2?

if you use the 'DIR /B /AD %BaseDir%' as suggested, the files would be filtered and therefore would not be moved.

Sara
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39974343
There is no difference between
  FOR /D %%F in ("%BaseDir%\*.*")
and
  FOR /F "delims=" %%F in ('dir /B /AD "%BaseDir%" ')
so, technically, accepting http:#a39972164 is not correct, as it does not solve anything, it is just a different way.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 39974415
There is no difference between ...

FOR /D %F in ("%BaseDir%\*.*") do echo %F
d:\temp\abc 
d:\temp\build
d:\temp\xyz

for /f "delims=" %f in ('dir /B /AD %BaseDir%') do echo %f
abc
build 
xyz

Open in new window


it is a difference. both commands list subfolders of %BaseDir% but the second only the names. it is true that files were not moved with either method and of course names can be extracted from paths without problems.

i didn't check the 'for /D' cause i thought the files issue was a real issue with the current solution. sorry for that.

Sara
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

623 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