Working on a Caffinate Alternative for Windows and was wondering if there was a way I could grab just the GUID of the current used powerplan without the rest of the output heres a example of what I mean.
What I am getting... when using powercfg /list
Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *
What I want...
381b4222-f694-41f0-9685-ff5bb260df2e
This is so I can refrence it and reload it in once caffinate is finished.
#ECHO OFF
SETLOCAL
FOR /f "tokens=1-4" %%g IN ('powercfg /list^|find "*" ') DO IF "%%g%%h%%i"=="PowerSchemeGUID:" SET "guid=%%j"
ECHO guid=%guid%
GOTO :EOF
If you want other guids from the list, simply substitute Balanced High performance or Power saver (or whatever else you may need) for *
Related
I think I've bitten off a lot more than I can chew.
I'm a court reporter, working in the Minnesota court system. We've developed a new policy for filing our electronic notes, as so many reporters are no longer printing to paper notes. Different brands of reporting machines create files with different naming conventions, and we want to standardize the names so someone in district administration can find the right notes easily if we are not around.
So we want to rename FILE.001, FILE.002, and 20151018-082815.sgstn, for example, to the date the file was created. The sgstn file, of course, has a unique name, but the ubiquitous writers that create FILE.* files really need renaming, and standardization will help all. I'm creating a CMD file that will also poll for the judge's name and the writer brand, so anyone pulling the notes out of storage will find exactly what they're looking for: "2015-10-18 Judge Jones, Stentura writer.001"
I've found plenty of solutions for renaming files with TODAY's date and time, but we want to rename them with THEIR OWN date and time.
I'm working as a standard user in Windows 7 Pro and do not have the option of installing third-party software. Windows allows individual users to set their preferred date style, and dbenham and others here have shown me how to get that style from the registry:
:: For REG.EXE 3.0 (Windows XP) and later versions
FOR /F "tokens=3" %%A IN (
'REG QUERY "HKCU\Control Panel\International" /v sShortDate'
) DO (SET sShortDate=%%A)
Echo %sShortDate%
That will return yyyy-MM-dd or d-M-yy or whatever the user has chosen.
And then I can test to see what the contents are of the different pieces of the string:
for /f "tokens=1,2,3 delims=/-" %%A in ("%sShortDate%") do (
for /f "tokens=1" %%D in ("%%A") do (
set Part1=%%D
)
for /f "tokens=1" %%D in ("%%B") do (
set Part2=%%D
)
for /f "tokens=1" %%D in ("%%C") do (
set Part3=%%D
)
)
echo Part1 = %Part1%
echo Part2 = %Part2%
echo Part3 = %Part3%
But this is all getting really unwieldy, and I'm only halfway there after a solid 24 hours of reading here and elsewhere. Is there really no way in Windows, at the CMD prompt or in any command-line-available tool, to get the date of a file in a predictable format and to use that date?
Can anyone take pity on me and give us a less cumbersome means of finding out what the user's particular system thinks the date should be formatted as and thus consistently rename all files that get dropped on the CMD shortcut?
Finally, this is my first post here. All constructive criticism gladly accepted. Seriously.
-- Timothy J. McGowan
Edited to add:
Maybe I've been overthinking this. It's the user's hive in the registry I'm polling for the date format. We ought to be able to write to our own hive from the command line; right?
So I could read the user's preferred date format, save that to a variable, force the date format I want to use, process the files, and then reset the user's date format. Right? Worth a shot.
I'll report back if it works, but any more comments are quite welcome!
A first idea :
Use the delimiters to get the file date or name or extension
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "$FileName=%%~na"
set "$FileExtension=%%~xa"
set "$FileDate=%%~ta"
set "$FileDate=!$FileDate:/=-!"
echo !$FileDate!-!$FileName!!$FileExtension!)
EDIT I
To get the date in YYYY-MM-DD :
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "$FileName=%%~na"
set "$FileExtension=%%~xa"
set "$FileDate=%%~ta"
set "$FileDate=!$FileDate:/=-!"
set "$FileDate=!$FileDate:~6,4!-!$FileDate:~3,2!-!$FileDate:~0,2!"
echo !$FileDate!-!$FileName!!$FileExtension!)
Perhaps this isn't a full answer to the question I asked, but it should be all I need to get a predictable date.
It's possible to save the user's preferred date style from the Registry, force one you want to work with, conduct your business, and then return the user's preferred date style (assuming they don't cancel the batch file in the middle of the run).
The output is messy but it simply proves the concept works.
#echo off
:: replace and then restore date style in Windows registry.
:: find current date style; save to environment variable OriginalsShortDate
FOR /F "tokens=3" %%A IN (
'REG QUERY "HKCU\Control Panel\International" /v sShortDate'
) DO SET OriginalsShortDate=%%A
:: display current date style as reflected by date of this CMD/BAT file
echo Current date style to be saved: %OriginalsShortDate%
echo Looks like this:
dir %0
echo Press any key to set yyyy-MM-dd date style
pause
:: force preferred date style and hide success message
reg add "HKCU\Control Panel\International" /v sShortDate /d yyyy-MM-dd /f >nul
:: prove it's changed
:: //////////// here's where you do your business \\\\\\\\\\\\
echo Check file's date now.
dir %0
echo (Your Windows task bar may not reflect change immediately.)
echo.
echo Press any key to set MM-dd-yy date style
pause
:: force next style
reg add "HKCU\Control Panel\International" /v sShortDate /d MM-dd-yy /f >nul
:: prove it's changed
echo Check file's date now; it will be in MM-dd-yy format.
dir %0
echo Press any key to return date format to your preferred style.
pause
:: return to user's original setting
reg add "HKCU\Control Panel\International" /v sShortDate /d %OriginalsShortDate% /f >nul
:: prove it's back to normal
dir %0
echo Your original, presumably preferred, date style has been reset.
echo Give your Windows task bar some time to update.
I need to pull a certain string from a command output in a batch file, I am trying to get the GUID of the active power scheme to query the settings.
Currently, my code looks like this:
FOR /F "delims=" %%i IN ('powercfg /getactivescheme') DO set scheme=%%i
powercfg /query %scheme%
but of course the powercfg /getactivescheme command adds other useless junk to the output, so I end up with
C:\Users\Richard\Desktop>FOR /F "delims=" %i IN ('powercfg /getactivescheme') DO
set scheme=%i
C:\Users\Richard\Desktop>set scheme=Power Scheme GUID: c0ea6ad3-6145-4447-a15e-5
fb97be69b98 (Energy Star)
Now, all I want to pull is: c0ea6ad3-6145-4447-a15e-5fb97be69b98 and truncate the Power Scheme GUID: and (Energy Star)
for input into the next command which is powercfg /query %scheme%
Any suggestions are welcome.
Thanks!
This gets the 4th token, separated by space/tabs
FOR /F "tokens=4" %%i IN ('powercfg /getactivescheme') DO set scheme=%%i
I have ZERO experience with this. Any and all help would be greatly appreciated.
Right now I have it so I can see the list of folders I am creating and edit which ones I want to create. I want to be able to create Fund Family folders and in each of those Fund Family folders have various entities and then within each entity create folders. I think this requires referencing many different outputlists. So like at the top the fundfamily list is fine...i think i need serval outpistlits for each funfamily that will contain the entities so entitylist01...entitylist02... if that makes sense
There are 32 fund families with around 600 entities in total. The entity list changes for each fund family.
this what I have so far...
mkdir C:\2013\"2 - November Projections"
pause
dir Z:\2013\"2 - November Projections"\"*" /b /a:d >fundfamily.txt
start C:\Windows\System32\notepad.exe "fundfamily.txt"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do mkdir C:\2013\"2 - November Projections"\"%%~a"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do dir Z:\2013\"2 - November Projections"\"%%~a"\"*" /b /a:d >entitylist.txt
start C:\Windows\System32\notepad.exe "entitylist.txt"
pause
for /f "tokens=*" %%a in (entitylist.txt) do mkdir C:\2013\"2 - November Projections"\"*"\"%%~a"\2013\"November Estimates"
pause
#echo off
setlocal
SET "sourcedrive=Z"
SET "destdrive=C"
SET /a year=2013
SET "projections=2 - November Projections"
SET "Estimates=November Estimates"
DIR /b /ad "%sourcedrive%:\%year%\%projections%" >fundfamily.txt
start /wait "Fund Family" C:\Windows\System32\notepad.exe "fundfamily.txt"
FOR /f "delims=" %%a IN (fundfamily.txt) DO (
ECHO MD "%destdrive%:\%year%\%projections%\%%~a"
DIR /b /ad "%sourcedrive%:\%year%\%projections%\%%~a" >entitylist.txt
start /wait "%%~a entities" C:\Windows\System32\notepad.exe "entitylist.txt"
FOR /f "delims=" %%b IN (entitylist.txt) DO ECHO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
)
It's incredibly difficult to solve a problem stated as ""my code doesn't do what I want it to do" without an indication of sample data or desired outcome.
For instance, you could provide a partial listing such as
fundfamily.txt
Jones
Brown
de la Vedova
Smith
entitylist.txt
Stock - Confusion Industries
Sweatshop Industrial Inc.
Royalties - Useless Invention
123, Slum St., Gloomsville
and a note like the entity list changes for each fundfamily - or renaims the same, or whatever. You can Edit this data into your question - 'tis what the Edit link under the tags is used for.
As for the above code - I've assumed that the structure is as I have indicated. I would suggest that it would be quite an onerous task if you have hundreds of fundfamily entries, but we have no further data to go on...so it's "air" code - untried for absence of available data.
Note that the first quoted string in the start/wait line becomes the window title. If you don't want a window title, use an empty string ""
I've left C:\Windows\System32\notepad.exe in place, but it's quite likely that simply notepad would suffice, or even that it could be omitted entirely. The /wait is the magic - this waits until the executable is finished before the batch proceeds to the next step.
Also note that the MD commands (MD and mkdir are synonyms) are merely echoed - so that you may test without actually changing anything. You'd need to remove the echo keyword before the md to actually create the directories.
I am launching a browser from batch file.
START "www.google.com"
I would like to know the PID of this browser window launched.
There can be many browser windows launched on a single machine. I need to find the PID of the process which was launched by my batch file only. I tried with WINDOWTITLE filter. But its not a good idea as titles may change in future. I am using Windows XP/7
Any help would be appreciated.
Thanks.
For what it worth (question is more than 2 years old) this code do the trick, just change variable according to default browser exe
set "browser=palemoon.exe"
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-before.txt
start www.google.com
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-after.txt
:: fc /L /LB1 test4-Before.txt test4-After.txt | find /I "%browser%"
for /f "delims=, tokens=2,*" %%A in ('"fc /L /LB1 task-before.txt task-after.txt | find /I "%browser%""') do set pid=%%A
SETLOCAL enabledelayedexpansion
pid=!pid:"=!
ENDLOCAL
echo pid is %pid%
This is just an idea, to get you maybe on the way
there is a command called Tasklist
there is a switch called filter /FI with lets you decide what filter parameters you want to output, f.e PID. Output this to a > 1.TXT
start your proces
recheck the watchlist and output to 2.TXT
Then you would have to get creative. COmpare 1 to 2,
maybe remove the processes in 1 from the 2.TXT
The remainig PID is what you wanted?
If you have some programming experience, you could create your own console application that accepts command-line parameters and passes them to the Win32 API CreateProcess() function. One of its output values is the spawned process ID, which your app could then return. Then just update your batch file to call your app instead of using START directly.
I'm trying to do the same thing. Though there must be some way of doing it, but all my Googling suggests not.
Check out robvanderwoude.com to see a list of 3rd party tools and examples. Also check out the full list of Sysinternal's process utilities here.
I've been looking at this for about 2 hours now and I think that there is a way to do this, but it requires some more insight on how windows handles iexplore.exe for PID...
I have a working version of a batch file I wrote that will get you what you want, BUT only if its the FIRST AND ONLY Internet Explorer Window open.
For some reason I can't get the PID to change when I open new browsers, but I can get results if there is no window open (obviously because there is no PID)
Anyhow, this is what I have... you should be able to run this on your system and it will tell you that there are no differences and it might actually produce results if your default browser is Firefox or Chrome or something... just need to make the changes to what I'm providing.
#echo off
IF EXIST c:\temp\pshell.txt del c:\temp\pshell.txt
IF EXIST C:\temp\PID1.txt del C:\temp\PID1.txt
IF EXIST C:\temp\PID2.txt del C:\temp\PID2.txt
IF EXIST C:\temp\PowerFormat.txt del C:\temp\PowerFormat.txt
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%1 IN ( c:\temp\pshell.txt ) DO #echo %%1>> C:\temp\PID1.txt
start "title" "www.google.com"
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%2 IN ( c:\temp\pshell.txt ) DO #echo %%2>> C:\temp\PID2.txt
FC /L c:\temp\pid1.txt c:\temp\pid2.txt> C:\temp\FileComparison.txt
FOR /F "tokens=7 delims=" %%3 IN (c:\temp\FileComparison.txt) DO #echo %%3>C:\temp\DiffPID.txt
FINDSTR "FC: no differences encountered" c:\temp\FileComparison.txt
IF '%ERRORLEVEL%'=='0' del C:\temp\FileComparison.txt & echo.No PID Found
IF NOT '%ERRORLEVEL%'=='0' type c:\temp\FileComparison.txt
pause
exit
Let me know if this helps...
I'm trying to write two batch files that will allow me to switch the Power Scheme (Control Panel -> Power Options -> Power Schemes Tab) from Home/Office Desk to Portable/Laptop and back. My operating system is Windows XP SP3.
My reason for doing this is because I want to disable SpeedStep when I'm playing games on my laptop (i.e. put it on the Home/Office Desk scheme) and enable SpeedStep otherwise (back to Portable/Laptop). Windows XP turns turns off dynamic switching in Home/Office Desk mode. I'd like to be able to do this programatically to save myself some time everytime I want to play a game.
Any thoughts on how to modify the power settings using a simple batch file? Python and Ruby scripting is also an option but isn't preferred.
C:>%windir%\system32\powercfg.exe /?
/SETACTIVE, /S Makes the power scheme with the specified name active.
Examples:
POWERCFG /SETACTIVE scheme
#echo off
setlocal EnableDelayedExpansion
echo Available power schemes:
echo/
set i=0
set "options="
for /F "tokens=2,3 delims=:()" %%a in ('powercfg /L') do if "%%b" neq "" (
set /A i+=1
set "options=!options!!i!"
echo !i!. %%b
set "scheme[!i!]=%%a"
)
echo/
choice /C %options% /N /M "Select desired scheme: "
powercfg /S !scheme[%errorlevel%]!
echo/
echo Power scheme set
Perhaps you need to adjust the "tokens=2,3 delims=:()" FOR options; this code was written for Windows 8.1 Spanish version.