I have a folder test which has some other files and folders. I also have a bat file delete.bat
test
test\delete.bat
The bat file delete the contents of test using the next command:
rmdir C:\test /s /q
I also want to delete the folder test. If i copy the delet.bat file and paste it in another directory the test folder will be deleted.
However, if i run the delete.bat file inside the test folder, all the contents of the test folder(included the delete.bat file) are deleted, but the test folder not.
I consider that this is because the test folder is opened.
Any suggestion? Is there any command that i can add at the beginning if delete.bat in order to close the folder first and then run the command rmdir C:\test /s /q?
The following delete.bat works fine is you run it using win+R in Windows.
cd c:\
rmdir C:\test /s /q
Ok,
Sasha_gud helps a lot.
Now i find a way to add code in bat file in order to run as administrator (so the run as administrator will be run automatically).
For more information look at
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
Here is the code:
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
#echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
REM Run shell as admin (example) - put here code as you like
cd c:\
rmdir C:\Users\haris\Desktop\test /s /q
Only the 2 last lines are my code.
Related
I'm setting up a pair of logon and logoff scripts that will download and upload the users Outlook signatures automatically to and from their home directory (Z:\ drive).I'm using the scripts below, which work fine when run manually, but do not seem to be running at all (or at least not working) when set up as logon and logoff scripts with group policy. I have verified with gpresult that the scripts are indeed applied.
Logon Script (to download signatures):
#echo off
set LOGFILE=Z:\batch.log
call :LOG >> %LOGFILE%
exit /B
:LOG
if exist "Z:\Signatures\" (
xcopy /e /Y /D Z:\Signatures %appdata%\Microsoft\Signatures
)
Logoff Script (to upload signatures):
#echo off
set LOGFILE=Z:\batch.log
call :LOG >> %LOGFILE%
exit /B
:LOG
if not exist "Z:\Signatures\" (
mkdir Z:\Signatures
attrib +h Z:\Signatures /s /d
)
xcopy /e /Y /D %appdata%\Microsoft\Signatures Z:\Signatures
Does anyone have an idea why this wouldn't be working?
Fixed by using %homeshare% instead of the mapped Z:\ drive
I am writing an application which uses batch file to copy some files to another location. I am using 64-bit windows 7.
I have asked for admin privileges too using below code:
Code block to get ADMIN right:
#echo off
:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
)
else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
Code to copy a file to system32 folder:
copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"
There is no error in copy operation, but the file is copied to "C:\Windows\SysWOW64" location automatically. Need help.
Try using:
#echo off
:: Batch-Admin API
net file>nul 2>&1&&if "%~1"=="64" (goto:GotAdmin) else (if exist "%windir%\Sysnative\" (call start %windir%\Sysnative\cmd /c "%~0" 64&exit) else (goto:GotAdmin))
echo Requesting administrative privileges...
(echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "ELEV","", "runas", 0 ) > "%temp%\admin.vbs"
cscript /Nologo "%temp%\admin.vbs"&exit
:GotAdmin
:: Place ADMIN tasks below
copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"
pause
exit
I modified rewrote your script to:
Use an alternative way to check for admin permissions net file && echo Admin || echo No-admin
Added 64-bit launcher VBScript launches everything as 32-bit (redirecting C:\Windows\System32 to C:\Windows\SysWOW64).
64-Bit launcher:
call start %WinDir%\SysNative\cmd /c %0 (This window is hidden)
I am trying to create batch files to switch on/off read-only mode of drives.
I have created two batch files which call diskpart, and two text files that contain the commands to be passed to diskpart.
When I run the diskpart commands separately, they work; But when I run them through the batch file and text files, they don't work.
Batch-file to lock disk
diskpart /s readonlyfile.txt > logfile .txt
pause
Batch-file to unlock disk
diskpart /s offreadonlyfile.txt
pause
readonlyfile.txt (for locking the disk)
diskpart
list disk
select disk 1
attributes disk set readonly
offreadonlyfile.txt (for unlocking the disk)
diskpart
list disk
select disk 1
attributes disk clear readonly
You need administrative privileges to use diskpart, and if you don't have them, then your batch script will fail silently. To automatically request administrative privileges, you can use visual basic in conjunction with your batch script. You can also use simple redirect to auto-create the temporary files for you and delete them when your done
#echo off
setlocal enabledelayedexpansion
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
set /p "mode=Do you ant to set read or write: "
:: call the first 4 letters of the string
call :%mode:~0,4%
pause
endlocal
goto eof
:read
echo.diskpart>readonlyfile.txt
echo.list disk>>readonlyfile.txt
echo.select disk 1>>readonlyfile.txt
echo.attributes disk set readonly>>readonlyfile.txt
diskpart /s readonlyfile.txt>logfile.txt
del readonlyfile.txt
exit /b 0
:writ
echo.diskpart>offreadonlyfile.txt
echo.list disk>>offreadonlyfile.txt
echo.select disk 1>>offreadonlyfile.txt
echo.attributes disk clear readonly>>offreadonlyfile.txt
diskpart /s offreadonlyfile.txt
del offreadonlyfile.txt
exit /b 0
Source for the UAC prompt script: https://sites.google.com/site/eneerge/scripts/batchgotadmin
How do I check if a bunch of files exist in my script?
I want my script to perform some add the files if its not present or just ignore and proceed with the rest of the code.
This is what I have writtes:
if not exist qapi/qapi*.h (
#ECHO OFF
CD %~dp0\..\..
ECHO %CD%
SET TOP_IOE_SW= %CD%
ECHO %TOP_IOE_SW%
SET BUILD_DIR=%TOP_IOE_SW%/build/ms
icacls * /q /c /t /grant Users:F
mkdir include\qapi
xcopy /Y qapi_export\qapi*.h include\qapi
xcopy /Y qapi\common\qapi*.h include\qapi
xcopy /Y core\api\qapi*.h include\qapi
xcopy /Y qapi\build include\build\
)
else (
call :next)
:next
////the rest of the code comes here///
When I run the script, it keeps prompting me to overwrite these files.
How can I have this done without the prompt?
Thanks
My .bat file looks like this:
#echo off
CD /D "%~dp0"
if [%2]==[] (
set user=%USERNAME%
) else (
set user=%2%
)
:getFile
if [%1]==[] (
set /p file=Enter file name :
) else (
set file=%~f1
echo File name: %~f1
)
:checkFile
for /f "useback tokens=*" %%a in ('%file%') do set file=%%~a
if not exist "%file%" (
echo Error: Could not find file: %file%
echo.
)
:: Check for admin permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' == '0' (
goto gotAdmin
)
:: Rerun this batch with admin rights
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd", "/c """"%~f0"" ""%file%"" ""%user%""""", "%CD%", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
echo.
:eof
pause
exit /B
I have these two test files:
C:\Test\Folder\ファイル.txt
C:\Test\フォルダ\File.txt
When I run the batch file above and drag 1 onto the cmd window I get:
, which is good.
When I do the same for 2, I get:
When I call UAC.ShellExecute, %file% isn't passed correctly.
How can I get around this problem?
My preferred way of starting a batch file with administrator permissions is to create a shortcut, and then mark that shortcut as requiring administrator permissions.
First right-click foo.bat, then create a shortcut. Open the properties for that shortcut, click the Advanced… button and enable Run as administrator.
This has a downside: you can't drag file names onto the resulting command prompt window. But you can drag a file onto the shortcut.
But what if I don't want or can't use a shortcut?
You can avoid the need to write arbitrary Unicode characters to the file by passing your file name as an argument to your script. Even if the VBS file is in ANSI encoding, the script host always uses Unicode internally.
So here is how you write the VBS file and run it:
:: Rerun this batch with admin rights
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd", "/c """"%~f0"" """ + Wscript.Arguments.Item(0) + """ ""%user%""""", "%CD%", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" "%file%"
exit /B
Try adding a CHCP (CHange Code Page) command to start of you batch file, using the UTF-8 code page 65001, e.g:
#echo off
chcp 65001
.
.
.
See here for a bit more info on code page identifiers: https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
EDIT: You MUST also use a unicode capable font such as Lucida Console for your command window. Without this the command processor chokes on the unicode characters, and will either not find the files, or may display a "system cannot write to the specified device" error.
Click the window icon at the top-left of the command window, choose Defaults on the menu, then on the Fonts tab choose Lucida Console.
UPDATE - Test batch file and output below.
Here's the batch file I'm using to test this:
#echo off
chcp 65001
CD /D "%~dp0"
:getFile
if [%1]==[] (
set /p file=Enter file name :
) else (
set file=%~f1
echo File name: %~f1
)
:checkFile
for /f "useback tokens=*" %%a in ('%file%') do set file=%%~a
if not exist "%file%" (
echo Error: Could not find file: %file%
echo.
) else (
echo Found file "%file%"
)
Here is the output from my test, when I drag firstly "C:\temp\test\ファイル.txt" into the window, then secondly "C:\temp\test\フォルダ\file2.txt".
My system is Win 7 Pro x64 SP1, with English UK settings.
Your problem is that the way you create your temporary VBS file means it is not a valid unicode file and so Windows doesn't know how to interpret the unicode name you have passed in.
Following beercohol's advice to use code page 65001, I still found that I could not access a file in a unicode directory. However, if I tried to create the file by hand with a unicode editor (e.g. using notepad and saving as a unicode encoding) and invoke that manual script instead of the autogenerated VBS file, it all just worked.
I've re-worked your script to use iconv to create a utf-16 file instead. Note that this script needs to be run with code page 65001 in order to work.
#echo off
CD /D "%~dp0"
if [%2]==[] (
set user=%USERNAME%
) else (
set user=%2
)
:getFile
if [%1]==[] (
set /p file=Enter file name :
) else (
set file=%~f1
echo File name: %~f1
)
:checkFile
for /f "useback tokens=*" %%a in ('%file%') do set file=%%~a
if not exist "%file%" (
echo Error: Could not find file: %file%
echo.
)
:: Check for admin permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' == '0' (
goto gotAdmin
)
:: Rerun this batch with admin rights
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd", "/c """"%~f0"" ""%file%"" ""%user%""""", "%CD%", "runas", 1 >> "%temp%\getadmin.vbs"
iconv.exe -f utf-8 -t utf-16le "%temp%\getadmin.vbs" > "%temp%\getadmin2.vbs"
"%temp%\getadmin2.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
if exist "%temp%\getadmin2.vbs" ( del "%temp%\getadmin2.vbs" )
pushd "%CD%"
CD /D "%~dp0"
echo.
:eof
pause
exit /B