Windows Batch File for loop on user dir - windows

I need to create a batchfile to delete a specific file in the users appdata dir. The batchfile is executed as localsystem.
I did test the following senarios:
Command Prompt as localsystem - this works
Command Prompt as admin - this works
Command Prompt as user - this works for all folders where the user has permissions
As soon as i put the for loop in a batch file, it dosent work anymore.
It also dosent matter in which context (User,admin,localsystem) i start it.
for /D %i in ("C:\Users\*") do del /Q /F "%i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
The exit code shows 255, which means to long error.
The error i get is the following:
C:\Windows\Scripts>sync_exeptionsiteslistandconfig_java.bat > output.txt
\Users\*") do del "i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" /Q /F was unexpected at this time.
Any help why the single line is working and a batchfile with this single line is not working is welcome.
Please also explain why it is not working. (I always run tasks as localsystem)

This may do what you want, but it only processes normal folders, not hidden etc.
Your problem may have been using %i within a batch script.
#echo off
for /D %%i in ("C:\Users\*") do del /Q /F "%%i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" 2>nul

Related

Block user from executing .bat files accidentally

We've large number of bat files in our server and they're scheduled using task scheduler. Sometimes, instead of right click to edit it, users execute them by double clicking it, triggering processes.
Can I able to change the double click properties of batch files only? Like triple click to execute, double click to do nothing.
Are there any batch commands to prompt user when they try to execute directly instead of command line?
I had similar cases and the issue solved when I added in the begin of the script TIMEOUT 10 the batch starts after 10 seconds, so if someone will accidentally start it will have time to close the window without running.
I do not recommend it but you can break the associated application for .bat files from registry changing the (Default) value from "%1" %* to nothing of the key: HKEY_CLASSES_ROOT\batfile\shell\open\command
I would definitely choose the first option.
#echo off &::in_con.bat filename and args
setlocal
set "_is.c=0"
for /f "tokens=*" %%# in ("%ComSpec%") do for /f "tokens=1-2 delims= " %%c in ('echo %CmdCmdLine%') do if /i "%%~nc"=="%%~n#" if /i "%%~d"=="/c" set "_is.c=1"
rem if running by click open in notepad
if "%_is.c%"=="1" if exist "%~1" notepad "%~1"
rem if running in console pass it throuth
endlocal & if "%_is.c%"=="0" cmd /c %*
goto :eof
always run .bat throuth cmd /c in_con.bat
Why not just rename them all to .txt and in your scheduler copy the file to a .bat|.cmd file and execute it. Best of all worlds.

How do I make a batch file delete it's own directory?

Okay, I apologize that I am very new at this, but I am trying to make my batch file delete it's own directory after it has been launched. This is how my folders are arranged:
Folder1
delete.bat
My goal is to make "delete.bat" delete "Folder1" after "delete.bat" has been launched. So here's my code:
rd /s /q %~dp0..\Folder1
This seems like it would work but it only deletes the contents of "Folder1" rather than the whole directory itself. What am I doing wrong?
Some thoughts...
%~dp0 gets the drive and path of the batch file, so you don't need to include ..\Folder1.
What you have should work. If it's not removing the folder itself, it means that it's locked, probably because cmd's current folder is Folder1. (That's a likely guess, but it's not the only reason it might be locked.) If it is cmd, you'll have to call the batch file from another folder, outside of Folder1.
While what you have will work, it will result in a funny error when resuming the non-existent batch file: The system cannot find the path specified. You can avoid that in the solution below.
One good solution: start /b "" cmd /c rd /s /q "%~dp0"
This creates a new process to remove the folder (and everything in it, including the batch file itself). Be careful. =)
From the corresponding MSDN link for rd:
You cannot use rmdir to delete the current directory. You must first change to a different directory (not a subdirectory of the current directory) and then use rmdir with a path.
I guess this is what's going wrong in your case since the batch file is located within the directory that you're trying to delete.
My implementation is effectively the same as Soja's, plus the info from dbenham's comment. I have added a 2-sec delay, to ensure there are no timing issues, even though I believe the error when the .bat file is deleting itself is harmless.
#echo off
:: Do the work
...your command here...
:: In order to delete the current dir we are running from (and all subdirs), none of them can be the
:: current working directory of any running process. Therefore, we are setting our own CWD to something
:: else, so it will be inherited by the below cmd.exe.
cd /d %temp%
:: The countdown is there to allow this batch file to exit, so it can then be deleted safely.
set DelayedSelfDeleteCommand="timeout /t 2 >NUL && rmdir /s /q "%~dp0""
:: Start a separate process (without waiting for it), to execute the command
start "" /b cmd.exe /C %DelayedSelfDeleteCommand%
Well I think it cannot be done (at least as normal user)
start /b "" cmd /c rd /s "%~dp0"
deletes folder but only with right permissions I think
start /b "" cmd /c rmdir /s "C:/folder"
This has the same result
del /s /q "C:\Temp\folder\*"
rmdir /s /q "C:\Temp\folder"
del %0
only way as for batch files is to use vbs script or autohotkey (send !{Space} // send e // send p formula) or hack it, you can delete only file used and content of folder but not working directory due to specification of cmd. Any other language would have no problem with it cuz it in RAM memory.
I recommend this way to do it (as for normal users):
in your bat file add
copy C:\urpath\deleteafter.bat C:\Temp\deleteafter.bat
start "" autohotkey.exe "X:\patchto\deletebatchfile.ahk"
deletebatchfile.ahk
sleep 2000
Run, C:\Temp\deleteafter.bat, C:\Temp\
deleteafter.bat
rmdir /s /q "C:\Temp\batfileworkingpath"
sleep 3
del %0

copying all the elements of a folder into another folder using a loop in batch file

I want to copy all the files of a folder into some other folders using batch script. Say, I have two folders named folder1 and folder2. these two folders are located in C:\Users\xyz . I want to copy the elements of another folder (say, folder3 which is located in C:\Users\abc\def) into these two folders. I have made the following script but nothing is copied. My sample batch file is as follows:
FOR /L %%A IN (1,1,2) DO (
xcopy /s C:\Users\abc\def\folder3 C:\Users\xyz\folder%%A
)
is there anything wrong in the batch file?
xcopy /s C:\Users\abc\def\folder3\*.* C:\Users\xyz\folder%%A\
where *.* is an appropriate filemask and the final \ in the destination name tells cmd that the destination is a directory.
I suggest using this command line in the batch file:
for /L %%A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%%A\" /C /G /H /I /K /R /Q /S /Y >nul
I enclosed both directory paths in double quotes in case of real paths contain 1 or more spaces or other special characters which require double quotes. The last paragraph on last help page output by running in a command prompt window cmd /? outputs on which characters in a directory/file name double quotes are required around the complete directory/file name.
The target path ends with a backslash to make it clear for console application xcopy that the target is a directory and not a file. Together with the redundant /I the target directory is created if not existing already.
For details on the options used on xcopy open a command prompt window and run xcopy /?. This outputs the help for this console application in the command prompt window. On Windows running a command or console application with /? as parameter outputs in general the help for the command/application.
Note: The copying from one user profile directory to another user profile directory requires local administrator privileges. Each user profile directory is by default protected for exclusive usage of the owning user. Therefore I suggest to open a command prompt window and execute in this window:
for /L %A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%A\" /C /G /H /I /K /R /S /Y
You can see if that works with just %A as required on command line instead of %%A as required in batch files and without /Q (quiet copying) and without >nul (redirection of success messages to device NUL to suppress them). Or when it does not work, you can see why it does not work as the error message can be viewed on running a command or a batch file from within a command prompt window instead of double clicking on a batch file because the console window keeps open.

Running executables using Windows batch files

I have an application in which I would like to run many executables using a batch file (in my case a program called AMDIS, http://chemdata.nist.gov/mass-spc/amdis/downloads/).
On the Windows command prompt it works if I type
C:\NIST08\AMDIS32\AMDIS_32.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​F /S /E
where AMDIS_32 is the program I want to run and C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​F the file I want it to analyze and /S /E some opions.
Now I would like to make these calls repeatedly using a batch file in Windows 7.
I tried making a batch file with
START C:\NIST08\AMDIS32\AMDIS_32.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​‌​F /S /E
but this doesn't seem to work. Does anyone know how I should do this?
cheers,
Tom
EDIT: based on the info in forum http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/fdb993d9-6a9c-4459-aedb-0283f2d6935d I found that my mistake had to do with saving my batch file in UNICODE rather than ANSI encoding - now it works - thanks to you all!!
:X
C:\NIST08\AMDIS32\AMDIS_32.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​F /S /E
goto X
#Echo OFF
Set /A "Interval=3"
PUSHD "C:\NIST08\AMDIS32"
:Loop
Start /B AMDIS_32.EXE "C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​F" /S /E
Ping -n %INTERVAL% Localhost >NUL
Goto :Loop

Running Multiple Installations with System-Reboot Inbetween them

Currently I have a set of software-Installations(and their paths) that i have to install on my Windows Machine.
What I do now is -- Hit RUN every time and type in the software installation path into it..
What I want is to design a Batch file which would install all the applications and REBOOT my system after every successful installation and then continue with the NEXT item in the list..
Is it possible using a .bat file ??
This really isn't something batch was designed for, so this will be a bit hacky. It's not elegant by any means but give it a shot, it might work for you.
for /f %%a in (C:\files.txt) do (
start /wait %%a
exit /b
)
for /f "skip=1" %%b in ("C:\files.txt) do (
echo %%b >>C:\newfiles.txt
)
xcopy C:\newfiles.txt C:\files.txt /y
del C:\newfiles.txt /f /q
shutdown /r /t 0 /f
The idea being that you have a text file with the paths of the executables that you want to install. It will go through and execute the first file in the list, wait for it to complete, then re-write the list without the file it just installed.
This is dependend on the setup file having no user interaction and exiting by itself, or maybe it's just to make things easier - in which case just go through each install yourself, and when it finishes the batch file will do the rest.
On the note of rebooting and continuing you will either need to run the batch file again yourself or put it in the registry to start up itself, the latter command being
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchInstaller" /d "C:\MyBatchFile.bat" /f
Hope this helps

Resources