1.

Solve : CACLS leading to deletion of files in root directory on USB Flashdrive?

Answer»

Hi all,

could someone pls help with the below ( I'm using it in the root directory of the USB flash drive, and for a folder called 'USB' which has encrypted file in it )
on decryption the folder is meant to be accessible (by using CACLS), then any decrypted folders/files (excluding the encryption master file which has a +S +H attribute to it) and not ;the 'USB' folder itself are to be deleted / removed...

Below is the code ( this works fine, the moment i switch computers, even though the drive is the same 'E:\' ), it tends to delete any files in the root directory of the USB Flash Drive ( where I execute this batch file from) , also it tends to lock up the Cacls Folder 'USB' and makes it inaccessible even with CACLS COMMANDS !

any help greatly appreciated !
cheers


:: Auto deletion of decrypted files

@Echo Off

cd %cd%

cacls USB /e /c /g %Username%:f

cd USB

cls

start usbsafeguard.exe


echo To delete recently decrypted/viewed files and folders.........press any key
echo.
echo Will only delete copies of decrypted files ( not the original encrypted files)
echo.
echo.

pause

cls

:: Deletes files in USB (except ones with attrib +h +s )

del/A:-H-S USB *.*

:: Deletes/Removes folders in USB folder (but not the 'USB' folder itself)

pushd %1
del /Q *.*
for /f "Tokens=*" %%G in ('dir /B') do rd /s /q "%%G"
popd


cd..

cacls USB /e /c /d %Username%

exit


Quote from: tomcatonnet99 on March 23, 2013, 08:05:48 AM


del/A:-H-S USB *.*

:: Deletes/Removes folders in USB folder (but not the 'USB' folder itself)

pushd %1
del /q *.*
for /f "Tokens=*" %%G in ('dir /B') do rd /s /q "%%G"
popd


The first command should PROBABLY be: del /A:-H-S \USB\*.*?

Then the pushd has %1 as a parameter but we don't know what it is.

This should delete the files and folders in \USB but retain the folder.

Code: [Select]pushd "\USB" || goto :EOF
rd /s /q "\USB" 2>nul
popd will give it a try,

1) the del /A:-H-S \USB\*.*? Whats the '?' for pls? is it to determine deletion be done if \usb is not empty
( also would that matter if a folder is empty and I run the above command)

2) could you pls explain how the latter half works?

pushd "\USB" || goto :EOF ......... ( what is || for )
rd /s /q "\USB" 2>nul ............( what is 2> exactly)
popd

many thanks indeed!

Regds

Tom

See the in-line comments in blue.

Quote from: tomcatonnet99 on March 24, 2013, 12:37:34 AM

1) the del /A:-H-S \USB\*.*? Whats the '?' for pls? is it to determine deletion be done if \usb is not empty
( also would that matter if a folder is empty and I run the above command)

The ? suppresses the question 'Are you sure?'
It should work in an empty folder too.


2) could you pls explain how the latter half works?

pushd "\USB" || goto :EOF ......... ( what is || for )

|| is a conditional operator and here it exits the batch file if the folder doesn't exist or cannot be changed to.
It runs if the errorlevel from the preceeding command was true. && is the opposite and runs a command if the errorlevel is zero/false.


rd /s /q "\USB" 2>nul ............( what is 2> exactly)
popd

2>nul redirects a harmless error message (from the STDERROR stream) to the NUL device so it isn't displayed on the screen.

Many thanks foxidrive, truly appreciate that explanation..

I think the problem is CACLS ... the first instance it unlocks the folder 'USB' ... runs through the entire routine without a glitch even with the earlier code for del and rd, run it a second time and it locks the folder and now I've tried everything and it wont unlock!

cacls USB /e /c /grant Everyone:f
cacls USB /p Everyone:f
cacls USB /e /c /g %Username%:f



Any suggestions? Why would it do that ( have tried it on 2 different laptops in WinXP and Win7 and both react similarly, just not the laptop on which it was initially created which is also a winXP) on the parent laptop it is able to unlock successfully regardless of the number consecutive locks/unlocks!

also does CACLS work exactly the same in Win7 / Win8 as I will need to use this flash drive and the above batch file in different environments
It may be because you grant permission to username, but then remove permissions to username and that may include all the default permissions.

What you could do is Edit and grant 'everyone' permission, and do your tasks, and then remove the 'everyone' permission.

You should find that even if it is 'locked' then by starting from an elevated admin prompt you will be able to access the folder again and change permissions.Quote from: tomcatonnet99 on March 25, 2013, 05:40:25 AM
also does CACLS work exactly the same in Win7 / Win8 as I will need to use this flash drive and the above batch file in different environments

The security you are using seems to be a bit haphazard as any admin will be able to take ownership, and change the permissions.

In Windows 7 and 8 there is an additional tool called ICACLS I think, and while CACLS works the same way (I think, I haven't checked deeply) I have found that there are some system folders that it cannot change the permissions of (at least to edit and grant permissions to 'everyone').may be so as regds its inability to change permissions to some system folders but this is on a USB flash drive...

MY set up is as follows:

I have a PROGRAM 'USB Safeguard' which resides in \USB folder, its executable file is hidden
with attrib +h +s. It creates its own .lnk invisible(-s-h attrib) encrypted file(needs password to unlock) ....

1)so far I've never seen it being deleted whilst using the TYPE nul>\USB\*.*

on using Del A:\-s-h %cd%USB\*.* DESPITE the -s -h its still deleting that file ! what am I doing wrong pls?

2) and I don't see why cacls would work on one laptop and not another

regds

further rd /s /q removes the \USB directory as well which isn't what I want for to happen ... only the contents (files and folders except +h +s attrib ones ) to be deleted / removed

If you use the code as provided then it won't remove the \USB folder too because it is in use, as the pushd command opens the folder and locks it from being deleted by the RD command.

Quote
pushd "\USB" || goto :EOF
rd /s /q "\USB" 2>nul
popd
Quote from: tomcatonnet99 on March 25, 2013, 06:14:00 AM
only the contents (files and folders except +h +s attrib ones ) to be deleted / removed

Sorry, that's different to what I had understood you wanted to do.

The RD command will remove all folders and files from within \USB



Another post:

This will not delete hidden files from \usb and will remove the folders and files.

Code: [Select]@echo off
del \usb\*.???
for /f "delims=" %%G in ('dir \usb /b /ad') do rd /s /q "\usb\%%G"
pause
I admit I didn't read all the posts, however I noticed the use of cacls and USB flash drives, so I think it's worth mentioning that flash drives, under normal circumstances, will use FAT32, which does not support Access Control Lists; cacls is a ACL editor, so there might be some assumptions or extra information from the ACL security info that you are trying to use that just isn't available.Quote from: foxidrive on March 25, 2013, 07:12:08 AM
Another post:

This will not delete hidden files from \usb and will remove the folders and files.

Code: [Select]@echo off
del \usb\*.???
for /f "delims=" %%G in ('dir \usb /b /ad') do rd /s /q "\usb\%%G"
pause

thanks, is this the same as using push \usb and then rd/ s /q it?? ( coz thats definitely a lot simpler, as I would need to understand what exactly the above means / how it works)
especially delims .... what is /f and whats the difference between "delims=" and "delims=*"

Quote from: BC_Programmer on March 25, 2013, 01:00:01 PM
I admit I didn't read all the posts, however I noticed the use of cacls and USB flash drives, so I think it's worth mentioning that flash drives, under normal circumstances, will use FAT32, which does not support Access Control Lists; cacls is a ACL editor, so there might be some assumptions or extra information from the ACL security info that you are trying to use that just isn't available.

Yes I'm using the NTFS format as I use the 'safeguard.exe' in the \USB folder which requires NTFS.

the problem is CACLS! it works perfectly well to lock&unlock the \USB from any unintentional deletion; but in Win7 / Win8 ICACLS would be required and as such I would like to have a CACLS /ICACLS available for any system operability...

(ICACLS is just way to complicated, its like MS have just nuked us! to stone age and driven users to an other planet and now we're like aliens! there )

Any ideas/ suggestions to lock / unlock \USB folder?


Discussion

No Comment Found