Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Remove Admin Rights and Move instead of Copy | Batch file

16 Nov 2015   #11
monikajuhasz

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
.
17 Nov 2015   #12
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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]).

Quote   Quote: Originally Posted by monikajuhasz View Post
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.
My System SpecsSystem Spec
18 Nov 2015   #13
monikajuhasz

Windows 7 Ultimate x64
 
 

Hi Pyprohly,

Sorry for causing trouble. Thank you very much for all your help. It works perfectly
Have a nice day.
Monika
My System SpecsSystem Spec
.

Reply

 Remove Admin Rights and Move instead of Copy | Batch file




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
How to run Batch File as Admin?
Hello, I want to know how can i run a Batch File (.bat) as administrator since the Run as Administrator Checkbox in Compatibility tab is gray (can't check)? This is the Batch File: @echo off cd /d "C:\Program Files (x86)\Ubisoft\Far Cry 2\bin" start /realtime FarCry2.exe Should i put...
General Discussion
.bat batch move file to folder based on filename + remove string
I have a folder with thousands of filenames like this. I need to put them inside folders and remove the useless parts from the filenames. I need to do this before adding the files inside my XBMC library. www.AnnoyingSpam.com ] Some.File.Name.With.A.Very.Long.String.avi ...
General Discussion
I need admin permission to delete/move file (but I'm both admin/owner)
Hi, I'm new to Windows 7. I'm still trying to master the file permissions.. I need some help and I'd appreciate it if someone could help me out. I have a folder with mp3s (created with my old Windows XP system). I've included this mp3s folder in the "my music" library and then I inspected...
System Security
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion
Admin rights for batch
I got a batch script which needs Admin privileges. For example it installs some Programmes or move/copy files to/from System directories. So on Windows 7 (and Vista) I can say "Run as" and I got a Message like "are you sure allowing ... bla" OK/Cancel? But how I can get arround this MSG-Box?...
Software
Unable to save host file even with admin rights/UAC off
Hi all I searched the net on how to overwrite or save over the host file in the C:\Windows\System32\drivers\etc folder but I cannot seem to make it work here. I tried opening Notepad and run as administrator then file>open the hosts file and then save but I keep getting access denied. When I try...
General Discussion


Our Sites

Site Links

About Us

Find 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 01:05.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App