New
#11
Good Morning Pyprohly,
Thank you very much for your reply. I couldn't make it work. I am sure I am doing something wrong and I got only an empty text file as the result. I am definitely not a scripting Guru . The thing is I did not want to disturb you therefore I tried to figure it out on the weekend where shall I have to paste your code but without success. I would like to the send the output of the batch script to M:\Pictures\escalation_monika_juhasz Can you please paste your redirection code into the batch script?
Under the date stamp I meant I would like to save the output of the batch script under the name of the current date. For example: 16.11.2015.txt or something like this. Thank you very much for your time and response and have a nice day. :)
:: CopyMoveFromList.bat TextFile [Mode]
@echo off
setlocal EnableDelayedExpansion
REM Synopsis.
REM Given a list of filenames, search for files with these names starting from the current
REM directory. Begin by dropping a text file, containing a list of filenames, onto this script.
::
set "output_folder=.\escalation_monika_juhasz"
set "default_mode=MOVE" &rem Valid modes: COPY, MOVE, DELETE, LIST
set "include_folders=TRUE"
set "silence_errors=FALSE"
::
goto :main
:copy ItemName OutFolder
setlocal
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
xcopy "%~1" "%~2\%~1" /e/c/i/q/h/r/k/y >NUL 2>&1 && (echo Moved folder '%~f1'& set success=1) || (echo Error moving folder '%~f1')
) else (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (
copy "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
) else (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' dir /a/b/s "%~1" ') do (
if exist "%%~J"\* (
xcopy "%%~J" "%~2\%~1" /e/c/i/q/h/r/k/y >NUL 2>&1 && (echo Moved folder '%%~fJ'& set success=1) || (echo Error moving folder '%%~fJ')
) else (
copy "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
) else (
for /f "delims=" %%J in (' dir /a:-d/b/s "%~1" ') do (
copy "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
)
endlocal & if "%SUCCESS%"=="1" set /a counter+=1
goto :eof
:move ItemName OutFolder
setlocal
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
move "%~1" "%~2\%~1" >NUL 2>&1 && (echo Moved folder '%~f1'& set success=1) || (echo Error moving folder '%~f1')
) else (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (
move "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
) else (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' dir /a/b/s "%~1" ') do (
if exist "%%~J"\* (
move "%%~J" "%~2\%~1" >NUL 2>&1 && (echo Moved folder '%%~fJ'& set success=1) || (echo Error moving folder '%%~fJ')
) else (
move "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
) else (
for /f "delims=" %%J in (' dir /a:-d/b/s "%~1" ') do (
move "%%~J" "%~2" >NUL 2>&1 && (echo Moved file '%%~fJ'& set success=1) || (echo Error moving file '%%~fJ')
)
)
)
endlocal & if "%SUCCESS%"=="1" set /a counter+=1
goto :eof
:delete ItemName
setlocal
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
rd "%~1" /s /q && (echo Removed folder '%~f1'& set success=1) || (echo Error removing folder '%~f1')
) else (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (
del "%%~J" /f /q && (echo Deleted file '%%~fJ'& set success=1) || (echo Error deleting file '%%~fJ')
)
)
) else (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' dir /a/b/s "%~1" ') do (
if exist "%%~J"\* (
rd "%%~J" /s /q && (echo Removed folder '%%~fJ'& set success=1) || (echo Error removing folder '%%~fJ')
) else (
del "%%~J" /f /q && (echo Deleted file '%%~fJ'& set success=1) || (echo Error deleting file '%%~fJ')
)
)
) else (
for /f "delims=" %%J in (' dir /a:-d/b/s "%~1" ') do (
del "%%~J" /f /q && (echo Deleted file '%%~fJ'& set success=1) || (echo Error deleting file '%%~fJ')
)
)
)
endlocal & if "%SUCCESS%"=="1" set /a counter+=1
goto :eof
:list ItemName
setlocal
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
echo %~f1&& set success=1
) else (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (
echo %%~fJ&& set success=1
)
)
) else (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' dir /a/b/s "%~1" ') do (
echo %%~fJ&& set success=1
)
) else (
for /f "delims=" %%J in (' dir /a:-d/b/s "%~1" ') do (
echo %%~fJ&& set success=1
)
)
)
endlocal & if "%SUCCESS%"=="1" set /a counter+=1
goto :eof
:item_not_found_error Str
setlocal
if /i "%SILENCE_ERRORS%"=="TRUE" goto :eof
echo Item '%~1' could not be located
endlocal
goto :eof
:mulitple_instances_error FileName
setlocal
if /i "%SILENCE_ERRORS%"=="TRUE" goto :eof
echo Mulitple instances of '%~nx1' found:
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c echo @PATH" ') do (echo '%%~J')
) else (
for /f "delims=" %%J in (' forfiles /m "%~1" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (echo '%%~J')
)
) else (
for /f "delims=" %%J in (' dir /a/b/s "%~1" ') do (echo '%%~J')
)
if /i "%MODE%"=="COPY" echo None were copied
if /i "%MODE%"=="MOVE" echo None were moved
if /i "%MODE%"=="DELETE" echo None were deleted
endlocal
goto :eof
:main
if not "%~3"=="" echo '%~nx0' takes 1 to 2 arguments&& exit /b 1
if "%~1"=="" echo '%~nx0' missing 1 required argument&& exit /b 1
if not exist "%~1" echo Input file not found&& exit /b 1
if not "%~2"=="" (set "mode=%~2") else set "mode=%DEFAULT_MODE%"
set "valid_modes=COPY, MOVE, DELETE, LIST"
for %%I in (%VALID_MODES%) do if /i "%%~I"=="%MODE%" set "mode_is_valid=TRUE"
if /i not "%MODE_IS_VALID%"=="TRUE" (
if not "%MODE%"=="" set/p=Unknown mode '%MODE%'. <NUL
set/p=Vaild modes are: <NUL
for %%I in (%VALID_MODES%) do set/p=%%I<NUL& goto :break
:break
set "need_comma=1"
for %%I in (%VALID_MODES%) do (if defined NEED_COMMA (set "need_comma=") else (set/p=, %%I<NUL))
echo.& exit /b 1
)
set "out_folder=!OUTPUT_FOLDER!?"
set "out_folder=!OUT_FOLDER:"=!"
if "%OUT_FOLDER%"=="?" (set "out_folder=%~n0") else set "out_folder=!OUT_FOLDER:~0,-1!"
for %%I in ("!OUT_FOLDER!") do set "out_folder=%%~fI"
if not exist "!OUT_FOLDER!"\* if /i not "%MODE%"=="LIST" md "!OUT_FOLDER!" 2>NUL || echo escalation_monika_juhasz could not be found or created&& exit /b 1
if /i "%MODE%"=="DELETE" echo.& set/p=Warning: The selected mode will permanently delete items. Are you sure you want to continue?<NUL& choice
if errorlevel 2 exit /b
set "tempfile=%TEMP%\tmp%RANDOM%_%~n0__#.txt"
if /i "%MODE%"=="LIST" (
set "tmp_found=>>"!TEMPFILE:_#=FOUND!""& copy NUL "!TEMPFILE:_#=FOUND!" >NUL
set "tmp_none=>>"!TEMPFILE:_#=NONE!""& copy NUL "!TEMPFILE:_#=NONE!" >NUL
set "tmp_multi=>>"!TEMPFILE:_#=MULTI!""& copy NUL "!TEMPFILE:_#=MULTI!" >NUL
) else (set "tmp_none="& set "tmp_multi=")
set /a counter=0
for /f "useback delims=" %%I in ("%~1") do (
if exist "%%~I"\* (set "dir_here=TRUE") else (set "dir_here=")
if defined DIR_HERE (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' forfiles /m "%%~I" /s /c "cmd /c echo @PATH" ^| find /c /v "" ') do (set /a inst=%%J-1)
) else (
for /f "delims=" %%J in (' forfiles /m "%%~I" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ^| find /c /v "" ') do (set /a inst=%%J-1)
)
if !INST! equ 0 call :item_not_found_error %%I %TMP_NONE%
if !INST! gtr 1 call :mulitple_instances_error %%I %TMP_MULTI%
if !INST! equ 1 (
if /i "%MODE%"=="COPY" call :copy %%I "!OUT_FOLDER!"
if /i "%MODE%"=="MOVE" call :move %%I "!OUT_FOLDER!"
if /i "%MODE%"=="DELETE" call :delete %%I
if /i "%MODE%"=="LIST" call :list %%I
) %TMP_FOUND%
) else (
if /i "%INCLUDE_FOLDERS%"=="TRUE" (
for /f "delims=" %%J in (' dir /a/b/s "%%~I" 2^>NUL ^| find /c /v "" ') do (set /a inst=%%J)
) else (
for /f "delims=" %%J in (' dir /a:-d/b/s "%%~I" 2^>NUL ^| find /c /v "" ') do (set /a inst=%%J)
)
if !INST! equ 0 call :item_not_found_error %%I %TMP_NONE%
if !INST! gtr 1 call :mulitple_instances_error %%I %TMP_MULTI%
if !INST! equ 1 (
if /i "%MODE%"=="COPY" call :copy %%I "!OUT_FOLDER!"
if /i "%MODE%"=="MOVE" call :move %%I "!OUT_FOLDER!"
if /i "%MODE%"=="DELETE" call :delete %%I
if /i "%MODE%"=="LIST" call :list %%I
) %TMP_FOUND%
)
)
if /i "%MODE%"=="LIST" (
for %%J in ("%TEMPFILE:_#=FOUND%") do if not "%%~zJ"=="0" (
echo ITEMS FOUND:
type "%TEMPFILE:_#=FOUND%"
)
del "%TEMPFILE:_#=FOUND%"
for %%J in ("%TEMPFILE:_#=NONE%") do if not "%%~zJ"=="0" (
echo.& echo ITEMS NOT FOUND:
type "%TEMPFILE:_#=NONE%"
)
del "%TEMPFILE:_#=NONE%"
for %%J in ("%TEMPFILE:_#=MULTI%") do if not "%%~zJ"=="0" (
echo.& echo ITEMS OCCURING MORE THAN ONCE:
type "%TEMPFILE:_#=MULTI%"
)
del "%TEMPFILE:_#=MULTI%"
)
echo.& if /i "%MODE%"=="COPY" echo %COUNTER% items were copied to directory '!OUT_FOLDER!'
if /i "%MODE%"=="MOVE" echo %COUNTER% items were moved to directory '!OUT_FOLDER!'
if /i "%MODE%"=="DELETE" echo %COUNTER% items were deleted
if /i "%MODE%"=="LIST" echo %COUNTER% items were foundECHO.
pause
GOTO:EOF