Solved Remove Admin Rights and Move instead of Copy | Batch file

monikajuhasz

New member
Local time
7:34 AM
Messages
10
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 (http://www.sevenforums.com/member.php?u=3585)
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 (http://www.sevenforums.com/member.php?u=3585)
PAUSE
GOTO:EOF
:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
​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:

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly, Thank you so much for taking time to write me this script. I was so happy :D 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 :o 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. :D 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.
 

Attachments

My Computer My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
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 My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly, You are fantastic :D 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. :thumbsup:
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
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. :) :o

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 My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
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:

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly,

It works perfectly :thumbsup:!!! Amazing :D. 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 My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
Hi Pyprohly,

Sorry to disturb you again :o 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 My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
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:
[COLOR="silver"]C:\Users\Pyprohly>[/COLOR]"MyScripts\CopyMoveFromList.bat" "Desktop\MyInputList.txt" [COLOR=red]>"Desktop\MyOutput.txt"[/COLOR]


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

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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 :o. 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 My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
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]).

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:

My Computer My Computer

At a glance

Windows 10, Windows 8.1 Pro, Windows 7 Profes...
Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Hi Pyprohly,

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

My Computer My Computer

At a glance

Windows 7 Ultimate x64
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Qosmio
OS
Windows 7 Ultimate x64
Back
Top