Windows Environmental Variable for Month Year - windows

I have a process that writes log files and screenshots to a folder on a regular basis creating
between
30-50 files a week. The folder structure is ..\Year\Month\filename.ext, and it cannot be changed.
I'd like to create a shortcut to stick in my Favorites so I can get to this folder quickly but I need a variable for the YEAR and MONTH. Are there an environmental variables for this?
Any bright ideas on how I could create one that updates automatically preferably without a script or scheduled job?

The %DATE% environment variable holds the current date. You might be tempted to parse the date with %DATE:~6,4% to get the year.
But this method would not be very solid. %DATE% returns the current date using the windows settings for the "short date format". The problem is that the short date format setting is fully and endlessly customizable. One user can configure its system to show the short date as 29/06/2012; while another user (even in the same system) may choose Fri062912. It's a complete nightmare for a BAT programmer.
One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation
WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table
returns the date in a convenient way to directly parse it with a FOR.
Completing the parse and putting the pieces together
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
SET /A MONTH=%%D
SET /A YEAR=%%F
)
your shortcut can point to a BAT file that includes the above code and then opens explorer on the right folder.
start "" "D:\FOLDER\%YEAR%\%MONTH%"

Related

Create multiple URL shortcuts from a text file using a batch script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 28 days ago.
Improve this question
Summary: I have a set of reports I must to run, and would like to create links to each report online. The problem I'm having is how to make a batch file which can successfully loop through the file containing my URLs, and make an internet shortcut for each URL with a name based on data from URL itself.
Example URL exactly as shown, each URL is a line in a text file, (one report per day of the year):
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-02;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-03;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
Breaking the URL down to understand the static vs dynamic data and how to manipulate it.
https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date= (static data)
2023-01-01 (dynamic data - format YYYY-mm-DD)
;end_date= (static data)
2023-01-31 (dynamic data - format YYYY-mm-DD)
;vehicle_ids=1103007;report_id=25;report_type=normal (static data)
I'm trying to make the URL shortcut names based off the line itself for example, with the URL https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal, I'd like to create a shortcut in directory named: C:\Users\UserName\OneDrive\Desktop\Folder for the links\01-01-2023 mileage report.URL. The filename of 01-01-2023 being dynamic based on the date stated in the URL itself.
Also I found out the hard way, the other day, this computer "%UserProfile%\Desktop\Anywhere Else" is not a valid directory, not sure if this is a new Windows 11 trait, (moved from Win 7), or a OneDrive issue. Either way, any old code I had that used the %UserProfile% variable is needing re-done, or I need to make a new environment variable.
Here are my issue(s):
I have no experience using for looping command, let alone the restrictions of batch when using special characters. I thought I'd best ask before goofing something up terribly (special characters in the text file containing all the URLS).
The URL can vary on length ,so I'm not sure how to extract the month/day/year to use in the filename correctly.
Batch is the only language I have any experience with, and that is limited too. So whilst I'm sure there are much better options to do the task I'm wanting, I don't know them, to try them.
What I have done:
I used spreadsheet to break the URL down, modify the dates for the entire year, concatenate the data in a manner which made a proper working URL for each day of the year, added a column for the filename, and Echo #Echo Off> To the filename, Echo Start (the URL)>> To the filename, Echo Exit>> To the filename.
At which point, I was able to copy the crude columns of code from the spreadsheet, and paste them into the Command Prompt, which created a batch file for each URL, (file name was based on date) like so:
#Echo OFF
Start "" "https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-
01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal"
Exit
Making a working batch file for each link but surely not the right way to do it. (It's not the first time I've used a spreadsheet to break apart a directory or URL and re-constitute it for a batch file).
However I feel making actual internet shortcuts would be a much better way to do this across all forms of devices, (I think one of the people who may look at the reports from time to time use MacOS, and I'm assuming a batch file to "start" the URL won't work on their Mac, let alone not being the most friendly way of doing the job.
What my mind is telling me so far to do, (this is the batch file in my mind at the moment):
#Echo Off
CLS
REM Batch file create URLs based on text file
REM Filenames based on URL data from line in file
REM File containing URLs C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt
REM Save directory C:\Users\UserName\OneDrive\Desktop\Report Creation\All URLS Here
for /F "tokens=*" %%A in (C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt)
do (Echo [InternetShortcut] > "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo URL=%%A >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo IconFile=C:\Program Files\Google\Chrome\Application\chrome.exe >>
"C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo IconIndex=0 >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
)
Exit
But I'm lost on how to extract the mm-DD-YYYY from the URL in the text file, and set it as the "option1", as I feel there is need for a second for command somewhere, to make this possible.
I have not ran my potential code, as shown above, through this system yet as I'm not sure how to make the filenames based on the data in the URL of the file filenames.txt, and I'm not sure this is even possible.
I did break the URLs down (365x) in spreadsheet, re-constitute them with proper dates, and make a column for filenames based on those dates, where I was able to further concatenate lines like Echo #Echo Off>FileName(s).bat, Echo Start "" "The URL with updated dates">>FileName(s).bat and Echo Exit>>FileName(s).bat, effectively creating a crude batch file containing the proper link, and having a correct name based on the date of the URL via the Command Prompt. However, making 365 batch files per year, and being locked to only Windows OS, not able to run those batch files on my phone either, makes them half as useful as if they were a .url file which any device can open.
Here is how I would think, based upon your provided text file content, and your unattempted code, it could be done:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "URLFile=C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt"
Set "SaveDir=C:\Users\UserName\OneDrive\Desktop\Report Creation"
For /F "UseBackQ Delims=<>" %%G In ("%URLFile%") Do (
For /F "Tokens=3 Delims=;=" %%H In ("%%~G") Do (
Echo [InternetShortcut]
Echo URL="%%~G"
Echo IconFile="C:\Program Files\Google\Chrome\Application\chrome.exe"
Echo IconIndex=0
) 1>"%SaveDir%\%%H.url"
)
As you had assumed, an additional for loop was needed.
Please be aware however, that it is not very likely that your phone, or a Mac will have a copy of chrome.exe in the location stipulated in the IconFile entry. So you may be better advised to miss out those two Icon* keys, or at least their values.

How can I change the windows USERNAME environment variable string?

I am looking for a solution for this after struggling for most of the day. In short my dilemma seems simple. An effective solution appears elusive based on the Google and Stackoverflow hits I've had on this one.
(EDIT) I need to clarify the use-case. We may have 6 users needing to run certain scripts that use the "%USERNAME%" string to login to case-sensitive environments, Linux, MySQL, etc. One user-name is mixed-case. In the example I used my name: "Will" for the scripts it must-be: "will" (all lower-case).
The general question though is how can the %USERNAME% environment variable value be change when it is mis-entered?
At present my Windows 7 Professional username is "will" but the environment variable has this value:
echo. User = %USERNAME%
User = Will
I need it to come-out as:
User = will
rem * All lower-case
To be clear ...
I want to change (source) value Windows sets the USERNAME environment variable when I login.
I thought it would be easy to find the setting or config option to change that string -- Until I tried. I think this must happen frequently judging from questions related to getting a USERNAME to work with cygwin, etc.
The question
How to convert the value of %USERNAME% to lowercase within a Windows batch script?
Bothers me too, because that's a messy script to "fix" something basic in a login-environment.
I did a quick survey around the office as well. Some people have mixed case, one or two have lowercase. It seems arbitrary depending on what was typed-in when account was created or something.
Our admin guy looked up my account on the domain (no, not active directory) and is same as my login-screen, "will" (lowercase).
Finally I did a couple of experiments. I changed my username in the Users control panel
from: "will" to "xxwill"
Thinking that would surely update my USERNAME string. I logged out. I did it again and rebooted a second time. The result was extremely surprising (or maybe it shouldn't have been):
User = Will
rem * NO change for the "xxwill" name-change!
Some further grist. I created an account "dodo" and account "Dogdo" and changed the usernames to "Dodo" and "dogdo" respectively.
User = dodo
User = Dogdo
rem * NO change for name-changes!
I see that the USERNAME can't be altered that way. I can't edit the system environment variable in the Advanced options. It may be in the profile. But can you edit a profile or even look inside it with just Administrator powers? I don't know yet; I'm not sure that's my preferred route either since it is hacking with a bit of "output" from some original setting, somewhere.
I hope someone can set me straight so I don't need that script-solution.
If you want to change the windows USERNAME environment variable string, you can do this in the Advanced User Accounts Control Panel.
Open it by running netplwiz ("C:\Windows\System32\Netplwiz.exe").
Then select the name you want to change and click on the Properties button.
Now you get the properties of the selected user account: you can change the User name (, Full name & Description) here.
You'll (just) need administrator rights to do this.
Log out and log on, open a command prompt and try to output the value for %USERNAME%. You'll notice it has been changed according to your modification.
#ECHO OFF
SETLOCAL
PUSHD "%temp%"
COPY NUL "%username%" >NUL 2>NUL
FOR /f "delims=" %%a IN ('dir /L /b "%username%"') DO set "usernamelc=%%a"
del "%username%" >NUL 2>NUL
POPD
ECHO %USERNAME% --^> %usernamelc%
GOTO :EOF
Perhaps this may help. YMMV.
I don't think there is a way to do exactly what you are saying, but it might be achievable, you will know by playing around with the locations where usernames are defined.
• Firstly, there is the user folder C:\user\username1 and that is defined at user creation. This can (I think) be changed (I've only seen references to it, but there are many references, this is easy to find).
• Secondly, there is the Control Panel > User Accounts > Change My Name. This seems to tie in with your $env:USERNAME
• Finally, there is the lusrmgr.msc > Users. This is tied most closely to your "real" username, and aligns with [Security.Principal.WindowsIdentity]::GetCurrent()
I say "real" because I found this recently when trying to use ssh-keygen to create a public key and although my Display Name and $env:USERNAME were set to "Name1", ssh-keygen would only see "Name2" and I had to alter the name in lusrmgr.msc to fix this (a reboot was required).
This is an old post, but posting in case this can help anyone with similar issues.

WMIC process list bug?

i recently found that wmic (windows management intrumentation command-line) existed :D, which is far better w/ more options than the regular "tasklist" in cmd that saves me time.
But i found a problem when exporting to .txt, is this: supposed to be that way? Because the first two letters of each line, are in the final part of each line... like here: .. is this a known "glitch/bug"? i couldn't find anything online that demonstrated this..
You have no problems with your files. You have a problem with notepad. There is a limit of 1024 characters in the length of the line. The 1025th character and following are moved to next line.
I'd suggest your problem is related to the fact that WMIC outputs unicode. I'd suggest piping the output through MORE.
I use `Editplus as an editor which appears to accept a unicode file quite happily. I'd suggest your editor isn't quite up to snuff.
MORE is a supplied utility - try more /? from the prompt for more info.
Processing your WMIC output through more is
wmic whatever | more >textfile
you might also try
for /f "delims=" %%a in ('wmic whatever') do >>textfile echo(%%a
(as a batch line - reduce %% to % to use from the prompt)
Notepad is a wannabe WP, wannabe editor. Tries to do both and does neither particularly well. Often gives problems in batch work. Since it's a supplied utility, there's no incentive to improve it - Microsoft simply doesn't have the funds.
Hence the availability of software from other vendors to do the task required.

How can I change the system date format using a batch file/script?

I am a software tester and I often need to switch my short date format from M/dd/yy to d/M/yy because of Australian/New Zealand customers using a different date format. It is a pain in the ass to open up the clock, click change date and time, click change calendar settings, select the different date format from the drop-down and apply it before testing. Especially since when I am done I have to go through all of that again to set it back. This happens often enough that I want to automate the process like I have with some others (uninstall latest beta and installing the newest build, etc).
Does anyone know how I would go about accessing these settings in a batch file or in some kind of script?
Thanks!
reg add "HKCU\Control Panel\International" /f /v sShortDate /t REG_SZ /d "d/M/yy" >nul
echo %date%
It's better to change the user's registry instead of the global.
HKEY_USERS\.Default\Control Panel\International
try this reg key with REG add and such
Add or edit the sShortDate string value with
MM/dd/yyyy,
M/d/yy,
M/d/yyyy,
MM/dd/yy,
yy/MM/dd,
yyyy-MM-dd, or
dd-MMM-yy.

how to get wall paper name using command?

I need find out the wall paper name that has been set in windows. using command through command prompt i need to find out wall paper name.consider an example, i have set somepicture.jpg as a wall paper. now by using a command through command prompt i should be able find the name of the wall paper(somepicture.jpg). is it possible? Please help me?
Thanks!
In Win7, path is stored in the registry key HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper. You can obtain it using
reg query "HKCU\Control Panel\Desktop" /v Wallpaper
But then you need a bit parsing of returned value.
in WinXP file name is 'C:\Documents and Settings\%USERNAME%\Local Settings\Application Data\Microsoft\Wallpaper1.jpg' (or bmp depending on image type).

Resources