Solved

Move dirs batch MS-DOS program

Posted on 2014-04-01
13
533 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
  • 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 32

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

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 68

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 68

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 32

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 68

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 32

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now