Solved

Move dirs batch MS-DOS program

Posted on 2014-04-01
13
536 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 33

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 69

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 69

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 69

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 33

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 69

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 33

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
Modify a small python script 19 108
Powershell pass array of objects to another script 8 61
Coding yesterday's date 3 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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