Solved Batch File Help (1 Viewer)

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
I am hoping someone could help me with this.
I am trying to use a batch file to get the file path of a users desktop due to some people having OneDrive enabled and syncing their desktop.

I found a way to get the path via [Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop) but how would I apply that to a variable to use later? I have next to no experience in this field.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:32
Joined
Oct 29, 2018
Messages
21,628
I am not familiar with that. When I tried entering it in the command prompt, I got syntax error. How do you use it? Is it a powershell script?
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
It seems to only work in Powershell as cmd doesnt recognize it.
 

plog

Banishment Pending
Local time
Today, 10:32
Joined
May 11, 2011
Messages
11,696
Here's how you set variables in batch files:


When I put this in a batch file and run it:

Code:
@echo off 
set mypath=%cd%
echo %mypath%
TIMEOUT /T 5

It spits out the directory of my batch file. But when I replace cd with your variable information it just spits out the name of the variable. You're variable isn't correct.
 

plog

Banishment Pending
Local time
Today, 10:32
Joined
May 11, 2011
Messages
11,696
I think this should generally work on windows:

Code:
@echo off 
set mypath=%USERPROFILE%\Desktop
echo %mypath%
TIMEOUT /T 5
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
I think this should generally work on windows:

Code:
@echo off
set mypath=%USERPROFILE%\Desktop
echo %mypath%
TIMEOUT /T 5
This is more or less how I initially tried to do it, and it works. The problem comes when people are using OneDrive and syncing their desktop as the path to the desktop changes.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:32
Joined
Oct 29, 2018
Messages
21,628
It seems to only work in Powershell as cmd doesnt recognize it.
Okay, that's what I thought. Please correct me if I am wrong, but wouldn't your users need admin rights to run the PS script?
 

plog

Banishment Pending
Local time
Today, 10:32
Joined
May 11, 2011
Messages
11,696
Code:
The problem comes when people are using OneDrive and syncing their desktop as the path to the desktop changes.

How do you know when they are synching it? If it exists isn't that good enough?

On my organizations system I have both these directories:

C:\Users\plog\Desktop
C:\Users\plog\OneDrive - MyCompany\Desktop


Can't you just check to see if %USERPROFILE%"\OneDrive - YourOrganization\Desktop" exists? If it does that's the directory and if not use %USERPROFILE%\Desktop
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
I did try START "MSAccess.exe" %UserProfile%\OneDrive - Company Name\Desktop\ReelTime.accdb, but all that seems to do is open the onedrive folder and doesnt launch the application.
 

plog

Banishment Pending
Local time
Today, 10:32
Joined
May 11, 2011
Messages
11,696
I think you need the full path to MSAccess.exe and then you might run into problems with the access file being a trusted location.
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
I think I got it. I wrapped the path in quotes and that seemed to let it do its thing.
 

plog

Banishment Pending
Local time
Today, 10:32
Joined
May 11, 2011
Messages
11,696
Just wondering what the big picture is? While back I had to use batch scripts as part of a Task Scheduler task to open Access databases, run a macro and then spit out reports and close. Just trying to make sure you fully automate whatever it is you are doing.
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
It is just a simple batch file that pulls the latest file from the server so people run the most current without me having to email it to them.
 

Isaac

Lifelong Learner
Local time
Today, 08:32
Joined
Mar 14, 2017
Messages
8,969
I have had so many problems with OneDrive hijacking Desktop paths and folders.
Sorry, I'm not asserting that it is anyone else's fault (including OneDrive), as I'm sure I could eventually master every nuance of it.

But dang is it annoying as ***

Even on my laptop I'm constantly saving stuff to "Desktop" as it appears on the left side tree, then finding it's not really on my Desktop, it's on some secondary Desktop that OneDrive made for me. Other times it appears they are totally in synch.

I feel that things have gone downhill since Windows began emphasizing the heck out of Libraries.

Just give us simple folders and paths please! And I like a free 5 GB of OneDrive, but for the love of God, stop commandeering all of our familiar paths to confuse the living blank out of everyone.

Just because I haven't taken the time to figure it all out doesn't mean they're wrong .... it just gives me an idea of how much negative impact it probably has on many other people who are less motivated and less knowledgeable and kind of depending on the concept of a single, clear, obvious Desktop !
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
OneDrive is nice but it does create a headache when dealing with paths and figuring out where things went. I ended up getting this to work by making a batch file specifically for OneDrive users. Not the best workaround I am sure, but it worked lol.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:32
Joined
Feb 19, 2002
Messages
43,785
I'm not sure why you are using OneDrive to distribute the FE since it sounds like people are working on a LAN. I'm assuming we are talking about an FE. Is that correct?

Where is the BE? Isn't there a shared server you could use so you have a standard UNC reference?

If you are somehow using OD to distribute the FK, you have to ensure that only YOU have update access to the folder with the FK because you'll have a real mess on your hand if OD saves a copy of the FE that has been modified by a user and that now becomes the "master".
 

AccessBlaster

Registered User.
Local time
Today, 08:32
Joined
May 22, 2010
Messages
6,082
I thought this might be a good test for A.I. While this is obviously air code and may not work, it could spur an idea among humans;)

I have the same issue in python and I don't want to hardcode a long path with OneDrive, I was thinking a relative path might work. Anyway, ignore the "relative path" bit.

Me: I'm having trouble because OneDrive is in the path with some users but not with others.

A.I. If your Access database file is stored in a path that includes OneDrive for some users but not others, you may encounter issues with referencing files or folders using a relative path, since the path to the OneDrive folder can vary depending on the user's configuration.
One solution to this problem is to use a combination of the Environ function and string manipulation functions to construct the path to the file or folder based on the user's environment variables.
For example, you could use the following code to construct the path to a file named "MyFile.txt" located in a subdirectory named "MyFolder" of the parent directory of the user's OneDrive folder:

A.I. In this code, the Environ function is used to retrieve the value of the OneDrive environment variable, which contains the path to the user's OneDrive folder. The path is then manipulated using the Left and InStrRev functions to remove the "OneDrive" folder from the path and obtain the parent directory. Finally, the full path to the file is constructed by appending the relative path to the parent directory.
This approach should work regardless of whether the user's OneDrive folder is included in the path or not.

Code:
Dim oneDrivePath As String
Dim filePath As String

' Get the path to the OneDrive folder
oneDrivePath = Environ("OneDrive")

' Remove the "OneDrive" folder from the path to get the parent directory
oneDrivePath = Left(oneDrivePath, InStrRev(oneDrivePath, "\") - 1)

' Construct the full path to the file
filePath = oneDrivePath & "\MyFolder\MyFile.txt"
 

tmyers

Well-known member
Local time
Today, 11:32
Joined
Sep 8, 2020
Messages
1,090
I'm not sure why you are using OneDrive to distribute the FE since it sounds like people are working on a LAN. I'm assuming we are talking about an FE. Is that correct?

Where is the BE? Isn't there a shared server you could use so you have a standard UNC reference?

If you are somehow using OD to distribute the FK, you have to ensure that only YOU have update access to the folder with the FK because you'll have a real mess on your hand if OD saves a copy of the FE that has been modified by a user and that now becomes the "master".
The backend is stored on a shared drive on a file server. I was not using OneDrive to distribute the FE, but some of my users use OneDrive to backup their computer and OneDrive alters/highjacks the file path to the desktop, documents and pictures which meant the path to their desktop was no longer C:\username\desktop so the batch file I had created to get the FE from the shared drive on the server and copy it to their desktop didnt work.

I have fixed it by making a file specifically for those who use OneDrive. I did have to "seed" the first copy of the FE onto their computer though for some odd reason. I am still tinkering with a nice clean way of doing this but in the midterm, this has been working nicely.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:32
Joined
Feb 19, 2002
Messages
43,785
I don't use the Desktop for local PC's. I have my batch file create a folder on the c: drive and download the master copy to that folder and run it from there. There is no error raised if the folder already exists so the batch file works for both new and existing users. I let the users chose where to put the batch file. I suggest the desktop but I don't force the issue. Forcing a file to the desktop is like the old days when every program that got installed thought it was the ONLY program in the universe and changed its environment which broke other applications.
 

Users who are viewing this thread

Top Bottom