I am trying to automate setting the App Compatibility flags for numerous EXE files we use. I can get the forfiles command to pull up all of the EXE full paths with no problem. The issue is passing that information to the REG ADD command with the space. What am I missing here? I've tried several sets of double quotes as well as single quotes but nothing is working right. What's the correct syntax for this command to work? If it can be done as shown below that's great. If it has to be done a different way, that's ok too.
Any help would be greatly appreciated.
SET Key="HKLM\SOFTWARE\Microsoft\Window NT\CurrentVersion\AppCompatFlags\Layers"
SET Command="'REG ADD' "%KEY%" /v #PATH /t "REG_SZ" /d "RUNASADMIN""
forfiles /p D:\<DIR> /S /m *.exe -c "cmd %Command%"
Try with
SET "Key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
for /r "d:\<dir>" %%a in ("*.exe") do (
reg add "%Key%" /t REG_SZ /v "%%~fa" /d RUNASADMIN
)
Related
I need to check if there is any file inside the subdirectories of the current directory, which was last modified a day ago or before.
This would be pretty easy by using the following line if it worked.
FOR /D /r %%G in ("*") DO forfiles /p "%%G" /D -1 /C "SET /a exists = 1"
However there is a bug with forfiles when executing commands with it and you should call another cmd from it. (See http://ss64.com/nt/forfiles.html)
At the end it needs to be called:
FOR /D /r %%G in ("*") DO forfiles /p "%%G" /D -1 /C "cmd /c SET /a exists = 1"
The problem with this, is that "exists" would be a local variable in the other cmd session and not the current one, so I won't have it available in my script.
Raymond Chen suggests to use another FOR to actually perform the operation in: https://blogs.msdn.microsoft.com/oldnewthing/20120803-00/?p=6953
This made me write the following line:
for /f %%i in ('FOR /D /r %%G in ("*"^) DO forfiles /p %%G /D -1 /C "cmd /c echo 1"') do set exists=%%i
However in this scenario, this doesn't work as I have another FOR in between and what gets written to 'exists' is the subdirectory instead of the '1'.
How can I fix this?
Thanks in advance.
Edit:
As mentioned by aschipfl, switching the placement of both FOR commands made it work, however I'm now with the limitation that it just checks the date and does not care about the time. So a file with modified date of Sep. 15, 2016 23:59 would show up if the script is ran at Sep. 16, 2016 00:00
Why do you have to use FORFILES?
Does the /MINAGE switch of ROBOCOPY help?
This should set the %_exists% variable if there are any files older than 1 day:
#ECHO OFF
(SET _fold=%USERPROFILE%\MYDIRECTORY)
SET "_exists="
FOR /F "DELIMS=" %%I IN (
'ROBOCOPY /L /S /MINAGE:1 /NS /NC /NP /NDL /NJH /NJS "%_fold%" NULL *.*'
) DO SET/A _exists+=1
IF NOT DEFINED _exists EXIT/B
REM Commands here if a file exists
ECHO( Your command is running
TIMEOUT -1 >NUl
EXIT/B
Obviously I've put a command on line 12 just for the hell of it, you would likely change that.
so ive been searching for the solution to this problem for awhile now, everywhere i look everyone just says "set the compsec to point to cmd"...which is super helpful cause no one actually even says how to do that.
but when i open cmd, and type "Set" and hit ENTER, it shows ComSpec=C:\Windows\system32\cmd.exe
I checked there and sure enough, cmd.exe is in there, it works just fine. But for/f still closes before performing any operation.
How do I fix this?
#echo off
for /f "tokens=2*" %%a in ('dir /b /s findstr "Find Me Testing"') do set "AppPath=%%~b"
set "AppPath=%AppPath%"
echo %AppPath%
for /f "tokens=2*" %%a in ('dir /b /s /a-d ^| findstr "Find Me Testing"') do set "AppPath=%%~b"
set "AppPath=%AppPath%"
echo %AppPath%
pause
for /f "usebackq" %a in ('dir /b /s /a-d ^| findstr "To Be Deleted.me"') do set fileLocation=%~pa
echo %fileLocation%
pause
pause
stop
pause
wait 50
As you can see I've been testing various methods of doing what I want.
I lay good odds that your problem is with the cmd.exe autorun feature.
If you open a command session and enter cmd /?, then at about the 5th paragraph you will see the following:
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
I'd be willing to bet that one of those two registry settings is set to a command or script that is causing your problem. Edit your registry and remove those settings, and your problem should go away.
You can see a similar story about a user having trouble with FOR /F at https://blogs.msdn.microsoft.com/oldnewthing/20071121-00/?p=24433.
The FOR /F command executes your commands within your IN('....') clause via a new cmd.exe process, and that process will always run any autorun setting that may be present. Unfortunately it is impossible to disable this FOR /F "feature" - I think this is a horrible design flaw.
Windows pipes also use child cmd.exe processes - one for each side of the pipe. But the pipe instantiation of cmd.exe includes the /D option, so autorun is disabled. You can see this by running the following command from the command line:
echo %^cmdcmdline% | findstr "^"
On my machine it produces the following:
C:\WINDOWS\system32\cmd.exe /S /D /c" echo %cmdcmdline% "
Now do the equivalent with FOR /F (on a healthy machine)
for /f "delims=" %a in ('echo %^cmdcmdline%') do #echo %a
My machine produces:
C:\WINDOWS\system32\cmd.exe /c echo %cmdcmdline%
No /D option :(
I am trying to add a directory to the PATH variable in windows. This is what I am entering the command line. (Or a batch file)
#echo off
set value=%path%;%ProgramFiles%\AtomScript\
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d %value% /f
And it comes up with this message
ERROR: Invalid syntax.
Type "REG ADD /?" for usage.
What am I doing wrong?
You probably have to quote %value% (with double-quotes) because its expansion has embedded blanks for C:\Program Files, etc.
That would be
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%value%" /f
You can see what the actual expansions are by turning echo on in your script:
#echo on
Maybe you solved it already, but as far as I can see you also might have a misspelling in "...\Sessions Manager...". At least on my system it's "Session" without the extra s.
First time posting so go easy on me. :)
I need to delete a registry that begins with 'MikePike' for example.
It needs to check if anything that has this in its name, because 'MikePike' will contain numbers after it, in no specific order. I cant just delete 'MikePike' because it would be different every time.
This needs to be done in a .bat file as I'm trying to make my teams' job easier, and we cannot install any additional software.
I did look at using wildcards but not sure if you can use this for registry edits.
Below is a snippet of what I have in my .bat:
`REGEDIT4
REGEDIT.EXE /E C:/rs-pkgs/REGTEST.REG
REGEDIT.EXE /E c:/rs-pkgs/SEARCHREG.REG
#echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
set "current-date=%%e%%b%%c"
set "current-time=%%d"
set "weekday=%%a"
set "dateandtime=%%e%%b%%c-%%d
)
del ~.*
popd
echo Todays date is the following: %weekday% %current-date% %current- time%
rename C:\rs-pkgs\REGTEST.REG %current-date%.REG
reg delete HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\services\GxEvMgrC(Instance001) /f
reg delete HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\services\GxClusPlugIn (234435) (7532)'
The last reg key will have random numbers in it, I have hit brick wall... :(
Any help is greatly appreciated, and will save countless hours :D
Thanks, Michael
#echo off
for /f %%a in ('
reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\services" ^|
find "GxClusPlugIn"
') do (
set "regs=%%a"
)
echo %regs%
reg delete "%regs%"
I have recently discovered that i am not able to fire the following command from a batch file:
for %%G in (%path%) do sqlcmd /S <my-server> /d <my-database> /E /i "%%G" -b
It tells me, that it could not find the command "sqlcmd". However, if I run this command directly in the command prompt (by removing one % of each pair) it works!
Why is that and how can I make it work with my batch file?
Update:
This is the following code I got in my batch file:
#echo off
set mypath=%~dp0
set mypath=%mypath%sql
for %%G in ("%mypath%") do echo %%G & sqlcmd /S sample-server /d sample-database /E /i "%%G" -b
pause
Here's your problem:
set path=%~dp0
set path="%path%sql\"
PATH is the sequence of directories Windows uses to search for executables, so when you change it, sqlcmd can no longer be found.
Simply use another variable name. Mypath seems good...
Try this:
for %%i in ("sqlcmd.exe") do "%%~$path:i" /S "my-server" /d "my-database" /E /i "%%~dp$path:i" -b
To check your path settings you can use the following code:
#echo off&setlocal
set "temppath=%path:;=";"%"
for %%i in ("%temppath%") do echo %%~i