Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Move dirs batch MS-DOS program

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
coerrace
Asked:
coerrace
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
ThommyCommented:
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
 
sarabandeCommented:
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
 
coerraceAuthor Commented:
ok and to avoid move the 2 files "secure1" and "secure2?
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
ThommyCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
coerraceAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
coerraceAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
coerraceAuthor Commented:
Thank you
0
 
sarabandeCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
sarabandeCommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now