Skip to content

Instantly share code, notes, and snippets.

@scheib
Created September 16, 2010 05:58
Show Gist options
  • Save scheib/582036 to your computer and use it in GitHub Desktop.
Save scheib/582036 to your computer and use it in GitHub Desktop.
@echo off
call :GOSUB__IS_A_DIR %1
if errorlevel 1 goto ERROR_not_found
set path_backup=%path%
set path=%1;%path%
echo Updated path. Backed up old path to path_backup.
goto END
:==ERROR_not_found
echo.
echo. Could not find directory:
echo. %1
echo.
echo. doing nothing.
echo.
goto END
:==GOSUB__IS_A_DIR
REM INPUT %1
REM OUTPUT errorlevel == 1 if input is not a dir
if (%1)==() exit /b 1
pushd "%~1" 2> nul
if errorlevel 1 exit /b 1
popd
exit /b 0
:END
@echo off
:
: Place this file in your SendTo folder
: Right click a file or directory, and send to this .bat
: A cmd window will be opened in the directory of that file.
:
if (%1)==() goto ERROR
call :GOSUB__IS_A_DIR %1
if errorlevel 1 (
start cmd /K cd /D "%~d1%~p1"
) ELSE (
start cmd /K cd /D "%~f1"
)
goto END
:==ERROR
echo.
echo. Designed for a "Send To"
echo. expected an argument of a file or directory
goto END
:==GOSUB__IS_A_DIR
REM INPUT %1
REM OUTPUT errorlevel == 1 if input is not a dir
if (%1)==() exit /b 1
pushd "%~1" 2> nul
if errorlevel 1 exit /b 1
popd
exit /b 0
:END
#use File::Copy;
print "Copies a list of files from a dir to another.\n";
print "\n";
print "Usage\n";
print " sourcefilelist.txt source_dir dest_dir\n";
print "\n";
print " where sourcefilelist.txt contains a relative filename per line.\n";
print "\n";
print " e.g. to copy c:\\test\\subdir\\file.txt to d:\\output\\subdir\\file.txt\n";
print " place 'subdir\\file.txt' into t.txt.\n";
print " call this script with arguments 't.txt c:\\test d:\\output'.\n";
print "\n";
$filelist_filename = $ARGV[0];
$srcdir_filename = $ARGV[1];
$dstdir_filename = $ARGV[2];
# Check input args
# does file list exist
open(filelist_file, "< $filelist_filename") or die "no file $filelist_filename : $!";
# does file source dir exist
opendir (srcdir_handle, "$srcdir_filename") or die "no dir $srcdir_filename : $!";
while( <filelist_file> )
{
chop;
$filetobecopied = "$srcdir_filename/$_";
$newfile = "$dstdir_filename/$_";
# flip all slashes to backslashes
$filetobecopied =~ tr|/|\\|;
$newfile =~ tr|/|\\|;
# remove the file name from dest file, just get the path
($newfilepath) = $newfile =~ m|(.*\\)|;
print "\n";
system("echo xcopy $filetobecopied $newfilepath");
system("xcopy $filetobecopied $newfilepath");
}
@echo off
if (%1)==() goto ERROR_USAGE
if not exist "%~1" exit /B 1
if not exist "%~1\" exit /B 1
exit /B 0
:== ERROR_USAGE
echo.
echo. %0
echo. usage:
echo.
echo. Call with a path name.
echo. If it is a directory, the ERRORLEVEL will be left 0
echo. If it is a file, ERRORLEVEL will be 1
echo. If it does not exist, ERRORLEVEL will be 1
pause
exit /B
@echo off
if (%1)==() goto ERROR_USAGE
if not exist "%~1" exit /B 1
if exist "%~1\" exit /B 1
exit /B 0
:== ERROR_USAGE
echo.
echo. %0
echo. usage:
echo.
echo. Call with a path name.
echo. If it is a file, ERRORLEVEL will be 0
echo. If it is a directory, the ERRORLEVEL will be 1
echo. If it does not exist, ERRORLEVEL will be 1
pause
exit /B
@echo off
if (%1)==() goto ERROR_USAGE
call isadirectory.bat %1
if ERRORLEVEL 1 exit /B 1
dir /a-d /s /b "%~1" > nul 2>&1
if errorlevel 1 exit /B 0
exit /B 1
:== ERROR_USAGE
echo.
echo. %0
echo. usage:
echo.
echo. Call with a path name.
echo. If it is a directory, and it is empty, the ERRORLEVEL will be left 0
echo. If it is not empty, the ERRORLEVEL will be 1
echo. If it is a file, ERRORLEVEL will be 1
echo. If it does not exist, ERRORLEVEL will be 1
pause
exit /B
@echo off
echo.
echo. This will DELETE DIRECTORIES on your hard drive that are empty
echo.
echo. Call this batch file either
echo. - from the directory you want to clean recursively
echo. - with the directory name specified as the first parameter
echo.
echo. Directories to be deleted will be added to list in a temporary file:
echo. %TMP%\toremovedir.txt
echo.
echo. You will be prompted before the list is processed for delete.
echo.
pause
: clear list
echo.> %TMP%\toremovedir.txt
: search for files to add to list
echo.
if (%1)==() (
set ROOTDIR=.
) else (
set ROOTDIR="%~f1"
)
for /r %ROOTDIR% %%X in (.) do call :GOSUB_CHECKDIR "%%~fX"
echo.
echo. The following directories were found empty and will be deleted
echo. ----------------------------------------------------------------------
type %TMP%\toremovedir.txt
echo. ----------------------------------------------------------------------
echo.
echo. Ready to launch the text file for review / editing
echo. Any changes to the file will be used after you confirm.
echo.
pause
%TMP%\toremovedir.txt
:CONFIRM
echo.
echo. Are you certain you wish to remove the directories?
echo. Close this shell, window, or press CTRL-C to quit.
set RESPONSE=preset-as-no
set /p RESPONSE=Enter 'yes' to confirm delete:
if (%RESPONSE%)==(no) goto CLEANUP
if NOT (%RESPONSE%)==(yes) goto CONFIRM
: delete directories in list
echo.
: sort list first to have sub directories deleted first. avoids errors.
sort /r %TMP%\toremovedir.txt /o %TMP%\toremovedir.txt
for /F "delims=" %%X in (%TMP%\toremovedir.txt) do call :GOSUB_DELETEDIR %%X
:CLEANUP
echo. For your reference, you may wish to copy the file just used:
echo. %TMP%\toremovedir.txt
echo.
echo done.
pause
exit /b
:GOSUB_CHECKDIR
echo checking: %1
call isemptydirectory.bat %1
if ERRORLEVEL 1 exit /b
echo will delete: %~f1
echo %1 >> %TMP%\toremovedir.txt
exit /b
:GOSUB_DELETEDIR
echo DELETING %1
rmdir /s /q %1
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment