I on windows 8.1. I get USB with hidden file/folders. To unhide them I use attrib command . I want to run the commands by simply inserting the USB. Pl help.
echo off
echo Please have patience!!! Wait or Minimise the window!!!
rem c:\script\unhide.bat
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)
Pause
Here I got the drive letter H. I am unable to use drive letter and use the following commands in USB. How can is I run below commands in USB. I mean Change Drive, run attrib command in USB, delete unwanted files from USB and see USB's contents.
cd\
attrib -s -h -r /s /d
del *.lnk
del thumbs.db
del desktop.ini
del autorun.inf
echo Your Folders has been recovered!!! Check your folders and files
dir
pause
exit
#ECHO OFF
SETLOCAL enableextensions
echo Please have patience!!! Wait or Minimise the window!!!
rem c:\script\unhide.bat
for /F "skip=1 tokens=1-3" %%i in ('
wmic logicaldisk where "drivetype=2" get caption^,drivetype^,SystemName
') do (
if "%%j"=="2" (
echo "%%i" is a USB drive ^(DriveType=%%j^).
pushd "%%i\"
SETLOCAL enabledelayedexpansion
echo current folder !CD!
ENDLOCAL
echo attrib -H -S -T /S /D /L >NUL 2>&1
echo del *.lnk 2>NUL
echo del thumbs.db 2>NUL
echo del desktop.ini 2>NUL
echo del autorun.inf 2>NUL
echo Your Folders has been recovered!!! Check your folders and files
dir
pause
popd
)
)
ENDLOCAL
goto :eof
Note:
wmic command changed as follows:
where clause;
description omitted as this property vary in word number and therefore breaks the tokenization;
always non-empty property SystemName appended to pass over the ending carriage return in the line returned: wmic behaviour = each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>). For another (general) approach see Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem;
for /F loop adapted according to altered wmic command (and skip=1);
operational attrib -H -S -T /S /D /L is merely echoed for debugging purposes; remove echo no sooner than debugged (the same for all del commands);
used pushd - popd pair: PUSHD changes the current directory/folder and stores the previous folder/path for use by the POPD command;
folder System Volume Information should keep attributes System & Hidden if present.
Related
I want to delete a specific directory on Windows. I use the code below. It works fine. I want to put the .bat file I made for this process in that directory. Naturally, the .bat file is also deleted. I want the .bat file to be excluded from this deletion. What should I do with the code?
Echo batch file delete folder
#RD /S /Q "D:\testfolder"
All you have to do is to lock your batch file, by opening a dummy read handle to it.
echo The batch file wont be deleted because it is locked by a dummy input redirection.
rd /s /q "D:\testfolder" 9<"%~f0"
Naturally, an error message will be shown by the rd command, because there is at-least one file in the target directory (your own batch file) which can not be deleted. You can hide that message by redirecting the standard error stream to the nul device:
rd /s /q "D:\testfolder" 9<"%~f0" 2>nul
There are several ways to achieve your task.
The method, (especially as you generally remove directories with one command, and delete files with another), is to identify the files and subdirectories separately. First identify the subdirectories and remove those, using the RD command, then delete all files except for the batch file itself, %0:
You could do that in one line using the ForFiles utility:
#%SystemRoot%\System32\forfiles.exe /P "%~dp0." /C "%SystemRoot%\System32\cmd.exe /C If #IsDir==TRUE (RD /S /Q #File) Else If /I Not #file == \"%~nx0\" Del /A /F #File"
Or you could use a For loop, with the Dir command:
#For /F Delims^=^ EOL^= %%G In ('Dir /B /A "%~dp0"') Do #If "%%~aG" GEq "d" (RD /S /Q "%%G") Else If /I Not "%%G" == "%~nx0" Del /A /F "%%G"
Please note that you can only remove/delete items for which you have the required permissions.
I would walk through the items in the folder whose contents you want to delete, remove one after another, except its name equals the name of the batch file:
rem // Change into target directory:
pushd "%~dp0." && (
rem /* Loop through immediate children of the target directory, regarding even
rem hidden and system items; to ignore such (replace `/A` by `/A:-H-S`): */
for /F "delims= eol=|" %%I in ('dir /B /A "*"') do (
rem // Check name of current item against name of this batch script:
if /I not "%%~nxI"=="%~nx0" (
rem /* Assume the current item is a sub-directory first (remove by `rd`);
rem when removal fails, try to delete it as a file (done by `del`): */
rd /S /Q "%%I" 2> nul || del /A /F "%%I"
)
)
rem // Return from target directory:
popd
)
SETLOCAL enabledelayedexpansion
(
FOR /f "delims=" %%a IN ('dir /ad /b /S "*-ABC*"') DO (
REM Get the foldername without the path name and without the extension (i.e. Folder1-ABC)
SET "eir_foldername=%%~na"
echo "!eir_foldername!"
REM Remove "-ABC" from the filename (i.e. Folder1)
SET "reg_foldername=!eir_foldername:-EIR=!"
echo "%%~dpa" "!reg_foldername!"
rem for /D /r %%~dpa!reg_foldername! in (*) do rmdir /q /s %%~dpa!reg_foldername!
for /D /r %%i in (%%~dpa!reg_foldername!) do echo %%i
Rename "%%~dpa" "!reg_foldername!"
REM Rename the file (with full path and filename) to the new filename (that does not have "-Eir" in it + the original extension)
)
)
echo Successfully renamed!
rem Endlocal
Endlocal
pause
:end
I have a path which contains various folders with the same name. For example, in the pash C:/Test/First/Second/, I have folders Folder1, Folder1-ABC, Folder2, Folder2-ABC.
What I am trying to do is to remove the folders without -ABC and rremove -ABC if a folder name contains it, for example Folder1-ABC should become Folder1).
#echo off
for /f "delims=" %%A in ('dir /ad /b /s ^| findstr /i /v "\-ABC$"') do (
if /i exist "%%~A-ABC\" (
echo rd /q /s "%%~A"
echo move "%%~A-ABC" "%%~A"
)
)
pause
With paired folder names i.e. name and name-ABC, you are looking for the latter name and removing -ABC from the name. It would be perhaps be easier to append -ABC than to remove -ABC.
findstr with /v will print lines that do not match -ABC at the end of line, due to use of $ anchor. Now each path processed in the loop appends -ABC to %%A to check if the paired name does exist. If it does, then the rd and move commands will execute.
The echo command infront of rd and move are for testing. If satisfied is OK, remove the echo commands.
Ok. I have this batch file that is designed to remove every file and folder from inside a folder that may or may not be present. The basic structure Follows:
#echo off
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo %CD%
echo %0
echo %~dp0
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
The reason for the echoes in I'd like to be able to ensure the batch script has actually changed the path to MyDirectory and is deleting the correct files(we had an incident earlier that I'm kindof in hot water for where the script didn't change paths and deleted everything from one of our docs folders).
The echoes return the either the name of the batch file itself, or the path the batch file was run from,instead of "c:\MyDirectory\". So in my case it's from c:\Testing\ (a dummy directory I created to avoid a second oops).
Is there a way to get the currently active directory from inside a batch script, so that I can verify the directory I'm about to empty??
Try this:
#echo off
setlocal EnableDelayedExpansion
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo !CD!
pause
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
Since you're in an if statement you should use setlocal EnableDelayedExpansion and use ! instead of % for variables.
To nuke the contents of a directory, use this shell script (batch file):
#echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
I am trying to connect to a network drive, copy a file and move it to another location, and log if it is successful or not. This is what I got so far:
#echo off
#setlocal enableextensions enabledelayedexpansion
if exist Transfer_logfile.txt (
ECHO Y | del Transfer_logfile.txt
)
set LogFile=_logfile.txt
set logg=^> _^&^& type _^&^&type _^>^>%LogFile%
for /F "tokens=*" %%A in (ipaddresses.txt) do (
net use \\%%A\c$\test /USER:test %logg%
echo copying files across to %%A... %logg%
COPY -f -R -Y C:\test\test.exe \\%%A\c$\test\test.exe %logg%
echo Copy completed.. %logg%
net use \\%%A\c$\test /delete /Y %logg%
)
pause
Any help on how to finish this of would be appreciated.
I am struggling to save any errors in the output. If it errors I want it to just save that it failed in output and go on to the next IP in the IP address text file.
I think, I need to wrap an if around the net use checking that the ip address pings first. However this does not work.
#echo off
#setlocal enableextensions enabledelayedexpansion
if exist Transfer_logfile.txt (
ECHO Y | del Transfer_logfile.txt
)
set LogFile=_logfile.txt
set logg=^> _^&^& type _^&^&type _^>^>%LogFile%
for /F "tokens=*" %%A in (ipaddresses.txt) do (
ping -n %%A > NUL
IF ERRORLEVEL 0 (
echo
ELSE goto :skipcopyhost1
net use \\%%A\c$\test %logg%
echo copying files across to %%A... %logg%
COPY -f -R -Y C:\test\test.exe \\%%A\c$\test\test.exe %logg%
echo Copy completed.. %logg%
net use \\%%A\c$\test /delete /Y %logg%
)
pause
Try this not tested batch code:
#echo off
if not exist ipaddresses.txt goto :EOF
setlocal
del /F Transfer_logfile.txt 2>nul
set "LogFile=Transfer_logfile.txt"
for /F %%A in (ipaddresses.txt) do (
%SystemRoot%\System32\ping.exe -n %%A >nul
if errorlevel 1 (
echo %%A is not available in network.>>%LogFile%
) else (
echo Connecting to %%A ...>>%LogFile%
%SystemRoot%\System32\net.exe use X: \\%%A\c$\test /persistent:no /Y 2>&1 >>%LogFile%
echo Copying file to %%A ...>>%LogFile%
copy /B /V /Y C:\test\test.exe X:\test.exe 2>&1 >>%LogFile%
echo Disconnecting from %%A ...>>%LogFile%
%SystemRoot%\System32\net.exe use X: /delete /Y 2>&1 >>%LogFile%
)
echo.>>%LogFile%
)
endlocal
pause
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
del /?
for /?
goto /?
if /?
net /?
net use /?
set /?
Further read the Microsoft articles:
Using Command Redirection Operators
Testing for a Specific Error Level in Batch Files
I don't explain all the errors in your code as they are too many.
I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?
solution that worked for me was
create bat file "delete_all_desktop_ini.bat"
with
del /s /q /f /a ".\desktop.ini"
put it in a folder and run it
it will delete all desktop inis in current directory and sub directories of this file.
i put it in a project folder that is in google drive
Give this a test:
I've altered the recycle bin name to what I see here in Windows 8.
The name changes with different versions of Windows.
#echo off
del /s /q /f /a "h:\desktop.ini"
del /s /q /f /a "h:\$Recycle.Bin\*.*"
The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"
Try it, to make it working remove echo from the script.
del /s /q /f /a ".\desktop.ini"
it should works as charm
save file .bat
put it in any folder
it will delete ini files in folders and sub folders