Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am using WinAutomation and I am trying to point to and open a file on any computer that my software is placed on without having to hard-code the username in the path, so I am looking for a way to retrieve the logged in Username, that will be case sensitive so I can grab that variable.
Using the Windows command line, (which I can run invisible to the user in the background) if the username in the file tree is "User Name", whoami will return "user name" and then,
If I point to a file C:\Users\%pcName%\Desktop\Masterlist.xlsx with that %pcName% not correctly capitalized it will not work...
I saw a question about Pythonic way to retrieve case sensitive path but I can't use Python for this. I am open to creative ways to get the case sensitive User Name but I keep seeing people deciding to enforce lowercase usernames as a solution.
Is there a way to find the case-sensitive logged in users name for use in file path references? I'm open to being creative at this point!!
First, let me make clear that your request is probably an XY issue. You're trying to achieve something and it looks like the solution is to fix the casing in the user name. However, there are several argument against that:
Windows file names are not case sensitive. It should not matter what casing you use. Maybe some other component compares these names in a case sensitive way where it shouldn't. In that case, the component should be fixed.
User names in Windows are not case sensitive. You can log on with any casing.
You're constructing a path C:\Users\%pcname%, which looks like %USERPROFILE%. However, a user profile is not necessarily in C:\Users. It could have been moved anywhere.
It's unclear to me, what exactly "does not work" and for which reason. You don't specify an error message which could let me diagnose the root cause, so we could fix the real problem.
You say that you can't use Python, but you do not specify any other programming language that you can use, so let me treat dos as an equivalent of batch.
Whatever your real problem is, echo %username% does what you ask for:
C:\Users\For example John>echo %username%
For example John
But be warned: in 90+% of the cases, this is not the correct approach.
In C#, you would use
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
for the user profile and
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
for the Desktop.
Winautomation has a function, "Get Special folder" that will return the filepath to the Desktop of the current PC as a variable. That variable can then be used as the root of whatever you are trying to access.
This is the correct way to reach the Desktop file path and does not require any special DOS usage.
'Desktop' is a User Shell Folder. The value for these folders is stored in the registry, with the right case :-).
The registry can be queried from the command prompt using 'reg query'.
If you just want the data value of the Desktop property, use the following:
for /f "tokens=2*" %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop ^| findstr "REG_"') do #echo %b
Related
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.
I am currently working on a batch file to note several informations about the computer and now I've got another obstacle which I would like to remove.
As some of you might know it, some of the wmic commands output is rather big and you need to can choose which column you want to see, but for some of those commands it's still pretty much filled with a lot of informations which keep filling up the files.
I've seen there is the command for but I haven't really got a clue how it works exactly because I would like to sort some of the lines to be excluded from the output. Let's take the command wmic nicconfig for example. I only need to informations of the currently used network adapters which are normally just the LAN port and wifi, which at least recieve an IP-Address and/or have a MAC Address which I need to write down.
My question is: Is it possible to explude every line which contains specific word in the description, "Wan Miniport" or "Check Point Virtual Network" f.ex., and/or are Null or empty in MAC Addresses?
Under SQL it would be as simple as just finding the right column name but this isn't sql and therefor I'm stuggling pretty much with this perhaps simple problem.
I just found a possible solution.
It will be something like this:
type C:\TestTextFile.txt | findstr /v Word1 | findstr /v /x Word2 > C:\TestTextFile2.txt
It will just need a little bit more work of defining the rest of the lines which should be excluded. I will try to figure it out with this link.
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.
How do I manually create short file names in Windows if by default short file name is not assigned?
When we run command as below c:\dir x we can see the list of short file name.
But I want to create new short file name if not exist in list.
If you are asking about Windows not generating SFNs in general, then that question belongs on SuperUser instead of here. For the record, you can turn the registry entry back on as so:
fsutil disable8dot3 set 0
If you are asking about selecting specific SFNs instead of letting Windows automatically generate them, you can do that too:
fsutil file setshortname *LFN* *SFN*
Alternately, you can do it programmatically with the SetFileShortName command.
These only work with NTFS volumes. On FAT volumes, there is no practical way to specify the SFN. You would need to directly edit the directory entry, making sure to update the LFN checksums.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I need to compare 2 folders "A" and "B" and get the list of files and folders newly added or modified.
I tried using Winmerge software but it is not comparing the files present inside the internal folders(so i have to point to each internal folder manually and have to compare)
Is there any way to achieve this.
The following PowerShell code compares the file listings of two folders. It will detect renamed or newly created files and folders, but it will not detect modified data or different timestamps:
$dir1 = Get-ChildItem -Recurse -path C:\dir1
$dir2 = Get-ChildItem -Recurse -path C:\dir2
Compare-Object -ReferenceObject $dir1 -DifferenceObject $dir2
Source: MS Devblog - Dr. Scripto
3rd party edit
How to run a PowerShell script explains how to run the code above in a script.
For Windows you can use this solution.
Here's the right way to do it, without the external
downloads. It looks like a lot at first, but once you've done it,
it's very easy.
It works in all Windows versions from 7 back to 95.
For our example assume that you're comparing two directories named 'A'
and 'B'.
run cmd.exe to get a command prompt. (In Windows 7, the powershell won't work for this, FYI.) Then do it again, so that you have two of
them open next to each other.
in each window go to the directories that you want to compare. (Using 'cd' commands. If you're not comfortable with this, then you
should probably go with the external utilities, unless you want to
learn command prompt stuff.)
type 'dir /b > A.txt' into one window and 'dir /b > B.txt' into the other. You'll now have two text files that list the contents of each
directory. The /b flag means bare, which strips the directory listing
down to file names only.
move B.txt into the same folder as A.txt.
type 'fc A.txt B.txt'. The command 'fc' means file compare. This will spit out a list of the differences between the two files, with an
extra line of text above and below each difference, so you know where
they are. For more options on how the output is formatted, type 'fc
/?' at the prompt. You can also pipe the differences into another
file by using something like 'fc A.txt B.txt > differences.txt'.
Have
fun.
This is not necessarily better than other options already mentioned but it might better fit certain use-cases. In my case, I wanted to see what was different before copying those differences from one directory to the other. This method is great for that since the /L option means to only log what would happen.
robocopy C:\dir1 C:\dir2 /MIR /FP /NDL /NP /L
You can further refine the output format with other flags, or change the logic used to to compare, etc. Refer to robocopy docs for all the options.
We have been using Beyond Compare for years and it's quite useful. You can see which files are identical, which files are in folder "A" only and which files are in folder "B" only, and files that are different (for those files you can see what specific modifications have been made).
Some years ago, I made a command line utility, CrcCheckCopy, to help me verify the integrity of large data copies. It reads the source folder and produces a list of the CRCs of all the files. And then, using this list, it can verify the other folder.
I also use it to verify the same folder after some years, to make sure nothing was accidentally deleted or modified.
I give it from free from here in case people who arrive to this question want to try it.
FreeFileSync did the job for me.