onlyforbopi
1/4/2017 - 8:55 PM

DOS_FILE_DIR

DOS_FILE_DIR

: ZIP FILES ON LOCAL DIRECTORY (TEST)
ECHO.
ECHO ZIP/RAR FILE : LD%mydate% to LD%mydate%.rar
ECHO.
REM C:\Users\P.Doulgeridis\rar.exe a -ep1 -r %dir%\LD%mydate%.rar %dir%\LD%mydate%
REM rar a -r %dir%\LD%mydate%.rar %dir%\LD%mydate%

REM DEBUG - OK
C:\Users\P.Doulgeridis\rar.exe a -ep1 -r "%dirfull%.rar" "%dirfull%"
REM rar.exe a -ep1 -r "C:\Users\P.Doulgeridis\Desktop\Transfer.rar" "C:\Users\P.Doulgeridis\Desktop\Transfer\"
REM C:\Progra~1\WinRAR\WinRAR.exe a -ep1 -r "%dirfull%.rar" "%dirfull%"
IF ERRORLEVEL 1  (
	CALL:error10 "206" "UNABLE TO ZIP FILE %dirfull%"
	GOTO EOF
) ELSE (
	ECHO ZIP OF FILE SUCCESFUL FROM %dirfull% TO %dirfull%.rar
)
	ECHO.

::::
::
DIR <directory>
DIR <\path\to\directory>
DIR <\path\to\directory\>

:: Output in file
DIR <directory> > <FILE_OT>
DIR <\path\to\directory> > <FILE_OT>
DIR <\path\to\directory\> > <FILE_OT>
: SET THE FILE INTO A VARIABLE
set sourcefile=C:\path\to\file1\file.txt

:: CHECK FOR EXISTENCE OF FILE
:: IF FILE DOESNT EXIST WRAPPER FUNCTION ERROR IS CALLED
ECHO CHECKING FOR EXISTENCE OF ORIGIN DRIVE ... 
IF NOT EXIST %sourcefile% (
	CALL:error10 "98" "UNABLE TO LOCATE SOURCE DRIVE %sourcefile% "
	GOTO EOF
) ELSE (
	ECHO SOURCE FILE %sourcefile% LOCATED SUCCESFULLY
)
	ECHO.
: SET THE DRIVE INTO A VARIABLE
set sourcedrive=C:\

:: CHECK FOR EXISTENCE OF FILE
:: IF DRIVE DOESNT EXIST WRAPPER FUNCTION ERROR IS CALLED
ECHO CHECKING FOR EXISTENCE OF ORIGIN DRIVE ... 
IF NOT EXIST %sourcedrive% (
	CALL:error10 "98" "UNABLE TO LOCATE SOURCE DRIVE %sourcedrive% "
	GOTO EOF
) ELSE (
	ECHO SOURCE FILE %sourcedrive% LOCATED SUCCESFULLY
)
	ECHO.
: SET THE DIRECTORY INTO A VARIABLE
set sourcedir=C:\path\to\dir

:: CHECK FOR EXISTENCE OF DIRECTORY
:: IF DIRECTORY DOESNT EXIST WRAPPER FUNCTION ERROR IS CALLED
ECHO CHECKING FOR EXISTENCE OF ORIGIN DRIVE ... 
IF NOT EXIST %sourcedir% (
	CALL:error10 "98" "UNABLE TO LOCATE SOURCE DRIVE %sourcedir% "
	GOTO EOF
) ELSE (
	ECHO SOURCE DIRECTORY %sourcedir% LOCATED SUCCESFULLY
)
	ECHO.
: Moves files and renames files and directories.
:: To move one or more files:

MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

  [drive:][path]filename1 Specifies the location and name of the file
                          or files you want to move.
  destination             Specifies the new location of the file. Destination
                          can consist of a drive letter and colon, a
                          directory name, or a combination. If you are moving
                          only one file, you can also include a filename if
                          you want to rename the file when you move it.
  [drive:][path]dirname1  Specifies the directory you want to rename.
  dirname2                Specifies the new name of the directory.

  /Y                      Suppresses prompting to confirm you want to
                          overwrite an existing destination file.
  /-Y                     Causes prompting to confirm you want to overwrite
                          an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.



:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z]
                           [/EXCLUDE:file1[+file2][+file3]...]

  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  For
               example, specifying a string like \obj\ or .obj will exclude
               all files underneath the directory obj or all files with the
               .obj extension respectively.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Copies one or more files to another location.

COPY [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
     [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

  source       Specifies the file or files to be copied.
  /A           Indicates an ASCII text file.
  /B           Indicates a binary file.
  destination  Specifies the directory and/or filename for the new file(s).
  /V           Verifies that new files are written correctly.
  /N           Uses short filename, if available, when copying a file with a
               non-8dot3 name.
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.

To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).
1. DOS_Drive_Exists              : Check if drive exists
2. DOS_Dir_Exists                : Check if directory exists
3. DOS_File_Exists               : Check if file exists
4. DOS_Remote_Exists             : Check if remote shared directory exists
5. DOS_zip_File                  : Zips a file or dir
6. DOS_list_dir                  : List a directory
7. DOS_copy_xcopy_move           : Copy / Xcopy / Move listing
: SET THE FILE INTO A VARIABLE
set sourcefile=\\10.10.10.10\path\to\file1\file.txt

:: CHECK FOR EXISTENCE OF FILE
:: IF FILE DOESNT EXIST WRAPPER FUNCTION ERROR IS CALLED
ECHO CHECKING FOR EXISTENCE OF ORIGIN DRIVE ... 
IF NOT EXIST %sourcefile% (
	CALL:error10 "98" "UNABLE TO LOCATE SOURCE DRIVE %sourcefile% "
	GOTO EOF
) ELSE (
	ECHO SOURCE FILE %sourcefile% LOCATED SUCCESFULLY
)
	ECHO.

:: CHECK IF CONTROL DIR HAS ANY FILES ( at least one )
dir /a-d "%CONTROLDIR%*" >nul 2>nul || (
    echo No files to process
	goto :nofiles
    exit /b
)

ECHO CONTAINS FILES
python C:\Users\p.doulgeridis\Desktop\BATCH_FOLDERCHECKS\MailFwteinis.py




:nofiles
:::::::
::1st way (untested - external script)

@Echo off
Setlocal
if {%1}=={} set _empty=Syntax: %0 "" &goto :message

::Does folder exist
if not exist %1 (
  set _empty=No Such Folder
  goto :message
)

:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b %1') do set _TMP=%%a

IF {%_TMP%}=={} (
  set _empty=Empty
) ELSE (
  set _empty=Not Empty
)

:message
echo:
Echo %_empty%
echo:
Endlocal & set _empty=%_empty%


::
:: 2nd way (tested)


( dir /b /a "%dir%" | findstr . ) > nul && (
  echo %dir% non-empty
) || (
  echo %dir% empty
)
@echo off



SET CONTROLDIR=C:\Users\p.doulgeridis\Desktop\TESTFILECHECK\
SET EXPECTED=1


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: CHECK IF FOLDER IS EMPTY OR NON EMPTY
:: IF WE WANT DIR DETECTION WE HAVE TO CHANGE DIR TO DIR /b ONLY

for /F %%i in ('dir /b /a-d "%CONTROLDIR%*.*"') do (
   echo Folder is NON empty
   goto :AFTERCHECK
)

echo Folder is empty or does not exist
CALL:error10 "303" "UNABLE TO COPY FILES "
GOTO EOF


:AFTERCHECK
ECHO FILECHECK SUCCESFUL. FILES DETECTED IN FOLDER.




::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: COUNTING NUMBER OF FILES IN DIR AND CHECKING
ECHO SECOND CHECK
PAUSE


REM for /f %%A in ('dir /b "%CONTROLDIR%*.bz2" ^| find "File(s)"') do set cnt=%%A
REM echo File count = %cnt%


set count=0
for %%x in (%CONTROLDIR%*.*) do set /a count+=1
echo %count%


IF %count% NEQ %EXPECTED% (
	ECHO ELEGXOS STON ARITHMO TON ARXEIWN APETYXE.
	CALL:error10 "181" "FILE NUMBER MISMATCH. TERMINATING."
	EXIT /B 10
) else (
	ECHO ELEGXOS ARITHMOU ARXEIWN EPITYXIS!
	ECHO VRETHIKAN %count% ARXEIA STO FOLDER.
)

PAUSE	




PAUSE
GOTO EOF

@echo off
cls
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" file.txt | find /C ":""

for /f %%a in ('!cmd!') do set number=%%a
echo %number%
@ECHO Off
SetLocal DisableDelayedExpansion

Set "SrcFile=%1"

ECHO Removing last line on file: %SrcFile%

If Not Exist "%SrcFile%" Exit /B
Copy /Y "%SrcFile%" "%SrcFile%.bak">Nul 2>&1||Exit /B

(   Set "Line="
    For /F "UseBackQ Delims=" %%A In ("%SrcFile%.bak") Do (
        SetLocal EnableDelayedExpansion
        If Defined Line Echo !Line!
        EndLocal
        Set "Line=%%A"))>"%SrcFile%"
EndLocal
Exit /B