Remove Admin Rights and Move instead of Copy | Batch file

Page 2 of 2 FirstFirst 12

  1. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #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
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #12

    Monikajuhasz, if you decide to repeat already mentioned code, please make an effort to highlight the parts you've edited, as I'm not a huge fan of spot-the-difference. And please surround all code in code tags (i.e., [CODE] and [/CODE]).

    monikajuhasz said:
    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.
    Indeed, you are doing something wrong: modifying my script is not the most efficient way to achieve what you are after. You shouldn't need to edit that batch file further, and adjusting it for your needs would in fact be quite tedious.

    The trick is to treat the batch file like a command. The command can be utilised by other scripts like any ordinary command you'd use.

    Here's what you should do... create a new script with lines similar to the below,
    Code:
    set "date_format=%DATE:~4,2%.%DATE:~7,2%.%DATE:~10,4%"
    CopyMoveFromList "C:\path\to\input_list.txt" LIST > "C:\Users\%USERNAME%\Desktop\Output-%DATE_FORMAT%.txt"
    And that's really about it.
    Last edited by Pyprohly; 17 Nov 2015 at 05:58.
      My Computer


  3. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #13

    Hi Pyprohly,

    Sorry for causing trouble. Thank you very much for all your help. It works perfectly :)
    Have a nice day.
    Monika
      My Computer


 
Page 2 of 2 FirstFirst 12

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:00.
Find Us