Remove Admin Rights and Move instead of Copy | Batch file

Page 1 of 2 12 LastLast

  1. Posts : 10
    Windows 7 Ultimate x64
       #1

    Remove Admin Rights and Move instead of Copy | Batch file


    I have a list of 1000 files names that I put in a text file named list.txt. Need to find them in more folders, sub folders which has 12000+ files in it. The below script works fine but I would like the Admin Right to be removed and also I want the files to be moved permanently into C:\your_files. Is there any one who could help me in this? Thank you in advance.

    @echo off
    REM (c) 2013 BY NEUTRON16 (https://www.sevenforums.com/members/neutron16.html)
    CLS
    TITLE Mass file finder by Neutron16
    REM finds files in list.txt file and copies them to C:\your_files
    REM CHECK FOR ADMIN RIGHTS
    COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
    IF ERRORLEVEL 1 GOTO:NONADMIN
    DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
    :ADMIN
    REM GOT ADMIN RIGHTS
    COLOR 1F
    ECHO Hi, %USERNAME%!
    ECHO Please wait...
    FOR /R "%~dp0" %%I IN (.) DO for /f "usebackq delims=" %%a in ("%~dp0list.txt") do echo d |xcopy "%%I\%%a" "C:\your_files" /e /i
    COLOR 2F
    ECHO.
    ECHO (c) 2013 by Neutron16 (https://www.sevenforums.com/members/neutron16.html)
    PAUSE
    GOTO:EOF
    :NONADMIN
    REM NO ADMIN RIGHTS
    COLOR 4F
    ECHO.
    ECHO PLEASE RUN AS ADMINISTRATOR
    ECHO.
    pause
    GOTO:EOF
      My Computer


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

    ​Hi Monikajuhasz,

    Neutron16 has signed his batch file as his work, and because of this, I therefore hesitate to help you modify it. I can, however, create a new solution completely from scratch for you.

    Take the below batch file for a spin instead. If you wish to move files instead of copy, you may replace the “copy” command with the “move” command.


    YYYY_script_V2.bat
    Code:
    :: 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=.\Output Folder"
    set "default_mode=COPY"    &rem Valid modes: COPY, MOVE, DELETE, LIST
    set "include_folders=TRUE"
    set "silence_errors=FALSE"
    set "adapt_current_directory=FALSE"
    ::
     
    goto :main
    :copy ItemFullName OutFolder
    setlocal
        for %%I in ("%~2") do ( call set "outfolder=%%~I" )
        for %%I in ("%~1") do (
            if not exist "%OUTFOLDER%" md "%OUTFOLDER%" 2>NUL || (echo Output folder '%OUTFOLDER%' could not be found or created& echo   Item '%%~I' not copied)&& exit /b 1
            if defined DIR_HERE (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    xcopy "%~1" "%OUTFOLDER%\%~nx1" /e/c/i/q/h/r/k/y >NUL 2>&1 && (echo Copied folder '%%~fI'& set success=1) || (echo Error copying folder '%%~fI')
                ) else (
                    copy "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Copied file '%%~fI'& set success=1) || (echo Error copying file '%%~fI')
                )
            ) else (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    if exist "%~1"\* (
                        xcopy "%~1" "%OUTFOLDER%\%~nx1" /e/c/i/q/h/r/k/y >NUL 2>&1 && (echo Copied folder '%%~fI'& set success=1) || (echo Error copying folder '%%~fI')
                    ) else (
                        copy "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Copied file '%%~fI'& set success=1) || (echo Error copying file '%%~fI')
                    )
                ) else (
                    copy "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Copied file '%%~fI'& set success=1) || (echo Error copying file '%%~fI')
                )
            )
        )
    endlocal & if "%SUCCESS%"=="1" set /a counter+=1
    goto :eof
     
    :move ItemName OutFolder
    setlocal
        for %%I in ("%~2") do ( call set "outfolder=%%~I" )
        for %%I in ("%~1") do (
            if not exist "%OUTFOLDER%" md "%OUTFOLDER%" 2>NUL || (echo Output folder '%OUTFOLDER%' could not be found or created& echo   Item '%%~I' not copied)&& exit /b 1
            if defined DIR_HERE (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    move "%~1" "%OUTFOLDER%\%~nx1" /e/c/i/q/h/r/k/y >NUL 2>&1 && (echo Moved folder '%%~fI'& set success=1) || (echo Error moving folder '%%~fI')
                ) else (
                    move "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Moved file '%%~fI'& set success=1) || (echo Error moving file '%%~fI')
                )
            ) else (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    if exist "%~1"\* (
                        move "%~1" "%OUTFOLDER%\%~nx1" >NUL 2>&1 && (echo Moved folder '%%~fI'& set success=1) || (echo Error moving folder '%%~fI')
                    ) else (
                        move "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Moved file '%%~fI'& set success=1) || (echo Error moving file '%%~fI')
                    )
                ) else (
                    move "%~1" "%OUTFOLDER%" >NUL 2>&1 && (echo Moved file '%%~fI'& set success=1) || (echo Error moving file '%%~fI')
                )
            )
        )
    endlocal & if "%SUCCESS%"=="1" set /a counter+=1
    goto :eof
     
    :delete ItemName
    setlocal
        for %%I in ("%~1") do (
            if defined DIR_HERE (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    rd "%~1" /s /q >NUL 2>&1 && (echo Removed folder '%%~fI'& set success=1) || (echo Error removing folder '%%~fI')
                ) else (
                    del "%~1" /f /q >NUL 2>&1 && (echo Deleted file '%%~fI'& set success=1) || (echo Error deleting file '%%~fI')
                )
            ) else (
                if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                    if exist "%~1"\* (
                        rd "%~1" /s /q >NUL 2>&1 && (echo Removed folder '%%~fI'& set success=1) || (echo Error removing folder '%%~fI')
                    ) else (
                        del "%~1" /f /q >NUL 2>&1 && (echo Deleted file '%%~fI'& set success=1) || (echo Error deleting file '%%~fI')
                    )
                ) else (
                    del "%~1" /f /q >NUL 2>&1 && (echo Deleted file '%%~fI'& set success=1) || (echo Error deleting file '%%~fI')
                )
            )
        )
    endlocal & if "%SUCCESS%"=="1" set /a counter+=1
    goto :eof
     
    :list ItemName
        echo %~f1
    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 '%~1' 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"
    set "mode_is_valid="
    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=%%~I"
     
    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 (
        set /a inst=0
        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" ') do (
                    set /a inst+=1
                    set "item=%%~J"
                    set "item_path=%%~dpJ"
                )
            ) else (
                for /f "delims=" %%J in (' forfiles /m "%%~I" /s /c "cmd /c if @ISDIR==FALSE echo @PATH" ') do (
                    set /a inst+=1
                    set "item=%%~J"
                    set "item_path=%%~dpJ"
                )
            )
            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 "%ADAPT_CURRENT_DIRECTORY%"=="TRUE" pushd !ITEM_PATH!
                if /i "%MODE%"=="COPY" call :copy "!ITEM!" "!OUT_FOLDER!"
                if /i "%MODE%"=="MOVE" call :move "!ITEM!" "!OUT_FOLDER!"
                if /i "%MODE%"=="DELETE" call :delete "!ITEM!"
                if /i "%MODE%"=="LIST" call :list "!ITEM!"
                if /i "%ADAPT_CURRENT_DIRECTORY%"=="TRUE" popd
            ) %TMP_FOUND%
        ) else (
            if /i "%INCLUDE_FOLDERS%"=="TRUE" (
                for /f "delims=" %%J in (' dir /a/b/s "%%~I" 2^>NUL ') do (
                    set /a inst+=1
                    set "item=%%~J"
                    set "item_path=%%~dpJ"
                )
            ) else (
                for /f "delims=" %%J in (' dir /a:-d/b/s "%%~I" 2^>NUL ') do (
                    set /a inst+=1
                    set "item=%%~J"
                    set "item_path=%%~dpJ"
                )
            )
            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 "%ADAPT_CURRENT_DIRECTORY%"=="TRUE" pushd !ITEM_PATH!
                if /i "%MODE%"=="COPY" call :copy "!ITEM!" "!OUT_FOLDER!"
                if /i "%MODE%"=="MOVE" call :move "!ITEM!" "!OUT_FOLDER!"
                if /i "%MODE%"=="DELETE" call :delete "!ITEM!"
                if /i "%MODE%"=="LIST" call :list "!ITEM!"
                if /i "%ADAPT_CURRENT_DIRECTORY%"=="TRUE" popd
            ) %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 found
    Note carefully that the first line of your newline separated list of filenames should now state the output location for your files.

    Feel free to post any feedback or suggestions regarding this script.


    Edit: (9/11/15) Updated script. Now supports copying folders. Added easy way to switch between copying and moving files, and added a delete files mode as well as a plain list-found-files mode. N.B. the output folder is now defined in the batch file, between the double colons, and not as the first line of the input list file.

    Edit 2: (20/11/15) Updated script. Added "adapt_current_directory" option, which causes relative paths to be relative from the path of the item currently being processed. And enabling this option should allow support for UNC paths. Also cleaned up sections of the script.

    Edit 3: (21/11/15) Fixed error message display issue with filenames containing spaces relating to 'item_not_found_error' function. Fixed wildcards evaluating in 'mulitple_instances_error' function.
    Last edited by Pyprohly; 20 Nov 2015 at 16:38.
      My Computer


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

    Hi Pyprohly, Thank you so much for taking time to write me this script. I was so happy receiving your message. I just hoped that somebody will reply and I did not even think that I will get reply so fast. I shame myself because the script is not working probably because I am not really good at scripting and I do something wrong. If you could help me to place the output location link wherever it should go I would be very appreciate for it. I will test the script on my computer so I'll give you the output location: C:\escalation_monika_juhasz The location where I would like the script to search for the files: M:\Pictures I also attached the sample list.txt Thank you very much in advance.
    Remove Admin Rights and Move instead of Copy | Batch file Attached Files
      My Computer


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

    My script works differently from Neutron's. Here's a hint to get it working,
    1. Create the batch file and place it inside your starting folder, M:\Pictures.
    2. Write your desired output location, "C:\escalation_monika_juhasz", as the very first line of your text file of filenames.
    3. Drop your text file of filenames onto the batch file through Explorer.

    If you wish to view the output of the script, you may want to tack the 'pause' command to the end of the batch file.
      My Computer


  5. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #5

    Hi Pyprohly, You are fantastic It works like a charm. I cannot believe it! Thanks so much for your time and quick reply and also for clear guidance. Have a wonderful day.
      My Computer


  6. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #6

    Hi Pyprohly,

    Sorry to disturb you again but I would like to ask your help if you would not mind. Is there any way to move complete folders with its content to C:\escalation_monika_juhasz folder with the same method? Either by name and/or by date older than ... Actually, both method would be very useful. :)

    So this is the current script for moving files:

    @echo off
    setlocal EnableDelayedExpansion
    REM Desc.:
    REM Given a list of filenames, search for files with these names
    REM in the current directory and subdirectories of this script.
    REM Begin by dropping a text file, containing a list of
    REM filenames, onto this script. The first line of the text file
    REM should specify the output location for where the listed files
    REM are to be moved to.

    if not "%~2"=="" exit /b 1
    if "%~1"=="" exit /b 1
    if not exist "%~1" echo Input file not found&& exit /b 1

    <"%~1" set /p "out_folder="& set "out_folder=!OUT_FOLDER:"=!"
    for %%I in ("!OUT_FOLDER!") do set "out_folder=%%~fI"
    if not exist "!OUT_FOLDER!"\* md "!OUT_FOLDER!" 2>NUL || echo Output folder could not be found or created&& exit /b 1

    set /a moved_counter=0
    for /f "useback skip=1 delims=" %%I in ("%~1") do (
    for /f "delims=" %%J in (' dir /a:-d/b/s "%%~I" 2^>NUL ^| find /c /v "" ') do (
    if %%J equ 0 set /a file_count=0
    if %%J equ 1 set /a file_count=1
    if %%J gtr 1 set /a file_count=2
    )
    if !FILE_COUNT!==0 (
    echo File '%%~I' could not be located
    ) else if !FILE_COUNT!==1 (
    for /f "delims=" %%J in (' dir /a:-d/b/s "%%~I" ') do (
    move "%%~J" "!OUT_FOLDER!" >NUL && (
    echo Moved file '%%~J'
    ) || (
    echo Error moving file '%%~J'
    )
    )
    set /a moved_counter+=1
    ) else (
    echo Error: Mulitple instances of '%%~nxI' found:
    for /f "delims=" %%J in (' dir /a:-d/b/s "%%~I" ') do (
    echo '%%~J'
    )
    echo None were copied
    )
    )
    echo.& echo %MOVED_COUNTER% files were moved to directory '!OUT_FOLDER!'
    ECHO.
    pause


    Thank you very much for your help in advance.
    Monika
      My Computer


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

    Hi Monika,

    So I spent some time tweaking the batch file to a level of perfection. See the updated script in my previous post. Along with being able to process folders as well as files now, it offers the option to copy, move, delete, as well as a way to simply list found files, and switching between these modes is easy. Try it out.

    As for your "move items by date older than" request, it certainly would be time consuming and take quite the effort to integrate that sort of functionality into the batch script. Instead, Monika, you may find the below script useful: it takes a list of filenames and makes a new list out of it, according to the "older than" specification you define at the top of the script (between those double colons).


    ListFilesOlderThan.bat
    Code:
    @echo off
    if not "%~2"=="" exit /b
    if "%~1"=="" exit /b
    if not exist "%~1" exit /b
    ::
    set "outfile=%~n1_New.txt"
    set "use=DATE" &rem Options: DATE, DAYS
    set "by_date=2015-11-6" &rem Date format: yy-mm-dd
    set "by_days=30"
    ::
    if /i "%USE%"=="DATE" (
        powershell -c "gc ""%~1""|%%{dir $_.Trim('"" ') -r -ea 0}|%%{if($_.LastWriteTime -le ([datetime]::ParseExact('%BY_DATE%','yyyy-M-d',$null))){$_.FullName}}" >"%OUTFILE%"
    ) else if /i "%USE%"=="DAYS" (
        powershell -c "gc ""%~1""|%%{dir $_.Trim('"" ') -r -ea 0}|%%{if($_.LastWriteTime -le (Get-Date).AddDays(-%BY_DAYS%)){$_.FullName}}" >"%OUTFILE%"
    )
    start "" "%OUTFILE%"
    The resulting list can then be feed immediately into post 2's batch script for quick copying, moving, etc.
    Last edited by Pyprohly; 20 Nov 2015 at 16:22. Reason: Fixed potential quoting issue in script. Also retitled script from "ListFilesOlderThan.txt" to "ListFilesOlderThan.bat" :P
      My Computer


  8. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #8

    Hi Pyprohly,

    It works perfectly !!! Amazing . Thank you so much for your time making this script for me. I really appreciate all the hard work you’ve done to help me and I want you to know how much I value your support. :)

    Thank you again and have a nice weekend.
    Monika
      My Computer


  9. Posts : 10
    Windows 7 Ultimate x64
    Thread Starter
       #9

    Hi Pyprohly,

    Sorry to disturb you again Is there any way to print the result of the moved folders and unmoved folders to a text file with date stamp? Thank you very much.

    Monika
      My Computer


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

    Hi. Sorry for the delayed response.

    Like any command, you can capture and send the output of the batch script to a file using redirection on the Command Prompt.
    E.g.
    Code:
    C:\Users\Pyprohly>"MyScripts\CopyMoveFromList.bat" "Desktop\MyInputList.txt" >"Desktop\MyOutput.txt"

    And what exactly do you mean by "with date stamp"?
      My Computer


 
Page 1 of 2 12 LastLast

  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 12:16.
Find Us